读书人

httpclient 乱码有关问题

发布时间: 2012-03-20 14:01:11 作者: rapoo

httpclient 乱码问题
asp 页面

title = request.Form("Title")
If title="" then
Response.end()
End If
Response.Write title

t = "中国";
HttpClient http = new HttpClient();
PostMethod post = new PostMethod("http://127.0.0.1/post.asp");
NameValuePair id = new NameValuePair("ChannelID", "1");
NameValuePair classid = new NameValuePair("ClassID", "248");
NameValuePair st = new NameValuePair("Title", t);
NameValuePair data = new NameValuePair("HtmlFileDate", yyyy+mm+dd+String.valueOf(System.currentTimeMillis()));
NameValuePair content = new NameValuePair("TextContent", c);
post.setRequestBody(new NameValuePair[] {id,classid, st,content,data});
http.executeMethod(post);
System.out.println(post.getStatusCode()+"----------");
String str = new String(post.getResponseBodyAsString().getBytes("iso-8859-1"),"GBK");
System.out.println(str+"\n\n");

这里输出200----------
??
这乱码怎么解决啊

post.releaseConnection();



[解决办法]
指定字符集

Java code
private static final String CONTENT_CHARSET = "GBK";// httpclient读取内容时使用的字符集HttpClient client = new HttpClient();client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET); 

读书人网 >J2SE开发

热点推荐