A handwritten HTTP client

Source: Internet
Author: User

public class HTTPClient {public static final string get = "Get";p ublic static final string post = "POST";p ublic static fin Al string put = "put";p ublic static final String delete = "delete";p ublic static string GetData (String path) throws Except Ion {String ResponseData = Visitwithoutparam (path, GET); return responsedata;}  public static string PostData (string path, string data) throws Exception {string responsedata = Visitwithparam (path, POST, data); return responsedata;} public static string PutData (string path, string data) throws Exception {return ' to do put data ';} public static string DeleteData (string path) throws Exception {return ' to do delete data ';} public static string Visitwithparam (string path, string method, String body) throws Exception{inputstream InputStream = Nu ll BufferedReader BufferedReader = null;try {URL url = new URL (path); HttpURLConnection httpurlconnection = (httpurlconnection) url.openconnection (); Httpurlconnection.setdoinput (true); Httpurlconnection.setdooutput (True); Httpurlconnection.setusecaches (false); Httpurlconnection.setrequestmethod (method); Httpurlconnection.setrequestproperty ("Content-type", "Application/json"); Httpurlconnection.setrequestproperty (" CharSet "," Utf-8 "), Httpurlconnection.setrequestproperty (" Content-length ", Integer.tostring (Body.getbytes (). Length));D ataoutputstream dataoutputstream = new DataOutputStream (Httpurlconnection.getoutputstream ()); Dataoutputstream.writebytes (body);d Ataoutputstream.flush ();d ataoutputstream.close (); InputStream = Httpurlconnection.getinputstream (); BufferedReader = new BufferedReader (new InputStreamReader (InputStream)); StringBuilder StringBuilder = new StringBuilder (); String line = null;while (line = Bufferedreader.readline ())! = null) stringbuilder.append (line); return Stringbuilder.tostring ();} catch (Exception e) {throw new Exception (e);} finally {//no need to change null Actuallytry {if (BufferedReader! = null) b Ufferedreader.close (); if (InputStream! = null) Inputstream.close ();} catch (Exception e) {}}}public static string Visitwithoutparam (string path, String method) throws Exception {InputStream inputstream = null; BufferedReader bufferedreader = null; URL url;try {url = new URL (path); HttpURLConnection httpurlconnection = (httpurlconnection) url.openconnection (); Httpurlconnection.setdoinput (true); Httpurlconnection.setusecaches (false); Httpurlconnection.setrequestmethod (method); InputStream = Httpurlconnection.getinputstream (); BufferedReader = new BufferedReader (new InputStreamReader (InputStream)); StringBuilder StringBuilder = new StringBuilder (); String line = null;while (line = Bufferedreader.readline ())! = null) stringbuilder.append (line); return Stringbuilder.tostring ();} catch (Exception e) {throw new Exception (e);} finally {try {if (BufferedReader! = null) bufferedreader.close (); if (inputstr EAM = null) Inputstream.close ();} catch (Exception e) {}}}}

A piece of code that I wrote a long time ago, when I forgot apache.httpclient this thing, and then I made a wheel again.

A handwritten HTTP client

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.