Java HTTP request get vs. post

Source: Internet
Author: User

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

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.