Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using Java for FTP Put command over Squid HTTP Proxy error

319958Jun 24 2011 — edited Jun 24 2011
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).

So here is my 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
I keep getting this exception:
Java call terminated by uncaught Java exception: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
So I have tried this with an FTPURLConnection etc and have had the same issue.

When debugging this, it appears that there are no less than THREE variables that are called doOutput within the ftpConn.
Only one is editable (whereas the other two are not)

The first one (and it seems the one that is returned via the getDoOutput) returns true after the setting of the ftpConn.setDoOutput(true);.

looking over the source code for HttpURLConnection (http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/net/sun/net/www/protocol/http/HttpURLConnection.java.htm):
 public synchronized OutputStream getOutputStream()
                    throws IOException {

                try {
                    if (!doOutput) {
                        throw new ProtocolException(
                                "cannot write to a URLConnection"
                                        + " if doOutput=false - call setDoOutput(true)");
                    }
Looking over the Proxy handling in FtpURLConnection (http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/net/sun/net/www/protocol/ftp/FtpURLConnection.java.htm) leads me to believe that when it determines that you are using an HTTP Proxy it passes off the the processing to the HttpURLConnection.

This is where I think the disconnect is happening (either for me or the library or the proxy).

Am I missing someway to set the setDoOutput or something else?

I have been able to successfully tunnel out of the proxy for GETs, but PUTs are all FAILING on the doOutput.

Thanks for any help or pointers to get this to work.

P.S. I am using Java 1.5.0_10 (we are here to remain consistent with other applications)

{I've also posted this on StackedOverflow so if that is considered rude or whatnot let me know and I'll remove one of the post)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 22 2011
Added on Jun 24 2011
2 comments
858 views