java调http接口 post方式请求 服务器识别全是乱码 服务器识别utf-8的内容 哪位大神知道怎么解决吗?
发布网友
发布时间:2022-04-25 18:28
我来回答
共2个回答
热心网友
时间:2023-10-23 17:19
看下你post的方法,设置下这个
httpURLConnection.setRequestProperty("Charset", "utf-8");
拼接参数时:转一下格式
URLEncoder.encode(String.valueOf(value), "utf-8")
下面是我使用的POST方法,最简单的一种
Map<String, String> params = new HashMap<>(); //参数
HttpURLConnection urlCon = null;
URL urlInstance;
StringBuilder sbResult = new StringBuilder();
try {
urlInstance = new URL(url);
urlCon = (HttpURLConnection) urlInstance.openConnection();
urlCon.setRequestMethod("POST");
urlCon.setDoOutput(true); // 是否可以发送数据到服务器
urlCon.setDoInput(true); // 设置是否读服务端
urlCon.setUseCaches(false); // 设置是否缓存
urlCon.setConnectTimeout(15000);// 设置响应超时
// 固定格式
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlCon.setRequestProperty("Charset", "utf-8");
// 对参数进行处理
String data = "";
if (params != null) {
String value;
Set<String> set = params.keySet();// 获取到所有map的键
for (String string : set) {// 遍历参数,拼接data
value = params.get(string);
data += string + "=" + URLEncoder.encode(String.valueOf(value), "utf-8") + "&";
}
}
urlCon.setRequestProperty("Content-Length", String.valueOf(data.length())); // 设置长度
// 往服务器写入数据
OutputStream out = urlCon.getOutputStream();
out.write(data.getBytes());
out.flush();
// 接收服务器返回的数据
InputStream in = urlCon.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;// 每一行的数据
while ((line = br.readLine()) != null) {
sbResult.append(line);
}
热心网友
时间:2023-10-23 17:19
/**
* POST方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @param headers
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String uri, Map<String, String> paramMap,
Map<String, String> headers) throws ClientProtocolException,
IOException {
HttpPost httpPost = new HttpPost(uri);
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new ByteArrayEntity(paramMap.get("reqData").getBytes("UTF-8")));
//httpPost.setEntity(new UrlEncodedFormEntity(params,
//DEFAULT_ENCODING));
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}
public static String post(String uri, String contentType, String content)
throws Exception {
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
PostMethod post = new PostMethod(uri);
RequestEntity entity = new StringRequestEntity(content, contentType,DEFAULT_ENCODING);
post.setRequestEntity(entity);
int statusCode = client.executeMethod(post);
if (statusCode == 200) {
return post.getResponseBodyAsString();
}
throw new IOException("status is " + statusCode);
}
你可以设置下他的默认传递代码方式:DEFAULT_ENCODING 为UTF-8