java调http接口 post方式请求 服务器识别全是乱码 服务器识别utf-8的内容 哪位大神知道怎么解决吗?
2个回答
展开全部
看下你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);
}
展开全部
/**
* 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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |