java的HttpClient请求,返回的数据部分乱码,请大侠指点呀!
HttpClienthttpClient=newHttpClient();StringBuilderresult=newStringBuilder();InputStre...
HttpClient httpClient = new HttpClient();
StringBuilder result = new StringBuilder();
InputStream ins = null;
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try
{
int statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK)
{
ins = method.getResponseBodyAsStream();
byte[] b = new byte[1024];
int r_len = 0;
while ((r_len = ins.read(b)) > 0)
{
result.append(new String(b, 0, r_len, method.getResponseCharSet()));
}
} else
{
System.err.println("Response Code: " + statusCode);
}
} catch (HttpException e)
{
System.err.println("Fatal protocol violation: " + e.getMessage());
} 展开
StringBuilder result = new StringBuilder();
InputStream ins = null;
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try
{
int statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK)
{
ins = method.getResponseBodyAsStream();
byte[] b = new byte[1024];
int r_len = 0;
while ((r_len = ins.read(b)) > 0)
{
result.append(new String(b, 0, r_len, method.getResponseCharSet()));
}
} else
{
System.err.println("Response Code: " + statusCode);
}
} catch (HttpException e)
{
System.err.println("Fatal protocol violation: " + e.getMessage());
} 展开
2个回答
展开全部
可以把你的这段代码简化一下
ins = method.getResponseBodyAsStream();
byte[] b = new byte[1024];
int r_len = 0;
while ((r_len = ins.read(b)) > 0)
{
result.append(new String(b, 0, r_len, method.getResponseCharSet()));
}
替换为:
byte[] ba = method.getResponseBody();
String str= new String(ba,"UTF-8"); //得到指定编码格式的字符串
这里的str就是你的返回值。这段代码我使用过,可以解决乱码问题
2015-02-02
展开全部
编码转换一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询