HTTP, get, post, httpclient

Source: Internet
Author: User

Http://www.blogjava.net/willpower88/archive/2008/04/14/192720.html

Http://www.blogjava.net/willpower88/archive/2008/04/14/192679.html

Http://lavasoft.blog.51cto.com/62575/168276/

Method 1: Send http get/POST requests in Java only. How?

Client: send a request

Give a URL, get the connection from the URL, and set various parameters (such as keep-alive, content-type, usecaches, and dooutput) on the connection ). After setting, I should write something to the connection. My outputstreamwriter is ready to go. Where can I write it? Connection. After writing, the OSW is switched off and the connection is also switched off.

Server: receives requests

Request to obtain data.

 

The entire logic is so simple.

 

Method 2: httpclient, an open-source project, adds makeup and looks more comfortable

 

Change a client and set the target server address and port.

Returns a method (get/post ).

Client execution method.

The method returns the server status and information returned by the server. Of course, the method is also responsible for releasing the connection. Note that the method is complete, not the client. The client is only responsible for the "management" method, and the specific execution is done by the method itself.

Note the following:

1) Get (read) is always so simple? You can set parameters such as XX and XX, and set parameters such as namevaluepair for post (deletion, query, and modification.

2) It does not matter whether simple username/password verification is required. It is better to pass the post parameter together;

3) It does not matter if the XML file needs to be directly uploaded, as shown in the following figure: 2)

4) In a multi-threaded environment, the client must be provided with a multi-threaded big manager multithreadedhttpconnectionmanager.

 

One little demo:

    public static void main(String[] args) throws HttpException, IOException {        testHttp(0);        testHttp(1);    }        /**     * type=0, get     * type=1, post     * http://haoma.imobile.com.cn/index.php?mob=18858111994     */    public static void testHttp(int type) throws HttpException, IOException{        HttpClient client = new HttpClient();        client.getHostConfiguration().setHost("haoma.imobile.com.cn", 80, "http");        HttpMethod method = null;        switch(type){        case 0:            method = getGetMethod();            break;        case 1:            method = getPostMethod();            break;        }                client.executeMethod(method);        System.out.println("statusLine = " + method.getStatusLine());        String response = new String(method.getResponseBodyAsString());        System.out.println("response = " + response);                method.releaseConnection();    }    private static HttpMethod getGetMethod() {        return new GetMethod("/index.php?mob=18858111994");    }    private static HttpMethod getPostMethod() {        PostMethod post = new PostMethod("/index.php");        NameValuePair simcard = new NameValuePair("mob", "18858111994");        post.setRequestBody(new NameValuePair[] { simcard });        return post;    }

 

 

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.