how to use proxy for HTTP connection on android? - java

The usual java way doesnt seem to work - i put in bogus values in there and it still "works", so it seems that android doesnt read those properties.
I also put this info into Settings section of OS (via GUI).
ANy ideas? Thx.
Properties props = System.getProperties();
props.put("http.proxyHost", "190.128.1.69");
props.put("http.proxyPort", "80");

Two ways to do it.
System.setProperty("http.proxyHost", <your proxy host name>);
System.setProperty("http.proxyPort", <your proxy port>);
or
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost httpproxy = new HttpHost("<your proxy host>",<your proxy port>);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpproxy);

Related

HttpURLConnection to HTTPs with proxy

I want to connect to an https:// URL in Java that requires proxy.
I have 2 proxies in the system:
HTTP -> proxy.teatre.guerrilla:8080
HTTPS -> proxy.teatre.guerrilla:8443
I've tried with
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.teatre.guerrilla", 8080));
URL url = new URL ( urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
But I got a Exception in thread "main" java.net.ConnectException: Connection timed out: connect
and I haven't seen the type Proxy.Type.HTTPS
I also tried
System.setProperty("http.proxyHost", "proxy.teatre.guerrilla");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "proxy.teatre.guerrilla");
System.setProperty("https.proxyPort", "8443");
with the same result.
I also tried to add this as Program arguments and VM arguments....
-Dhttp.proxyHost=proxy.teatre.guerrilla -Dhttp.proxyPort=8800 -Dhttps.proxyHost=proxy.teatre.guerrilla -Dhttps.proxyPort=8443
please try
System.setProperty("http.proxyHost", "proxy.teatre.guerrilla");
System.setProperty("http.proxyPort", 8080);
You better not include proxy handling in your code. You never know in which environment your JAVA application will be running. So please configure the PROXY settings with JVM args like that: How do I set the proxy to be used by the JVM

Get Proxy Automatically

I have the following HttpConnection call to get and parse json object.
As you see, I am passing the proxy inside the code as follows.
However, I need to know how could I able to get proxy without passing it manually.
proxy= new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyString, 80));
HttpURLConnection dataURLConnection = endURL.openConnection(proxy);
dataURLConnection .setRequestProperty ("Authorization", token);
dataURLConnection .setRequestMethod("GET");
InputStream response = dataURLConnection .getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(response));
I believe you are looking for the java.net.ProxySelector class, since 1.5.
Here's an example of it's functionality;
URI targetURI = new URI("http://stackoverflow.com/");
ProxySelector proxySelector = ProxySelector.getDefault();
List<Proxy> proxies = proxySelector.select(targetURI);
for(Proxy proxy : proxies) {
Proxy.Type proxyType = proxy.type(); //Will return a Proxy.Type (SOCKS, for example)
SocketAddress address = proxy.address(); //Returns null if no proxy is available
}
Edit: Just realized you're already using the Proxy class, so you can just use one of the resulting proxies directly in the HttpURLConnection, of course.
You could set proxy host and port on jvm system property, that way you don't have to create and pass the proxy while creating a new connection.
System.setProperty("https.proxyHost", "myproxy.com");
System.setProperty("https.proxyPort", "8080");
System.setProperty("http.proxyHost", "myproxy.com");
System.setProperty("http.proxyPort", "8080");
But, keep in mind, this will affect all new connections across the JVM once the proxy is set. If you were using Spring or some other framework, they may offer you option to contextually set the proxy.

Java dispatch soapmessage and proxy

I create a Java client for a webservice using this link but I need to connect to a Proxy (with user and password) before call the webservice.
At other times, I created a Proxy and open a httpconnection directly but now, I donĀ“t know how put a Proxy with a Dispatch< SOAPMessage > that calls to "invoke(soapMsg)" method.
Any idea?
Regards.
I found the problem. With Dispatch only is necessary to specify:
System.setProperty("http.proxySet", "true");
System.setProperty("https.proxySet", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
System.setProperty("http.proxyUser", proxyUser);
System.setProperty("http.proxyPassword", proxyPassword);
Also is necessary to specify the "Endpoint address property" that is the WS address.
dispatcher.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://123.145.67.89:8080/name");

Correct way to configure a WebSphere server for an outcoming SSL call

In the company where I work I'm developing a web app on a WebSphere 6.1 server. The web application I'm writing has to connect to an external company by using an SSL connection with mutual authentication.
First thing to say: I'm kind of a noob with such things so sorry if I'll say something stupid :)
I have both public and private certificate. I've added the private certificate to the NodeDefaultKeyStore and the public certificate chain to the NodeDefaultTrustStore. Then I've seen that the server has an SSL configuration that encapsulates both KS and TS, and this configuration is linked to the node I'm running my application on.
As a client library, I'm using HttpClient 4.2.3. I created the HttpClient like this
Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
// e) SETUP SSL
SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSystemSocketFactory();
Scheme httpsScheme = new Scheme("https", HTTPS_PORT, sslSocketFactory);
Scheme httpScheme = new Scheme("http", HTTP_PORT, PlainSocketFactory.getSocketFactory());
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(httpScheme);
schemeRegistry.register(httpsScheme);
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(schemeRegistry);
// f) CREAZIONE CLIENT HTTP
HttpClient httpClient = new DefaultHttpClient(sslSocketFactory);
// g) CREAZIONE DEL PROXY (possibile che venga disattivato)
Resources res = new Resources();
String proxyHost = res.get(PROXY_HOST);
int proxyPort = Integer.parseInt(res.get(PROXY_PORT));
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
System.setProperty("java.net.useSystemProxies", "false");
// ######################## ==> CHIAMATA AD INPS
HttpResponse resp = httpClient.execute(httpPost);
I've seen the SSLSocketFactory and doesn't contain the certificates I specified. I've seen that the SSLSOcketFactory.getSystemSocketFactory() just reads the javax.ssl.xxxxx properties to initialize the KS and TS to be used for the SSL connection.
So... I have to link the server configuration to my application, but I'm not sure about how to do it in a "proper" way: I could set at runtime such properties with the System.setProperty, but I think it's not a good way to do this kind of work. Is there any way to refer the SSL config (maybe via JNDI) from the application? Or the best way is to configure two URL linking to the KS and TS files and configure the SSLSocketFactory manually?
Thanks in advance for the reply!
Lorenzo
Since you've added the certificates to the NodeDefault stores, I don't think you need to do any manual SSL setup in your code at all. The only additional thing you might need to do is add your destination host to SSL certificate and key management > Dynamic outbound endpoint SSL configurations and select the client certificate alias you want to use for that destination.

Using Java, Need to establish an https connection via proxy

I need to establish and send/read over/from an https connection (to a website of course) but through an http proxy or SOCKS proxy. A few other requirements
supports blocking (I can't use non-blocking/nio)
isn't set as an environment or some other global scope property (there are multiple threads accessing)
I was looking into HttpCore components but I did not see any support for blocking https.
Look at the java.net.Proxy class. That does what you need. You create one, and then pass it to the URLConnection to create the connection.
To support per-thread proxy, your best bet is Apache HttpClient 4 (Http Components Client). Get the source code,
http://hc.apache.org/downloads.cgi
It comes with examples for both HTTP proxy and SOCKS proxy,
ClientExecuteProxy.java
ClientExecuteSOCKS.java
Did you look at Apache HTTP Client? Haven't used it in ages but I did use it to pick a proxy server dynamically. Example from site here:
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setProxy("myproxyhost", 8080);
httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost",
new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password"));
GetMethod httpget = new GetMethod("https://www.verisign.com/");
try {
httpclient.executeMethod(httpget);
System.out.println(httpget.getStatusLine());
} finally {
httpget.releaseConnection();
}
System.setProperty("http.proxyHost", "proxy.com");
System.setPropery("http.proxyPort", "8080");
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();
http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

Categories