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