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 = Inputstream.read (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