public
String submitPost(String url, NameValuePair nvp[])
...
{
HttpClient hc = new HttpClient();
//httpclient默认使用ISO-8859-1读取http响应的内容。我们要把它转化为GBK
hc.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
PostMethod hm = new PostMethod(url);
//对输入字符编码
EncodingUtil.formUrlEncode(nvp, "GBK");
hm.addParameters(nvp);
int statusCode = -1;
try ...{
hm.getResponseBodyAsString();
statusCode = hc.executeMethod(hm);
} catch (HttpException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
};
try ...{
if(hm.getResponseBodyAsString()!=null)...{
return hm.getResponseBodyAsString();
}
} catch (IOException e) ...{
e.printStackTrace();
}
return null;
}
HttpClient hc = new HttpClient();
//httpclient默认使用ISO-8859-1读取http响应的内容。我们要把它转化为GBK
hc.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
PostMethod hm = new PostMethod(url);
//对输入字符编码
EncodingUtil.formUrlEncode(nvp, "GBK");
hm.addParameters(nvp);
int statusCode = -1;
try ...{
hm.getResponseBodyAsString();
statusCode = hc.executeMethod(hm);
} catch (HttpException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
};
try ...{
if(hm.getResponseBodyAsString()!=null)...{
return hm.getResponseBodyAsString();
}
} catch (IOException e) ...{
e.printStackTrace();
}
return null;
}
注意:hc.getParams()方法,在httpclient2.0里是没有的。