Proxy problem - ((HttpURLConnection)con).usingProxy() always return false...

1,753 views
Skip to first unread message

kévin defives

unread,
Mar 16, 2012, 12:22:42 PM3/16/12
to RestFB
Hello everybody,

This is my code :

protected HttpURLConnection openConnection(URL url) throws IOException
{
Properties systemProperties = System.getProperties();

systemProperties.setProperty("http.proxyHost","gateway.zscaler.net");
systemProperties.setProperty("http.proxyPort","80");
systemProperties.setProperty("proxySet", "true");

URLConnection con = url.openConnection();

System.out.println(((HttpURLConnection)con).usingProxy());

return (HttpURLConnection)con;
//return (HttpURLConnection) url.openConnection(proxy);
//return (HttpURLConnection) url.openConnection();
}

As you can see, I've modified the function openConnection(URL url) for
that it works with my proxy.

But it don't works, could you help me ?

I don't understand why
System.out.println(((HttpURLConnection)con).usingProxy()); always
display on the screen "FALSE". O_o

Could you help me ?

PS : I've tried with Proxy = new... ; url.openConnection(proxy) but it
was the same error.

Regards,

kévin defives

unread,
Mar 16, 2012, 12:47:28 PM3/16/12
to RestFB
This is the error message :

Exception in thread "main"
com.restfb.exception.FacebookNetworkException: A network error
occurred while trying to communicate with Facebook: Facebook request
failed (HTTP status code null).
The problem is on httpURLConnection connect().

Kumar Pallav

unread,
Mar 16, 2012, 1:12:34 PM3/16/12
to res...@googlegroups.com
Please write the method which is calling openConnection ,you are passing url as parameter , is that initialized before being passed . or in the method you are calling it is null and never initialized .
--
Regards

Kumar Pallav
Analyst Programmer, Wipro Infotech
Mob: 91-7736637166

Marcel Stör

unread,
Mar 16, 2012, 3:04:48 PM3/16/12
to res...@googlegroups.com
I'm pretty sure you need to set the proxy properties before the URL
instance is created. A few examples:
http://www.rgagnon.com/javadetails/java-0085.html

Cheers,
Marcel


--
Marcel St�r, http://www.frightanic.com
Couchsurfing: http://www.couchsurfing.com/people/marcelstoer
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

kévin defives

unread,
Mar 18, 2012, 5:22:58 AM3/18/12
to res...@googlegroups.com
Okay thanks. I'll try it tomorrow :)

kévin defives

unread,
Mar 19, 2012, 5:31:14 AM3/19/12
to RestFB
It's right cause it works !

Thx very much. This the code pour connection with proxy :

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

protected HttpURLConnection openConnection(URL url) throws IOException
{
Proxy proxy = new Proxy(Proxy.Type.HTTP, new
InetSocketAddress("URL_OF_YOUR_PROXY", PORT_OF_YOUR_PROXY));
URL my_url = new URL(url.toString());
return (HttpURLConnection) my_url.openConnection(proxy);
}

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

Anyway, if your proxy use an SSL certification, read this :
http://www.developpez.net/forums/d922760/java/general-java/apis/securite/sun-security-provider-certpath-suncertpathbuilderexception-unable-to-find-valid-certification-path-to-request/
(in french, sorry). For test you could add in your public static void
main this code (not available for production) :

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

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {

@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}

@Override
public void checkServerTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {
// TODO Auto-generated method stub

}

@Override
public void checkClientTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {
// TODO Auto-generated method stub

}
}
};

// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new
java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

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

Regards,
> Marcel St r,http://www.frightanic.com
Reply all
Reply to author
Forward
0 new messages