java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25115048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:40:06  来源:igfitidea点击:

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

javaproxyntlm

提问by Dunken

I am working on a thread java application that hitting a url to send sms messages
the problem is i am behind an NTLM proxy server and i have searched most of the day and tried many solutions but no success the application give the titled error and when i tried to print the error response i have found that its an error page comes from the proxy server

我正在开发一个线程 java 应用程序,它点击一个 url 来发送短信
问题是我在 NTLM 代理服务器后面,我已经搜索了一天的大部分时间并尝试了许多解决方案但没有成功应用程序给出了标题错误,当我试图打印错误响应我发现它的错误页面来自代理服务器

this is the hitting code

这是命中代码

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);

here i get the 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;
    }

any help is appreciated thanks

任何帮助表示赞赏谢谢

采纳答案by Dunken

After many tries i have realized that the code above is fine and the 400 error that i was getting is from not encoding the URL parameters that could have spaces

经过多次尝试,我意识到上面的代码很好,我得到的 400 错误是由于没有对可能有空格的 URL 参数进行编码

just used

刚用过

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

for parameters that code have spaces solved the problem

对于代码有空格的参数解决了问题