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