If you are not JSONP, you can use HttpClient to request data

Source: Internet
Author: User

When you use multiple servers for collaborative service, there is a case of cross-domain request data, in addition to using JSONP, you can use Apache httpclient to request data.

HttpClient is called at the service layer, the requested data may be JSON data, or it may be HTML code.

The following is the auxiliary tool class for httpclient, which can be called directly.

Httpclientutil.java

 Packagecom.taotao.utils;Importjava.io.IOException;ImportJava.net.URI;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Map;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;ImportOrg.apache.http.client.utils.URIBuilder;ImportOrg.apache.http.entity.ContentType;Importorg.apache.http.entity.StringEntity;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.apache.http.util.EntityUtils; Public classHttpclientutil { Public StaticString doget (string url, map<string, string>param) {        //Create a HttpClient objectCloseablehttpclient httpclient =Httpclients.createdefault (); String resultstring= ""; Closeablehttpresponse Response=NULL; Try {            //Create URIUriBuilder Builder =Newuribuilder (URL); if(param! =NULL) {                 for(String key:param.keySet ()) {Builder.addparameter (key, Param.get (key)); }} URI uri=Builder.build (); //Create an HTTP GET requestHttpGet HttpGet =NewHttpGet (URI); //Execute RequestResponse =Httpclient.execute (HttpGet); //determine if the return status is            if(Response.getstatusline (). Getstatuscode () = = 200) {resultstring= Entityutils.tostring (Response.getentity (), "UTF-8"); }        } Catch(Exception e) {e.printstacktrace (); } finally {            Try {                if(Response! =NULL) {response.close ();            } httpclient.close (); } Catch(IOException e) {e.printstacktrace (); }        }        returnresultstring; }     Public Staticstring doget (string url) {returnDoget (URL,NULL); }     Public StaticString doPost (string url, map<string, string>param) {        //Create a HttpClient objectCloseablehttpclient httpClient =Httpclients.createdefault (); Closeablehttpresponse Response=NULL; String resultstring= ""; Try {            //Create an HTTP POST requestHttpPost HttpPost =Newhttppost (URL); //Create a parameter list            if(param! =NULL) {List<NameValuePair> paramlist =NewArraylist<>();  for(String key:param.keySet ()) {Paramlist.add (NewBasicnamevaluepair (Key, Param.get (key)); }                //Simulation Formurlencodedformentity entity =Newurlencodedformentity (paramlist);            Httppost.setentity (entity); }            //executing an HTTP requestResponse =Httpclient.execute (HttpPost); Resultstring= Entityutils.tostring (Response.getentity (), "Utf-8"); } Catch(Exception e) {e.printstacktrace (); } finally {            Try{response.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returnresultstring; }     Public Staticstring doPost (string url) {returnDoPost (URL,NULL); }         Public Staticstring Dopostjson (string URL, string json) {//Create a HttpClient objectCloseablehttpclient httpClient =Httpclients.createdefault (); Closeablehttpresponse Response=NULL; String resultstring= ""; Try {            //Create an HTTP POST requestHttpPost HttpPost =Newhttppost (URL); //Create request Contentstringentity entity =Newstringentity (JSON, Contenttype.application_json);            Httppost.setentity (entity); //executing an HTTP requestResponse =Httpclient.execute (HttpPost); Resultstring= Entityutils.tostring (Response.getentity (), "Utf-8"); } Catch(Exception e) {e.printstacktrace (); } finally {            Try{response.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returnresultstring; }}

If you are not JSONP, you can use HttpClient to request data

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.