;tomcat8发送http的post请求对方接受中文参数乱码,代码中设置了con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
时间: 2024-02-26 18:52:50 浏览: 72
如果你的代码已经设置了请求头的编码方式,但是对方仍然接收到了乱码,可能是因为对方没有正确处理编码方式导致的。在这种情况下,你可以尝试将请求体中的参数以UTF-8编码方式传递。例如:
```
String postData = "{\"name\":\"中文参数\"}";
byte[] postDataBytes = postData.getBytes("UTF-8");
URL url = new URL("http://example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
con.setDoOutput(true);
con.getOutputStream().write(postDataBytes);
```
这样做可以将中文参数以UTF-8编码方式传递到对方,避免出现乱码。同时,也需要确保对方能够正确处理POST请求和UTF-8编码方式。
相关问题
使用java如何實現以下請求curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{ "grant_type": "client_credentials", "client_id": "YXA6i-Ak8Ol4Eei2l11ZjV-EAg", "client_secret": "YXA6VunqiNxoB7IwXHInk1cGiXOOJfc", "ttl": "1024000" }' 'http://a1.easemob.com/easemob-demo/testapp/token'
使用Java可以通过HttpURLConnection或者OkHttp库发送POST请求实现以下命令:
curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{ "grant_type": "client_credentials", "client_id": "YXA6i-Ak8Ol4Eei2l11ZjV-EAg","client_secret": "YXA6VunqiNxoB7IwXHInk1cGiXOOJfc","ttl": "1024000" }' 'http://a1.easemob.com/easemob-demo/testapp/token'
示例代码:
使用HttpURLConnection发送POST请求:
```java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpPost {
public static void main(String[] args) throws Exception{
String url = "http://a1.easemob.com/easemob-demo/testapp/token";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//添加请求头
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
//发送POST请求
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes("{ \"grant_type\": \"client_credentials\", \"client_id\": \"YXA6i-Ak8Ol4Eei2l11ZjV-EAg\",\"client_secret\": \"YXA6VunqiNxoB7IwXHInk1cGiXOOJfc\",\"ttl\": \"1024000\" }");
wr.flush();
wr.close();
//读取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//打印响应结果
System.out.println(response.toString());
}
}
```
使用OkHttp库发送POST请求:
```java
import okhttp3.*;
import java.io.IOException;
public class OkHttpPost {
public static void main(String[] args) throws Exception{
String url = "http://a1.easemob.com/easemob-demo/testapp/token";
String json = "{ \"grant_type\": \"client_credentials\", \"client_id\": \"YXA6i-Ak8Ol4Eei2l11ZjV-EAg\",\"client_secret\": \"YXA6VunqiNxoB7IwXHInk1cGiXOOJfc\",\"ttl\": \"1024000\" }";
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
//发送POST请求
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
}
```
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")怎么添加请求参数
您可以使用conn.getOutputStream()方法获取输出流,然后将请求参数写入输出流中。例如:
```
String params = "param1=value1¶m2=value2";
byte[] postData = params.getBytes(StandardCharsets.UTF_8);
conn.setRequestProperty("Content-Length", String.valueOf(postData.length));
OutputStream os = conn.getOutputStream();
os.write(postData);
os.flush();
os.close();
```
在此示例中,我们将请求参数作为字符串“params”传递,并将其转换为字节数组。然后,我们设置Content-Length头,并获取输出流。最后,我们将请求参数写入输出流中并关闭它。
阅读全文