HttpURLConnection在代理后面的Jboss 4.0.3中获得500响应代码

时间:2015-09-14 06:11:55

标签: java servlets proxy jboss-4.0.x

我已经实现了一个servlet并将其部署在Jboss 4.0.3中的war文件夹中,以连接到外部网站。我的机器在代理后面,我也有有效的代理身份验证凭据。

当我尝试连接而不在程序中提供任何代理设置时,我得到:

  

servlet的Servlet.service()SearchRequestHandler引发了异常   java.io.IOException:服务器返回HTTP响应代码:500为URL:   http://www.sample.com/test.do

如果我只使用以下代码服务器和代理端口:

System.setProperty("java.net.useSystemProxies","false");
System.setProperty("http.proxyHost", "192.168.1.226");
System.setProperty("http.proxyPort", "3128");

OR

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.226", 3128));
URL _serverConnection = new URL("http://www.sample.com/test.do");
HttpURLConnection _connection = (HttpURLConnection) _serverConnection.openConnection(proxy);

我收到了需要身份验证的错误:

  

servlet的Servlet.service()SearchRequestHandler引发了异常   java.io.IOException:服务器返回HTTP响应代码:407为URL:   http://www.sample.com/test.do

如果我将身份验证标头添加到请求中,例如:

String encoded = new String(Base64Encoder.encode(new String("shantha:******").getBytes()));
_connection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);

OR

Authenticator.setDefault(new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication("shantha",
                        "******".toCharArray()));
            }
        });

我仍然得到同样的结果:

  

servlet的Servlet.service()SearchRequestHandler引发了异常   java.io.IOException:服务器返回HTTP响应代码:500为URL:   http://www.sample.com/test.do

上述案例中提到的没有代理设置的基本程序是:

URL _serverConnection = new URL("http://www.sample.com/test.do");
HttpURLConnection _connection = null;
StringBuffer _strBuffer = new StringBuffer();

_connection = (HttpURLConnection) _serverConnection.openConnection();

_connection.setUseCaches(false);
_connection.setDoOutput(true);
_connection.setDoInput(true);

_connection.setRequestMethod("POST");

OutputStream _outStream = _connection.getOutputStream ();
BufferedWriter _bufWriter = new BufferedWriter( new OutputStreamWriter(_outStream, "UTF-8"));
_bufWriter.write("search-param-string");
_bufWriter.flush();
_bufWriter.close();
_outStream.close ();

int _status = _connection.getResponseCode();

BufferedReader _bufReader = new BufferedReader(new InputStreamReader(_connection.getInputStream()));
String _outputLine;
while ((_outputLine = _bufReader.readLine()) != null) {
    _strBuffer.append(_outputLine + "\n");
}

我确实尝试了代理选择器,但它不起作用并给出了相同的500错误。如果我在具有main方法的独立类中运行该程序,它可以使用代理设置。

真的很感激,如果有人可以帮助解决这个问题。

0 个答案:

没有答案