簡體   English   中英

java.io.IOException:服務器返回HTTP響應代碼:400表示URL

[英]java.io.IOException: Server returned HTTP response code: 400 for URL

我正在研究一個線程java應用程序,它打一個url來發送短信
問題是我在NTLM代理服務器后面,我已經搜索了大部分時間並嘗試了許多解決方案,但沒有成功應用程序給出標題錯誤,當我嘗試打印錯誤響應時,我發現它的錯誤頁面來自代理服務器

這是擊球代碼

System.setProperty("java.net.useSystemProxies", "true");
                    System.setProperty("http.proxyHost", AUTH_PROXY");
                    System.setProperty("http.proxyPort", AUTH_PROXY_PORT);
                    System.setProperty("http.proxyUser", AUTH_USER);
                    System.setProperty("http.proxyPassword", AUTH_PASSWORD);

                    Authenticator.setDefault(
                              new Authenticator() {
                                public PasswordAuthentication getPasswordAuthentication() {
                                  return new PasswordAuthentication(AUTH_USER, AUTH_PASSWORD.toCharArray());
                                }
                              }
                            );

                     URL url = new URL(urlString);

                     HttpURLConnection httpConn =(HttpURLConnection)url.openConnection();
                     httpConn.setReadTimeout(10000);
                     String resp = getResponse(httpConn);
                     logger.info("urlString=" + urlString);
                     logger.info("Response=" + resp);

在這里,我得到了respoonse

private String getResponse(HttpURLConnection Conn) throws IOException {


        InputStream is;
        if (Conn.getResponseCode() >= 400) {
            is = Conn.getErrorStream();
        } else {
            is = Conn.getInputStream();
        }


        String response = "";
        byte buff[] = new byte[512];
        int b = 0;
        while ((b = is.read(buff, 0, buff.length)) != -1) {
            response += new String(buff, 0, b);

        }
        is.close();

        return response;
    }

任何幫助表示贊賞謝謝

經過多次嘗試后,我意識到上面的代碼很好,我得到的400錯誤來自於不編碼可能有空格的URL參數

剛用過

URLEncoder.encode(urlParameter,"UTF-8");

對於代碼有空格的參數解決了問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM