简体   繁体   English

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

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

I am working on a thread java application that hitting a url to send sms messages 我正在研究一个线程java应用程序,它打一个url来发送短信
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 问题是我在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 在这里,我得到了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 任何帮助表示赞赏谢谢

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 对于代码有空格的参数解决了问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器返回 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400 - server returns java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回URL的HTTP响应代码:400:java.io.IOException - Server returned HTTP response code: 400 for URL : java.io.IOException 获取java.io.IOException:服务器返回的HTTP响应代码:URL为400,但可以在浏览器中正常访问 - Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 - URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器为URL返回HTTP响应代码:400 - java.io.IOException: Server returned HTTP response code: 400 for URL: HttpURLConnection错误-java.io.IOException:服务器返回的HTTP响应代码:URL的400 - HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器返回的HTTP响应代码:URL的400 - java.io.IOException: Server returned HTTP response code: 400 for URL 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code java.io.IOException:HTTP响应代码:400用于URL - java.io.IOException: HTTP response code: 400 for URL java.io.IOException:服务器返回HTTP响应代码:502 - java.io.IOException: Server returned HTTP response code: 502
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM