How to Learn HttpClient GET and POST requests in Java

Source: Internet
Author: User

How to Learn HttpClient GET and POST requests in Java
Author: fengxueting Source: http://www.cnblogs.com/fengxueting-px/

Java-based HttpClient GET and POST requests

1. Preface
2. GET request
3. POST request

 

I. Preface

This blog POST records HttpClient's GET and POST requests.

This article is based on the following:

Http://huangqiqing123.iteye.com/blog/2054436 (addHeader and setHeader of HttpClient)
Http://zywang.iteye.com/blog/916834 (use Apache HttpClient to access JSP to send GET and POST requests)
Http://www.linuxidc.com/Linux/2012-02/55502p3.htm (use of HttpClient 4.0)

 

2. GET request

An example of a GET request is as follows:

    //httpClient    HttpClient httpClient = new DefaultHttpClient();    // get method    HttpGet httpGet = new HttpGet("https://api.microsofthealth.net/v1/me/Summaries/Daily");        // set header    String Au="Bearer "+access_token;    httpGet.setHeader("Authorization",Au);        //response    HttpResponse response = null;      try{        response = httpClient.execute(httpGet);    }catch (Exception e) {}     //get response into String    String temp="";    try{        HttpEntity entity = response.getEntity();        temp=EntityUtils.toString(entity,"UTF-8");    }catch (Exception e) {}         return temp;

 

Iii. POST requests:

An example of a GET request is as follows:

    //httpClient    HttpClient httpClient = new DefaultHttpClient();    // get method    HttpPost httpPost = new HttpPost("https://login.live.com/oauth20_token.srf");          // set header    httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");     //set params    List<NameValuePair> params = new ArrayList<NameValuePair>();    params.add(new BasicNameValuePair("client_id",client_id));    params.add(new BasicNameValuePair("redirect_uri",redirect_uri));    params.add(new BasicNameValuePair("client_secret",client_secret));    params.add(new BasicNameValuePair("code",code));    params.add(new BasicNameValuePair("grant_type","authorization_code"));    try{        httpPost.setEntity(new UrlEncodedFormEntity(params));    }catch (Exception e) {}     //response    HttpResponse response = null;      try{        response = httpClient.execute(httpPost);    }catch (Exception e) {}        //get response into String    String temp="";    try{        HttpEntity entity = response.getEntity();        temp=EntityUtils.toString(entity,"UTF-8");    }catch (Exception e) {}        return temp;    

 

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.