Java calls restful WebService & Jackson

Source: Internet
Author: User

Package Com.bullshit.webcrawler.client.impl;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Java.util.list;import Com.bullshit.storage.entity.authuser;import Com.bullshit.storage.entity.proxy;import Com.bullshit.storage.entity.vo.authusersvo;import Com.fasterxml.jackson.databind.objectmapper;public Class Proxycollectionrestservice {private static final String TargetUrl = "http://localhost:8080/blueberry/rest/v1/api/          Proxies ";p ublic proxy[] getallproxies () {proxy[] proxies = null;try {URL restserviceurl = new URL (targeturl);          HttpURLConnection httpconnection = (httpurlconnection) restserviceurl.openconnection ();      Httpconnection.setrequestmethod ("GET");      Httpconnection.setrequestproperty ("Accept", "Application/json"); if (Httpconnection.getresponsecode ()! =) {throw new RunTimeexception ("HTTP GET Request Failed with Error code:" + Httpconnection.getresponsecode ()); } BufferedReader Responsebuffer = new BufferedReader (New InputStreamReader (httpconnection.getinput      Stream ())));          String output;          Objectmapper objectmapper = new Objectmapper ();          while (output = Responsebuffer.readline ()) = null) {proxies = Objectmapper.readvalue (output, proxy[].class); list<proxy> myObjects22 = objectmapper.readvalue (output, Objectmapper.gettypefactory (). Constructcollectionty              PE (List.class, Proxy.class));          System.out.println (Myobjects22.size ());          Break     } httpconnection.disconnect ();     } catch (Malformedurlexception e) {e.printstacktrace ();     } catch (IOException e) {e.printstacktrace (); }return proxies;} Public Authusersvo Postauthuserinfo (Authuser record) {AUTHUSERSVO AUTHUSERSVO = null; try {URL TargetuRL = new URL (targeturl);             HttpURLConnection httpconnection = (httpurlconnection) targeturl.openconnection ();             Httpconnection.setdooutput (TRUE);             Httpconnection.setrequestmethod ("POST");             Httpconnection.setrequestproperty ("Content-type", "Application/json");             Objectmapper objectmapper = new Objectmapper ();             String input = objectmapper.writevalueasstring (record);             OutputStream outputstream = Httpconnection.getoutputstream ();             Outputstream.write (Input.getbytes ());             Outputstream.flush ();                            if (Httpconnection.getresponsecode ()! =) {throw new RuntimeException ("Failed:http Error code:"             + Httpconnection.getresponsecode ());                           } BufferedReader Responsebuffer = new BufferedReader (New InputStreamReader (             (Httpconnection.getinputstream ())));            String output; System.out.println ("Output from server:\n");             while (output = Responsebuffer.readline ()) = null) {System.out.println (output);        } httpconnection.disconnect ();        } catch (Malformedurlexception e) {e.printstacktrace ();       } catch (IOException e) {e.printstacktrace (); }return AUTHUSERSVO;} public static void Main (string[] args) {Proxycollectionrestservice service = new Proxycollectionrestservice (); Proxy[] s = service.getallproxies (); System.out.println (s.length);}}

  

Java calls restful WebService & Jackson

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.