title: HttpURLConnection Proxy strange IOException ("Error writing to server")
body: <p>I want to create a function which creates a (HTTP)URLConnection thru a Proxy Server.<br>I have tested this with some free proxys from the internet.<br> With all of them (i have tested), <strong>everything works fine</strong>.<br><br>But if I run this in my customers network (with customers proxy-server) the code fails at <code>urlCon.getInputStream()</code> with an IOException.<br><br>I did some tests with Apache HttpClient on the same network (with same proxy-server).<br> The Apache HttpClient works fine.<br>But why does my code (with HttpURLConnection) not work?</p>
  
  <p><strong>EDIT:</strong> In other words:<br>
  Why is my code throwing <strong>only</strong> an exception, when using the customers proxy server?<br>Why i get <strong>no exception</strong>, when using ApacheHTTPClient and the customers proxy?<br><br>
  Here is my working code:</p>
  
  <pre><code>    final String proxyURL = txtProxyURL.getText();
      final String proxyPort = txtProxyPort.getText();
      final String proxyUserName = txtProxyUserName.getText();
      final String proxyPassword = new String(txtProxyPassword.getPassword());
  
      String sURL = m_mainFrame.getCurrentAdress();
      BufferedReader reader = null;
      try 
      {            
              HttpURLConnection urlCon;                
              URL testURL = new URL(sURL);                
  
              Authenticator authenticator = new Authenticator() {
                  @Override
                  public PasswordAuthentication getPasswordAuthentication() {
                      return (new PasswordAuthentication(proxyUserName, proxyPassword.toCharArray()));
                  }
              };
              Authenticator.setDefault(authenticator);                
  
              //Proxy instance, proxy ip = 10.0.0.1 with port 8080
              Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyURL, Integer.parseInt(proxyPort)));
              urlCon = (HttpURLConnection)testURL.openConnection(proxy);
  
              urlCon.setRequestMethod("GET");
              urlCon.setDoOutput(true);
              urlCon.setReadTimeout(1000 * 300);
              urlCon.setConnectTimeout(1000 * 300);
              urlCon.connect();
  
              //!!! HERE THE EXCEPTION OCCURS (when calling getInputStream):
              reader = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"UTF-8"));
              StringBuilder sb = new StringBuilder();
              char[] cbuf = new char[512];
              while (reader.read(cbuf) &gt; -1)
              {
                  String s = new String(cbuf);
                  sb.append(s);
              }
  
              System.out.println(sb.toString());   
  
              int responseCode = urlCon.getResponseCode();                
              if (responseCode == 200)
                  JOptionPane.showMessageDialog(null, "Proxy OK for URL: " + sURL);
              else
                  JOptionPane.showMessageDialog(null, "Proxy not OK for URL:"+ sURL + "\r\nIt returns Code: " + responseCode);
      }
      catch (Exception ex) 
      {
          ex.printStackTrace();            
      }
  </code></pre>
  
  <p>The code fails with this exception:</p>
  
  <pre><code>java.io.IOException: Error writing to server
  at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)....
  
  Caused by: java.io.IOException: Error writing to server
  at sun.net.www.protocol.http.HttpURLConnection.writeRequests(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(Unknown Source)
  at *.*.webstarter.ui.SettingsDlg.testProxy(SettingsDlg.java:673)
  ... 84 more
  </code></pre>