title: Using Java for FTP Put command over Squid HTTP Proxy error
body: <p>I am a bit green with Java but I have ran into an issue when trying to UPLOAD a local file to a remote FTP server through a SQUID HTTP proxy (please note this obviously ties my hands so that 'FTP' and 'SOCKS' proxy types are not in scope).  </p>
  
  <p>So here is my code:</p>
  
  <pre><code>                       URL url = new URL(urlString);
                         OutputStream os ;                             
  
                         System.setProperty("proxySet", "true");
                         System.setProperty("proxyHost", proxyHost.trim());
                         System.setProperty("proxyPort", proxyPort.stringValue());
  
  
                         URLConnection ftpConn = url.openConnection() ;
                         ftpConn.setAllowUserInteraction(true); //threw this in to see if it would work, does nothing
                         ftpConn.setDoOutput(true); //here is where I set the setDoOutput
                         os = ftpConn.getOutputStream(); //here is where the exception occurs
  </code></pre>
  
  <p>I keep getting this exception:</p>
  
  <blockquote>
    <p>Java call terminated by uncaught Java
    exception: java.net.ProtocolException:
    cannot write to a URLConnection if
    doOutput=false - call
    setDoOutput(true)</p>
  </blockquote>
  
  <p>So I have tried this with an <em>FTPURLConnection</em> etc and have had the same issue.</p>
  
  <p>When debugging this, it appears that there are no less than THREE variables that are called <em>doOutput</em> within the ftpConn.<br>
  Only one is editable (whereas the other two are not)</p>
  
  <p>The first one (and it seems the one that is returned via the <em>getDoOutput</em>) returns <em>true</em> after the setting of the <em>ftpConn.setDoOutput(true);</em>.</p>
  
  <p>looking over the source code for <a href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/net/sun/net/www/protocol/http/HttpURLConnection.java.htm" rel="nofollow">HttpURLConnection:</a></p>
  
  <pre><code> public synchronized OutputStream getOutputStream()
                      throws IOException {
  
                  try {
                      if (!doOutput) {
                          throw new ProtocolException(
                                  "cannot write to a URLConnection"
                                          + " if doOutput=false - call setDoOutput(true)");
                      }
  </code></pre>
  
  <p>Looking over the Proxy handling in <a href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/net/sun/net/www/protocol/ftp/FtpURLConnection.java.htm" rel="nofollow">FtpURLConnection</a> leads me to believe that when it determines that you are using an HTTP Proxy it passes off the the processing to the HttpURLConnection.  </p>
  
  <p>This is where I think the disconnect is happening (either for me or the library or the proxy).</p>
  
  <p>Am I missing someway to set the <em>setDoOutput</em> or something else?</p>
  
  <p>I have been able to successfully tunnel out of the proxy for GETs, but PUTs are all FAILING on the doOutput.</p>
  
  <p>Thanks for any help or pointers to get this to work.</p>
  
  <p>P.S.
  I am using Java 1.5.0_10 (we are here to remain consistent with other applications)</p>