Encapsulated HttpClient Tool Class

Source: Internet
Author: User

import org. Apache. HTTP. HttpResponse;import org. Apache. HTTP. Httpstatus;import org. Apache. HTTP. Namevaluepair;import org. Apache. HTTP. Client. Entity. Urlencodedformentity;import org. Apache. HTTP. Client. Methods. HttpGet;import org. Apache. HTTP. Client. Methods. HttpPost;import org. Apache. HTTP. Impl. Client. Defaulthttpclient;import org. Apache. HTTP. Message. Basicnamevaluepair;import org. Apache. HTTP. Params. Coreconnectionpnames;import org. Apache. HTTP. Protocol. HTTP;import org. Apache. HTTP. Util. Entityutils;Import Java. NET. URI;Import Java. NET. URL;Import Java. Util. ArrayList;Import Java. Util. Linkedhashmap;Import Java. Util. List;public class Httputils {static public string doget (string url, string queryString) {try {URL u = N ew url (url + queryString);Uri uri = new uri (U. Getprotocol(), NULL, u. GetHost(), U. Getport(), U. GetPath(), U. Getquery(), NULL);HttpGet method = new HttpGet (URI);Defaulthttpclient client = new Defaulthttpclient ();Client. Getparams(). Setparameter(Coreconnectionpnames. CONNECTION_timeout, the* +);HttpResponse response = Client. Execute(method);if (response. Getstatusline(). Getstatuscode() = = Httpstatus. SC_OK) {return entityutils. toString(Response. GetEntity(),"Utf-8");}} catch (Exception e) {E. Printstacktrace();} return"";} static public string doPost (string url, String queryString) throws Exception {URL u = new url (URL);Uri uri = new uri (U. Getprotocol(), NULL, u. GetHost(), U. Getport(), U. GetPath(), U. Getquery(), NULL);HttpPost method = new HttpPost (URI);list<namevaluepair> namevaluepairs = new Arraylist<namevaluepair> ();Namevaluepairs. Add(New Basicnamevaluepair ("Data", queryString));Urlencodedformentity en = new urlencodedformentity (Namevaluepairs, HTTP. UTF_8);Method. Setentity(en);Defaulthttpclient client = new Defaulthttpclient ();Client. Getparams(). Setparameter(Coreconnectionpnames. CONNECTION_timeout, the* +);HttpResponse response = Client. Execute(method);if (response. Getstatusline(). Getstatuscode() = = Httpstatus. SC_OK) {return entityutils. toString(Response. GetEntity(),"Utf-8");}else {return"Connect error statuscode="+response. Getstatusline(). Getstatuscode();}    }};

Encapsulated HttpClient Tool Class

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.