二、Post 类请求接口
  Post请求都是向服务器提求表单的,所以要封装相应的HTTP请求头,并且把相应的参数添加到请求中,具体代码如下:
public static String PostRequests(String url, Map params, Map header) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
PostMethod method=new PostMethod(url);
int i;
//封装HTTP请求头
if(header != null && header.size()>0){
Set key = header.keySet();
for (Iterator ? it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
method.addRequestHeader(s,header.get(s));
}
}
//设置Http Post数据
if (params != null) {
NameValuePair[] postData = new NameValuePair[params.size()];
i=0;
for (Map.Entry entry : params.entrySet()) {
postData[i] = new NameValuePair(entry.getKey(), entry.getValue());
i++;
}
method.addParameters(postData);
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
}
} catch (IOException e) {
System.out.println("执行HTTP Post请求" + url + "时,发生异常!"+e);
} finally {
method.releaseConnection();
}
return response.toString();
}
  代码分析:
  (1)新建HttpClient和PostMethod,通过PostMethod来创建URL请求相关的URL和参数,然后通过HttpClient发起HTTP请求,接收返回结果。
  (2)通过addRequestHeader()将HTTP请求的头,添加到HttpClient中,从而达到对HTTP请求头的封装。
  (3)通过NameValuePair来组织接口数据,通过addParameters()将参数添加到HTTPClient中。
  (4)通过HttpClient的getResponseBodyAsStream()来获取请求结果,并将结果添加到StringBuffer里面,后以字符串的形式来返回。