HttpURLConnection 發送PUT請求 json請求體 與服務端接收

來源:互聯網
上載者:User

標籤:sage   tin   byte   line   接收   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請求            conn.setRequestMethod("PUT");            conn.setUseCaches(false);            conn.setInstanceFollowRedirects(true);            // json格式上傳的模式            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() == 200) {                  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 {                //如果出錯,一定要檢查URL對沒有!                BufferedReader br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));                String jsontxt = br.readLine();                br.close();            }            conn.disconnect();                    }        catch (Exception e)        {            System.out.println(e.getMessage());        }    }

  

服務端:

@Overrideprotected void doPut(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("我是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();// 內容     resp.setHeader("resp1", "resp1Text");super.doPut(req, resp);}

  

HttpURLConnection 發送PUT請求 json請求體 與服務端接收

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.