The HTTP client is an open source package under Apache that is used to write Java code that accesses the service using the HTTP protocol.
The main features of the Http client:
(1) Implementation of all HTTP methods (Get,post,put,head, etc.) (2) Support automatic Steering (3) Support HTTPS Protocol (4) support proxy server, etc.
Packages required with HTTP client: Commons-httpclient.jar,commons-logging.jar,commons-codec-1.x.jar
- HttpClient can be downloaded from http://jakarta.apache.org/commons/httpclient/downloads.html
- HttpClient uses Apache Jakarta Common under the sub-project logging, you can from this address Http://jakarta.apache.org/site/downloads/downloads_ commons-logging.cgi download to common logging, remove Commons-logging.jar Add to CLASSPATH from the downloaded zip package
- HttpClient uses Apache Jakarta Common under the sub-project codec, you can from this address Http://jakarta.apache.org/site/downloads/downloads_ commons-codec.cgi download to the latest common codec, remove Commons-codec-1.x.jar from the downloaded package and add to CLASSPATH
Http client use (post as an example)
HttpClient HttpClient = new HttpClient ();
Access Host IP
String host = "198.32.32.2";
Port number
int port = 8081;
Agreement
String protocol = "HTTP";
Httpclient.gethostconfiguration (). Sethost (host,port, Protocol);
Specific access Path
String url = ""
HttpMethod method = new Postmethod (URL);
Parameters
namevaluepair[] data = {New Namevaluepair ("id", "youusername"), New Namevaluepair ("passwd", "Yourpwd")};
Method.setrequestbody (data);
Perform
int status = Httpclient.executemethod (method);
Get back
String response = new String (Method.getresponsebody (), "UTF-8");
Reference Document: http://www.ibm.com/developerworks/cn/opensource/os-httpclient/