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");
Related
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
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.
I am making a broswer type app i want to set the proxy only for this browser
I tried to modify global proxy by using this code but it does not work
System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");
System.getProperties().put("http.proxySet", "true");
So I looked at proxySelector class and I really don't understand how to set the proxy for my brower
I know there is a hidden class in ProxySelector in com.android.settings/.ProxySelector
But I have to manually entered the proxy.
Is there any way so that i can configure proxy only for mybrowser(Just a Webview) ??
Please Help. Thanks in Advance!!!
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");
These dont' work in the JDK, only in the Apache HTTP client.
System.getProperties().put("http.proxySet", "true");
This is an urban myth. It appears in some early Java books but has never done anything in the JDK. It is a relic of the defunct HotJavaBean browser c. 1998.
You should use it like
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyHost", someProxyURl);
System.setProperty("http.proxyPort", someProxyPort);
System.setProperty("http.proxyUser", someProxyUser);
System.setProperty("http.proxyPassword", someProxyPassword);
....
I have a Java webapp, running in Tomcat 6, that loads RSS feeds from remote URLs.
I use Rome to handle the RSS feeds and different formats for me. The connection part looks like like that :
try{
feedSource = new URL(rssObject.getAsset());
}catch(MalformedURLException mue){
logger.error(...);
throw mue;
}
try{
URLConnection connection = feedSource.openConnection();
feed = new SyndFeedInput().build(new XmlReader(connection));
}catch(Exception){handle...}
The code works fine, except at this new client, where they use a proxy.
In order to use the proxy, I set the http.proxyHost and proxyPort system properties :
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);
HTTP GET is made to the proxy alright, but now I get a HTTP 502 error (bad gateway or something similar).
Analysing the HTTP exchange with Wireshark, I noticed that the proxy is requiring authentication. It sends a HTTP 507. Java is somehow trying to authenticate but it uses the wrong username and passwords. It seems to use the host name as the username, as for the password I don't know.
So I tried to implement the Authenticator method of specifying a username+password :
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
logger.info(MessageFormat.format("Generating PasswordAuthentitcation for proxy authentication, using username={0} and password={1}.", username, password));
return new PasswordAuthentication(username, password.toCharArray());
}
});
Now my problem is that it is ignored. The getPasswordAuthentication method is never called. I don't see the logging statement in the log file and using Wireshark I can see that it still uses the host name as the user name.
Why ? It seems that java somehow tries to authenticate by itself without consulting the Authenticator.
The proxy seems to be a MS device that uses NTLM for authentication. Is there some built-in mechanism in java to handle this ? The machine on which the app runs is Win Server 2008 R2.
We did the same here for authenticating on a NTLM based proxy.
The authentication on the proxy is actually a normal HTTP Basic Authentication.
We used the following method:
protected URLConnection newURLConnection(URL pURL) throws IOException {
URLConnection urlConnection = super.newURLConnection(pURL);
String auth = new String(Base64.base64Encode(new String("username:password").getBytes()));
auth = "Basic " + auth;
urlConnection.setRequestProperty("Proxy-Connection","Keep-Alive");
urlConnection.setRequestProperty("Proxy-Authorization",auth);
return urlConnection;
}
That, together with the proxy jvm settings, did the trick.
See http://en.wikipedia.org/wiki/Basic_access_authentication.
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);