HttpURLConnection send put request JSON request body and server receive

Source: Internet
Author: User

Tag:sage   tin   byte   line    receive     har   ext1   readline   rect   

public void Testhttp () {String result = "";            try {URL posturl = new URL ("Http://localhost:8080/webTest/TestSerlvte");            HttpURLConnection conn = (httpurlconnection) posturl.openconnection ();            Conn.setdooutput (TRUE);              Conn.setdoinput (TRUE);//Conn.setconnecttimeout (5 * 1000);            Put request Conn.setrequestmethod ("put");            Conn.setusecaches (FALSE);            Conn.setinstancefollowredirects (TRUE);            JSON format upload mode conn.setrequestproperty ("Content-type", "application/json;charset=utf-8");            Conn.setrequestproperty ("Header1", "Header1text1");            Conn.setrequestproperty ("Header2", "header1text2");            String payload = "{\" appid\ ": 6,\" appkey\ ": \" 0cf0vgd/clirvmvvt/r5heuth5m=\ ", \" openid\ ": 200}";            OutputStreamWriter OSW = new OutputStreamWriter (Conn.getoutputstream ());            Osw.write (payload);          Osw.flush ();  Osw.close ();            System.out.println (Conn.getresponsecode ()); if (conn.getresponsecode () = =) {InputStreamReader ISR = new InputStreamReader (Conn.getinputstream ())                  ;                  BufferedReader br = new BufferedReader (ISR);                  String inputline = null;                  while ((Inputline = Br.readline ()) = null) {result + = Inputline;                  } isr.close ();              Conn.disconnect ();                } else {//If an error occurs, be sure to check the URL pair no!                BufferedReader br = new BufferedReader (New InputStreamReader (Conn.geterrorstream ()));                String jsontxt = Br.readline ();            Br.close ();                    } conn.disconnect ();        } catch (Exception e) {System.out.println (E.getmessage ()); }    }


Service side:

@Overrideprotected void DoPut (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { System.out.println ("I am Put"); Req.getheader ("Header4"); System.out.println ("header4" + "----" + req.getheader ("Header4")); ServletInputStream InputStream = Req.getinputstream ();    StringBuilder content = new StringBuilder ();      Byte[] B = new byte[req.getcontentlength ()];      int lens =-1;      while ((lens = (b)) > 0) {          content.append (new String (b, 0, Lens));      }      String Strcont = content.tostring ();//Content     Resp.setheader ("Resp1", "Resp1text"); Super.doput (req, resp);}


HttpURLConnection send put request JSON request body and server receive

Related Article

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: 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.