Get interfaces in different ways (including WebService, sockets ...) ) data

Source: Internet
Author: User

The development of interface program has been done recently. found that because of each company's different, they will use various ways to open the interface to our use of its data, there are HTTP, sockets, stored procedures, XFire ...

Different interface programs need to get the data they put out in different ways.

1.HTTP (How to get the data, the following is the HTTP use of the process and parsing, ~ ~ ~ mainly see Code)

1. Create the HttpClient object.
2. Create an instance of the request method and specify the request URL. Create a HttpGet object if you need to send a GET request, or create a HttpPost object if you need to send a POST request.
3. If you need to send request parameters, you can call the HttpGet, HttpPost common setparams (Hetpparams params) method to add the request parameters, and for HttpPost objects, you can also call Setentity ( Httpentity entity) method to set the request parameters.
4. Call the Execute (httpurirequest request) of the HttpClient object to send the request, which returns a HttpResponse.
5. Call HttpResponse's Getallheaders (), Getheaders (String name) and other methods to get the server's response header; call HttpResponse getentity () The Httpentity method gets the object that wraps the server's response content. This object is used by the program to obtain the server's response content.
6. Release the connection. The connection must be released regardless of the success of the execution method

1      PackageCom.xiong.webService;2     Importjava.util.ArrayList;3     ImportJava.util.Iterator;4     Importjava.util.List;5     ImportJava.util.Map;6     ImportNet.sf.json.JSONObject;7     Importorg.apache.http.HttpEntity;8     ImportOrg.apache.http.HttpResponse;9     ImportOrg.apache.http.HttpStatus;Ten     ImportOrg.apache.http.NameValuePair; One     Importorg.apache.http.client.HttpClient; A     Importorg.apache.http.client.entity.UrlEncodedFormEntity; -     ImportOrg.apache.http.client.methods.HttpPost; -     Importorg.apache.http.entity.StringEntity; the     Importorg.apache.http.impl.client.DefaultHttpClient; -     ImportOrg.apache.http.message.BasicHeader; -     ImportOrg.apache.http.message.BasicNameValuePair; -     ImportOrg.apache.http.params.BasicHttpParams; +     ImportOrg.apache.http.params.HttpConnectionParams; -     ImportOrg.apache.http.params.HttpParams; +     ImportOrg.apache.http.protocol.HTTP; A     Importorg.apache.http.util.EntityUtils; at     ImportOrg.apache.log4j.Logger; -     Importorg.dom4j.Document; -     Importorg.dom4j.DocumentException; -     ImportOrg.dom4j.DocumentHelper; -     Importorg.dom4j.Element; -     ImportOrg.dom4j.io.SAXReader; in  -  to@SuppressWarnings ({"Deprecation", "resource" }) +      PublicString WebService (String url,map<string,string>map) { -          theHttpPost Httprequst =NewHttpPost (URL);//Create a HttpPost object *         Try { $Httpparams httpparameters =NewBasichttpparams (); Panax NotoginsengHttpconnectionparams.setconnectiontimeout (Httpparameters, 600*1000);//set request time-out seconds -Httpconnectionparams.setsotimeout (Httpparameters, 600*1000);//set wait data timeout seconds theHttpClient HttpClient =Newdefaulthttpclient (httpparameters); +              Alist<basicnamevaluepair> list =NewArraylist<basicnamevaluepair>(); the              for(map.entry<string,string>MapEntry:map.entrySet ()) { +List.add (NewBasicnamevaluepair (Mapentry.getkey (), Mapentry.getvalue ())); -             } $Httprequst.setentity (Newurlencodedformentity (list,http. Utf_8)); $HttpResponse response =Httpclient.execute (httprequst); -             //Request succeeded -             if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) the             { -Httpentity httpentity =response.getentity (); WuyiString result = entityutils.tostring (httpentity, "UTF-8");//Take out the answer string theresult =dom4janalysis (result); -                 returnresult; Wu             }     -}Catch(Exception e) { About e.printstacktrace (); $             return""; -         }  -         return NULL; -     } A     /** + * Parse the returned file the * According to the needs of their own analysis -      * @paramStr $      * @return the      */ the      Publicstring dom4janalysis (String str) { the          the         Try { -Document document =Documenthelper.parsetext (str); inElement rootelement =document.getrootelement (); the              the             returnRootelement.gettext (); About}Catch(documentexception e) { the e.printstacktrace (); the         } the         return NULL; +}

How to use HTTP

1  Packagecom.xiong.test;2 ImportCom.xiong.webService;3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6  Public classTest1 {7      Public Static voidMain (string[] args) {8         9Baseservice Baseservice =NewBaseservice ();Ten          Onemap<string,string> map =NewHashmap<string, string>(); AMap.put ("name", "Zhang San"); -         //Baseservice.get_exist_user_info--WebService address -String result =Baseservice.webservice (baseservice.get_exist_user_info, map); the     } -}

Get interfaces in different ways (including WebService, sockets ...) ) 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.