Java Http错误流Garble

时间:2017-12-16 22:47:16

标签: java api https httpsurlconnection

我正在尝试使用Java中的restful api,并且在我收到http错误代码时尝试解析服务器响应。 (api以JSON形式返回自定义响应。)

当我在浏览器中键入api url时,故意传递不良参数以获得422响应,api返回json解释出现了什么问题。

但是,在我使用getErrorStream的java代码中,响应似乎是加密或编码的。不确定是哪一个。

任何人都可以解释可能出现问题的地方吗?

    System.setProperty("https.proxyHost", proxyHost);
    System.setProperty("https.proxyPort", proxyPort);


    URL workfrontProject = new URL(apiURLwithparams);
    HttpsURLConnection httpConn =(HttpsURLConnection) workfrontProject.openConnection();


    httpConn.setAllowUserInteraction(false);
    httpConn.setDoInput(true);
    httpConn.setDoOutput(true);
    httpConn.setUseCaches(false);
    httpConn.setRequestMethod("GET");
    httpConn.setConnectTimeout(60000);
    httpConn.setReadTimeout(300000);
    httpConn.connect();
    System.out.println("Connected to: "+httpConn.getURL().toString());

    //returns 422 because I pass bad params intentionally
    // hence error stream should be set

    System.out.println("The Response Code is: "+httpConn.getResponseCode());

    Scanner wfAdminIssues = new Scanner(new InputStreamReader(httpConn.getErrorStream()));
    StringBuilder response = new StringBuilder();

    while (wfAdminIssues.hasNext()) {
        System.out.print(wfAdminIssues.next());
    }

    wfAdminIssues.close();

然后错误流输出看起来像这样(必须使用图像导致字符甚至不会复制)。Bad Text

在浏览器中,虽然我得到:{“error”:{“class”:“java.lang.IllegalArgumentException”,“message”:“不支持字段(fakeField)”}}

0 个答案:

没有答案