I have implemented a servlet and deployed it in Jboss 4.0.3, in a war folder, to connect to an external web site. My machine is behind a proxy and I have valid proxy authentication credentials too.
When I try to connect without giving any proxy settings in the program, I get:
Servlet.service() for servlet SearchRequestHandler threw exception java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.sample.com/test.do
If I give only the proxy server and the proxy port using:
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);
I get the authentication required error:
Servlet.service() for servlet SearchRequestHandler threw exception java.io.IOException: Server returned HTTP response code: 407 for URL: http://www.sample.com/test.do
And if I add the authentication header to the request like :
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()));
}
});
Still I get the same :
Servlet.service() for servlet SearchRequestHandler threw exception java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.sample.com/test.do
.
The basic program without proxy settings, mentioned in above cases, is :
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");
}
I did try proxy selector also but it does not work and gives the same 500 error. If I run the program in a stand alone class having main method it works with proxy settings.
Really appreciate if anybody can give a help on solving this issue.