title: HttpURLConnection get 500 response code in Jboss 4.0.3 behind a proxy
body: <p>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.</p>
  
  <p>When I try to connect without giving any proxy settings in the program, I get:</p>
  
  <blockquote>
    <p>Servlet.service() for servlet SearchRequestHandler threw exception
    java.io.IOException: Server returned HTTP response code: 500 for URL:
    <a href="http://www.sample.com/test.do" rel="nofollow">http://www.sample.com/test.do</a></p>
  </blockquote>
  
  <p><strong>If I give only the proxy server and the proxy port using:</strong></p>
  
  <pre><code>System.setProperty("java.net.useSystemProxies","false");
  System.setProperty("http.proxyHost", "192.168.1.226");
  System.setProperty("http.proxyPort", "3128");
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I get the authentication required error:</p>
  
  <blockquote>
    <p>Servlet.service() for servlet SearchRequestHandler threw exception
    java.io.IOException: Server returned HTTP response code: 407 for URL:
    <a href="http://www.sample.com/test.do" rel="nofollow">http://www.sample.com/test.do</a></p>
  </blockquote>
  
  <p><strong>And if I add the authentication header to the request like :</strong></p>
  
  <pre><code>String encoded = new String(Base64Encoder.encode(new String("shantha:******").getBytes()));
  _connection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>Authenticator.setDefault(new Authenticator() {
              public PasswordAuthentication getPasswordAuthentication() {
                  return (new PasswordAuthentication("shantha",
                          "******".toCharArray()));
              }
          });
  </code></pre>
  
  <p>Still I get the same :</p>
  
  <blockquote>
    <p>Servlet.service() for servlet SearchRequestHandler threw exception
    java.io.IOException: Server returned HTTP response code: 500 for URL:
    <a href="http://www.sample.com/test.do" rel="nofollow">http://www.sample.com/test.do</a></p>
  </blockquote>
  
  <p>.</p>
  
  <p><strong>The basic program without proxy settings, mentioned in above cases, is :</strong></p>
  
  <pre><code>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");
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Really appreciate if anybody can give a help on solving this issue.</p>