Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java and squid proxy problem

Status
Not open for further replies.
Nov 24, 2004
159
GB
HI

I am trying ti run the following

Properties p=System.getProperties();
p.setProperty( "http.proxyHost", "isln616" );
p.setProperty( "http.proxyPort", "9090" );
URL durl = new URL("InputStream in = durl.openStream();
System.setProperties(p);

The error i get is always
***************************************************
Fatal I/O error: Connection timed out: connect
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net. Source)
at sun.net. Source)
at sun.net. Source)
at sun.net. Source)
at sun.net.ttpClient(Unknown Source)
at sun.net. Sour
ce)
at sun.net.(Unknown Source)
at sun.net. So
urce)
at sun.net.own Source)
at java.net.URL.openStream(Unknown Source)
at BICDownloaderms.main(BICDownloaderms.java:49)
[Loaded java.util.IdentityHashMap$KeySet from shared objects file]
[Loaded java.util.IdentityHashMap$IdentityHashMapIterator from shared objects fi
le]
[Loaded java.util.IdentityHashMap$KeyIterator from shared objects file]
[Loaded java.io.DeleteOnExitHook from shared objects file]

***************************************************************


I can successfully connect to the url using mozille or ie
 
Some thoughts:

- I miss the
Code:
		props.put("http.proxySet", "true");
line

- I don't know if getProperties method returns a copy or a reference, so I'd set the properties before doing the call

- Is the URL actually accesible from browser (no firewall and so on)?

Cheers,
Dian
 
the url is accessible from a browser

I have also tried from a direct internet connection

here is the code


import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.*;
import java.util.Properties;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;

/*
* Created on Mar 2, 2007
* S.W.I.F.T. s.c.r.l.
*/

public class msBICDownloader {

public static void main(String[] args) {
int exitcode = 0;
int statusCode;
String url = " + "action=getfile&productline=bicdir&product=bicdb&content=delta&format=txt";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);

// Provide custom retry handler if required
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));

try {
// We strongly recommend obfuscation of password and restricted access to its storage
Properties prop = new Properties();
InputStream fis = (InputStream)new FileInputStream("c:/kk/bicdownload.prop");
prop.load(fis);

// Set Credentials
UsernamePasswordCredentials credentials;
credentials = new UsernamePasswordCredentials(prop.getProperty("username"),
prop.getProperty("password"));
client.getState().setCredentials(new AuthScope(" 443), credentials);

// Executing the method.
statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
// Handling HTTP error 404 and 500 not covered in this example
// All http error cause in this example exit with status 1.
System.err.println("Method failed: " + method.getStatusLine()+ "\n" +
method.getResponseBodyAsString());
System.out.println(method.getRequestCharSet() + "\n" + method.getRequestHeader("").toString());
exitcode = 1;
}
else {

//Get the file size from the response body and do something with it
Header[] contlen = method.getResponseHeaders("Content-Length");
if (contlen.length != 0) {
StringTokenizer stone = new StringTokenizer(contlen[0].getValue(), "=");
int size = new Integer(stone.nextToken()).intValue();
// Do something with the file size
System.out.println("File size is: " + size);
}

// Get the filename from the response body.
InputStream is = method.getResponseBodyAsStream();
Header[] contdisp = method.getResponseHeaders("Content-Disposition");

String filename = null;
StringTokenizer sttwo = new StringTokenizer(contdisp[0].getValue(), "=");
while (sttwo.hasMoreTokens()) filename = sttwo.nextToken();

// Hardcoded disk and directory path are indicative
FileOutputStream fos = new FileOutputStream("/tmp/" + filename);
byte[] buffer = new byte[4096];

int count = is.read(buffer);
while (count != -1) {
fos.write(buffer, 0, count);
count = is.read(buffer);
}
fos.flush();
fos.close();
is.close();
}
} catch (HttpException e) {
exitcode = 2;
System.err.println("Fatal HTTP Error: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
exitcode = 3;
System.err.println("Fatal I/O error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
System.exit(exitcode);
}
System.out.println("Download done");
}
}
 
I cahnged that still the same problems no route to host, I have tried from a pc with out a proxy or firewall and removed the proxy settings, but it still failes to connect.

I can reach the url with a browser
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top