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
Related
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 trying to make a get request in Java using HttpURLConnection. The response is 200 when I am on my home network but on the company nework i am getting a connect timed out error (SocketTimeoutException) I changed the timeout to be 20 seconds and I am still getting this error after a few seconds. The link is available when I view it in the browser on the same network that I am getting the timeout from the Java code, so I could it still be a firewall issue? Or something else? Here is my code:
String USER_AGENT = "Mozilla/5.0";
URL obj = new URL("http://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setConnectTimeout(20000);
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
This is typically an error produced by a firewall in your company's network. But if your browser is able to get to the internet, then your browser is using some proxy (likely the system proxy). So, you have to parametrize the JVM through the standard Java network properties to make it use the system proxy:
java -Djava.net.useSystemProxies=true ...
Or, alternatively, find out which are the system proxy parameters (host, port, user and password) and pass them to the JVM:
java -Dhttp.proxyHost=... -Dhttp.proxyPort=...
I am trying to connect azure using adal4j library for java.But i have to connect through the proxy.Following is the snippet of code
String url = "https://login.microsoftonline.com/tenant_id/oauth2/authorize";
authContext = new AuthenticationContext(url,false,
service);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyhostname", 443));
authContext.setProxy(proxy);
ClientCredential clientCred = new ClientCredential(XXXX, xxxx);
Future<AuthenticationResult> future = authContext.acquireToken(
clientCred,
null);
authResult = future.get();
Also i have tried with
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyUser", "xxxx");
System.setProperty("http.proxyPassword", "xxxx");
System.setProperty("http.proxyHost", "xxxxxxx");
And all the time i am getting this following error
the error is.....java.net.ConnectException: Connection timed out: connect
java.util.concurrent.ExecutionException: java.net.ConnectException: Connection timed out: connect
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at com.toyota.eap.auth.Test.main(Test.java:76)
Caused by: java.net.ConnectException: Connection timed out: connect
Note: This error is only if we have proxy within the office. From outside the office If I ran thisprogramme there is no issue.
Any Thought on this.
Thanks
There were the existed threads to answer the issue for java.net.ConnectException: Connection timed out in using Adal4j with Proxy. Please review ADAL for Java Proxy and Java proxy issues - Connection Timed Out and How do I make HttpURLConnection use a proxy?.
For more details, you can use the function setConnectTimeout of Class URLConnection (refer to http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html) to resolve it, see the picture and code below:
String url = "<url_link for http or https>";
int timeout = 30*1000; // 30 seconds
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxy_host>", <proxy_port>));
// if need to auth for proxy
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("<user>",
"<password>".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
// open connection using proxy directly for this connection
// if not, setting in the JVM startup argus or using System.setProperty for app global scope
HttpURLConnection conn = new URL(url).openConnection(proxy); // Also HttpsURLConnection
conn.setConnectTimeout(timeout); // set Timeout
Meanwhile, according to my experience,if the proxy IP and port located on-promise network environment, you can be use it on local environment, but failed on Azure. From this perspective, I think you should confirm the proxy is valid on-promise and Azure firstly.
If the connection can connect and take a long time, it is useful to set the connection timeout property as my references above .
is your proxy https? if so, use jvm arguments https. not http:
HTTPS
-Dhttps.proxyHost=proxy-name-without-https.com -Dhttps.proxyPort=proxy-port
ex. -Dhttps.proxyHost=myproxy.com -Dhttps.proxyPort=2021
I am working on creating a Video sitemap for a site that has hosted videos on Brightcove video cloud. In order to get all the video information from the site, Brightcove suggests to read the response from their url of following form
http://api.brightcove.com/services/library?token="+accountToken+"&page_size=1&command=find_all_videos&output=JSON&get_item_count=true
the output of the url is in JSON, where accountToken is just an identifier of the account.
When I hit the above url with Token in the browser, it gives me the correct response.
I wrote below program snippet to read from that url
URL jsonURL = new URL("http://api.brightcove.com/services/library?token="+accountToken+"&page_size=1&command=find_all_videos&output=JSON&get_item_count=true");
HttpURLConnection connection = (HttpURLConnection) jsonURL.openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String lineRead = "";
while (reader.ready()) {
lineRead = lineRead + reader.readLine();
}
As my browser uses proxy, I added below code to include proxy settings
System.setProperty("http.proxyHost", "my.proxyurl.com");
System.setProperty("http.proxyPort", "80");
Without using proxy settings, it returns java.net.ConnectException: Connection refused: connect and with proxy it gives me java.io.IOException: Server returned HTTP response code: 503
So my question is , why is it giving me a 503(Service Unavailable) error ? From the browser its working fine.
Update 1:
It seems like an issue with the Network. I pinged the domain and it said "Request Timed out". Working via HTTP though. Looks like an issue with the Firewall.
I think, it may due to your internet connection, I have tried your code I didn't get any 503(Service Unavailable). Check out with different connection connection(without proxy) and it should work. Or you can try it with slightly different approach:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", "port));
conn = new URL(jsonURL).openConnection(proxy);
If you have SOCKS type proxy, change Proxy's constructor parameter to Proxy.Type.SOCKS.
Minor correction to Jamas code
String host="myproxy.com";
int port=8080;
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
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);