HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。(来自百度百科)
java代码:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class Test{
public static void testHttpClient(String phone, String name) throws IOException {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("utf-8");//设置编码
//构造POST请求,GET请求也类似
PostMethod postMethod = new PostMethod(url);//类似http://....
postMethod.addParameter("phone", phone);//Post参数
postMethod.addParameter("name", name);
httpClient.executeMethod(postMethod);
//解析response
StringBuffer buffer = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), "utf-8"));
String tmpLineStr = "";
while ((tmpLineStr = br.readLine()) != null) {
buffer.append(tmpLineStr);
}
String returnValue = buffer.toString();
System.out.println(returnValue);
postMethod.releaseConnection();
}
}