如何在HttpURLConnection IOException获取内容?

时间:2017-07-13 02:36:48

标签: java jsp httpurlconnection

我的客户做了一种WS。 例如,即使在401等错误连接上,此WS也可以返回有效数据。

POST /tst/service/v1/service_client HTTP/1.1
Accept: application/json
app-token: 1234
Content-Type: application/json; charset=utf8
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.7.0_21
Host: customer.com
Connection: keep-alive
Content-Length: 285

{"idPedidoExterno":16798135,"amount":240}

HTTP/1.1 401 Unauthorized
date: Thu, 13 Jul 2017 01:53:22 GMT
access-control-allow-origin: *
server: Jetty(9.2.9.v20150224)
access-control-max-age: 3600
access-control-allow-headers: x-requested-with, content-type
connection: close
access-control-allow-methods: POST, PUT, GET, OPTIONS, DELETE
x-application-context: gateway-panvel-service:default:9021
Content-Type: application/json;charset=UTF-8

{"code":90456,"message":"Cliente n..o aprovado na an..lise antifraude."}

所以我需要抓住那个JSON

{"code":90456,"message":"Cliente n..o aprovado na an..lise antifraude."}

我怎么能这样做?我知道底部的答案是getErrorStream,但对我来说不起作用。

我有一个捕获(IOException e)来捕获401错误,但无法访问HttpURLConnection对象。

这是我的代码

try{

    URL url = new URL(vURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept", "application/json");
    conn.setRequestProperty("Content-Type", "application/json; charset=utf8");

    if(additionalParams.get("JSON_Data") != null){

        OutputStream os = conn.getOutputStream();
        os.write(DataWS.getBytes("UTF-8"));
        os.close();

    }

    if (conn.getResponseCode() != 200) {

        result.put("resultWS","error_diff_200");

        result.put("resultWS_desc",conn.getResponseCode());
        result.put("WS",vURL);
        result.put("JSON",DataWS);


        BufferedReader br1 = new BufferedReader(new InputStreamReader((conn.getErrorStream())));


            StringBuilder sb1 = new StringBuilder();
            String output1;

            while ((output1 = br1.readLine()) != null) {
                sb1.append(output1);
            }
            JSONObject x11 = new JSONObject(sb1.toString());
            result.put("dataWS",x11);


    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn.getInputStream())));


    StringBuilder sb = new StringBuilder();
    String output;

    while ((output = br.readLine()) != null) {
        sb.append(output);
    }

    conn.disconnect();

    try{
    JSONObject x1 = new JSONObject(sb.toString());
    result.put("dataWS",x1);    
    }
    catch(Exception ex){
        JSONArray arr1 = new JSONArray(sb.toString());
        result.put("dataWS",arr1);  
    }
    result.put("resultWS","OK"); 
    result.put("WS",vURL);

  } catch (MalformedURLException e) {

      result.put("resultWS","error");
      result.put("dataWS","errorMalformed");    
      result.put("result_desc_Malformed",e.toString());
      result.put("WS",vURL);

  } catch (IOException e) {

      result.put("resultWS","error");
      result.put("dataWS","errorIO");   


        result.put("resultWS_desc_IOException",e.toString());
        result.put("WS",vURL);


  }

这是我得到的结果:

{
"resultWS_desc" : 401,
"resultWS" : "error",
"resultWS_desc_IOException" : "java.io.IOException: Server returned HTTP response code: 401 for URL: http://server/tst/gateway/v1/service",
"dataWS" : "errorIO"

}; ]

0 个答案:

没有答案