HttpUrlConnection獲取非200狀態碼的響應體


HttpUrlConnection 請求完成后  獲取響應體的方式根據狀態碼不同而不同

200狀態碼:connection.getInputStream();

非200狀態碼:connection.getErrorStream();

    public static Result sendPost(String httpUrl, String param) throws IOException,MalformedURLException  {
        HttpURLConnection connection = null;
         InputStream is = null;
         OutputStream os = null;
         BufferedReader br = null;
         Result result = new Result();
         try {
             URL url = new URL(httpUrl);
             // 通過遠程url連接對象打開連接
             connection = (HttpURLConnection) url.openConnection();
             // 設置連接請求方式
             connection.setRequestMethod("POST");
             // 設置連接主機服務器超時時間:15000毫秒
             connection.setConnectTimeout(60000);
             // 設置讀取主機服務器返回數據超時時間:60000毫秒
             connection.setReadTimeout(60000);
             // 默認值為:false,當向遠程服務器傳送數據/寫數據時,需要設置為true
             connection.setDoOutput(true);
             // 默認值為:true,當前向遠程服務讀取數據時,設置為true,該參數可有可無
             connection.setDoInput(true);
             // 設置傳入參數的格式:請求參數應該是 name1=value1&name2=value2 的形式。
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
             // 設置鑒權信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
             connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
             // 通過連接對象獲取一個輸出流
             os = connection.getOutputStream();
             if(param!=null){
                 os.write(param.getBytes());
             }
             // 通過連接對象獲取一個輸入流,向遠程讀取
             if (connection.getResponseCode() == 200) {
                 is = connection.getInputStream();
                 // 對輸入流對象進行包裝:charset根據工作項目組的要求來設置
                 br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                 StringBuffer sbf = new StringBuffer();
                 String temp = null;
                 // 循環遍歷一行一行讀取數據
                 while ((temp = br.readLine()) != null) {
                     sbf.append(temp);
                     sbf.append("\r\n");
                 }
                 result=Results.successWithData(sbf.toString(), "200", "獲取成功");
             }else {
                 try {
                    is = connection.getErrorStream();
                     // 對輸入流對象進行包裝:charset根據工作項目組的要求來設置
                     br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                     StringBuffer sbf = new StringBuffer();
                     String temp = null;
                     // 循環遍歷一行一行讀取數據
                     while ((temp = br.readLine()) != null) {
                         sbf.append(temp);
                         sbf.append("\r\n");
                     }
                     result.setMsg(sbf.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                 result.setCode(String.valueOf(connection.getResponseCode()));
                 result.setSuccess(false);
             }
    
         } catch (MalformedURLException e) {
            throw e;
         } catch (IOException e) {
             throw e;
         } finally {
             // 關閉資源
             if (null != br) {
                 try {
                     br.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             if (null != os) {
                 try {
                     os.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             if (null != is) {
                 try {
                     is.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                  }
             // 斷開與遠程地址url的連接
             connection.disconnect();
         }
         return result;
    }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM