Http clinet Use

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.