Http Client是个apache下的一个开源包,用于使用http协议访问服务的java代码编写。
Http Client的主要功能:
使用Http Client需要的包:commons-httpclient.jar,commons-logging.jar,commons-codec-1.x.jar
Http Client使用(以Post方式为例)
HttpClient httpClient = new HttpClient();
// 访问主机IP
String host = “198.32.32.2”;
// 端口号
int port = 8081;
// 协议
String protocol = “http”;
httpClient.getHostConfiguration().setHost(host,port, protocol);
// 具体访问路径
String url = “”
HttpMethod method = new PostMethod(url);
// 参数
NameValuePair[] data = { new NameValuePair("id", "youUserName"), new NameValuePair("passwd", "yourPwd") };
method.setRequestBody(data);
// 执行
int status = httpClient.executeMethod(method);
// 得到返回
String response = new String(method.getResponseBody(),"UTF-8");
参考文档:http://www.ibm.com/developerworks/cn/opensource/os-httpclient/
原文地址:http://www.cnblogs.com/hyiam/p/3817036.html