作者:鸟的蛋蛋 | 来源:互联网 | 2023-05-18 18:45
DefaultHttpClientclientnull;clientnewDefaultHttpClient();client.getParams().setParam
DefaultHttpClient client = null;
client = new DefaultHttpClient();
client.getParams()
.setParameter(CoreProtocolPNames.USER_AGENT,
"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101
Firefox/11.0");
client.getParams().setParameter(
CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
client.setHttpRequestRetryHandler(requestRetryHandler);
HttpPost hp = new HttpPost("http://www.intertops.eu/logon.htm?lan=en");
hp.addHeader。。。。。//hp增加请求头
List
list = new ArrayList();
list.add。。。。 //list添加参数
HttpResponse response;
UrlEncodedFormEntity uefEntity;
try {
hp.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
System.out.println("UnsupportedEncodingException");
e1.printStackTrace();
}
try {
response = client.execute(hp);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
response = null;
}
// 使用ResponseHandler接口处理响应,HttpClient使用ResponseHandler会自动管理连接的释放,解决了对连接的释放管理
private static ResponseHandler responseHandler = new ResponseHandler() {
// 自定义响应处?
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
String charset = EntityUtils.getContentCharSet(entity) == null ? "UTF-8"
: EntityUtils.getContentCharSet(entity);
return new String(EntityUtils.toByteArray(entity), charset);
} else {
return null;
}
}
};
输出内容是一堆乱码,求答案
2 个解决方案
可以试试这样转码……
String ?="";
String s=new String(super.getRequest().getParameter(?).getBytes("ISO-8859-1;charset=utf-8"));