需要用到的jar包:
commons-codec-1.4.jar
commons-httpclient-3.1.jar
commons-logging-1.0.4.jar
jsoup-1.6.2.jar
/**
* 通过Post方式请求webserviec方法
* @param methodPost 接口地址+"/"+请求的方法名
* @param host
* @param params 请求参数
* @return
* @throws HttpException
* @throws IOException
*/
public String webServicePost(String postMethod, String host, Map<String, String> params)
throws HttpException, IOException {
InputStream is = null;
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(postMethod);
method.setRequestHeader("Host", host);
method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
for (String key : params.keySet()) {
method.setParameter(key, params.get(key));
}
client.executeMethod(method);
is = method.getResponseBodyAsStream();
org.jsoup.nodes.Document document = Jsoup.parse(is, "utf-8", "");
if (is != null)
is.close();
return document.toString();
}