Put a connection http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml first
Then post the code
Public voidSendget (String Requrl, String params, string contentType)throwsException {stringbuffer result=NewStringBuffer (); BufferedReader reader; URLConnection Conn; URL URL=NewURL (Requrl); Conn=url.openconnection (); Conn.setrequestproperty ("Accept", ContentType); Conn.setrequestproperty ("Content-type", ContentType); Conn.setrequestproperty ("Connection", "keep-live"); //Conn.setrequestproperty ("Authorization", "Bearer" + this.token);Conn.connect (); Map<string, list<string>> map =Conn.getheaderfields (); for(String key:map.keySet ()) {System.out.println (key+ "--->" +Map.get (key)); } Reader=NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while(line = Reader.readline ())! =NULL) {result.append (line); } reader.close (); SYSTEM.ERR.PRINTLN (result); }
The above is the Get method
Public voidSendpost (String Requrl, jsonobject body)throwsException {stringbuffer result=NewStringBuffer (); PrintWriter writer; BufferedReader reader; URLConnection Conn; URL URL=NewURL (Requrl); Conn=url.openconnection (); Conn.setrequestproperty ("Accept", "Application/vnd.plcm.plcm-recording-info-list+json"); Conn.setrequestproperty ("Content-type", "Application/vnd.plcm.plcm-recording-info-list+json"); Conn.setrequestproperty ("Connection", "keep-alive"); //conn.setrequestproperty ("User-agent",//"mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) gecko/20100101 firefox/34.0 ");Conn.setrequestproperty ("Authorization", "Bearer" + This. Token); Conn.setdooutput (true); Conn.setdoinput (true); Writer=NewPrintWriter (Conn.getoutputstream ()); if(Body! =NULL) {writer.write (body.tostring ()); } writer.flush (); Conn.connect (); Map<string, list<string>> map =Conn.getheaderfields (); for(String key:map.keySet ()) {System.out.println (key+ "--->" +Map.get (key)); } Reader=NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while(line = Reader.readline ())! =NULL) {result.append (line); } reader.close (); System.err.println (Result.tostring ()); }
The above is the post-submitted code
(please automatically ignore accept and Content-type in the request head)
To say, before sending a GET request, accidentally pasted
writer = new printwriter (Conn.getoutputstream ()); if (body! = null) { writer.write (body.tostring ()); } Writer.flush ();
This part of the code, the server throws 415 as response code
Unsupported Media type
Combined with the first link, can almost believe that get only one package, and post two times, in layman's words, assuming that a httprequest as a header and body composition, get once the header and body are sent together, and post first send header , and then send the body,
This remains to be verified later.
Java HTTP request get and post