i have some code to test if the proxy server and port is working ,some of the code like this:
System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "localhost");
System.getProperties().put("https.proxyPort", "1234");
System.getProperties().put("http.proxyHost", "localhost");
System.getProperties().put("http.proxyPort", "1234");
HttpURLConnection conn = (HttpURLConnection) new URL("https://www.google.com").openConnection();
conn.getContent();
conn.disconnect();
it seems that openConnection() method will do thing like this:
try to connect given URL using proxy.
if it fails to use proxy,it will connect URL directly without proxy.
that's the problem,i meant to test if the proxy is working,but this code won't stop if the proxy can not connect.
i also tried to use isReachable() method of InetAddress class,but i get the same result.
so how could i stop this connection if the proxy doesn't work ,in order to test if the proxy is reachable ?
System.getProperties().put("proxySet", "true");
That one doesn't do anything. It is an urban myth. It was part of the defunct 1997 HotJava bean and leaked from there into various books. It has never been part of any JDK. Try setting it to false in some situation where you need it on and see for yourself.
Sorry guys, I found out the way to do it.
I used java.net.Proxy class to open a connection via proxy.
It's easy to use and works fine. See Java Networking and Proxies
Related
I am working on app (android) that is supposed to visit website & retrieve some data from there. I was thinking it would be nice to use tor so I would not leave so much info about myself.
I've completed scraping part and everything works. Problem is, i can not make proxy accessing internet. I wanted to use Orbot as proxy. I always get 405 error and text:
"this is an http connect tunnel, not a full http proxy it appears you have configured your browser to use this tor port as an http proxy this is not correct: this port is configured as connect tunnel, not an http proxy. please configure your client accordingly. you can also use https; then the client should automatically use http connect"
Code:
UserAgent agent = new UserAgent();
agent.setProxyHost("127.0.0.1");
agent.setProxyPort(8118);
agent.visit("http://stackoverflow.com");
I've tried
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8118");
instead of the two middle lines from previous code as well plus few other probably not smart things (: and nothing has helped.
From error text about connect tunnel and proxy I figured problem is somewhere in networking but although I searched about it, I couldn't fix it. Perhaps something in Orbot's settings?
My question is, what am I missing please? :)
I am currently implementing an OpenID authentication based on this example. Now I am developing behind a network proxy, therefore the server cannot connect to google. The java proxy settings seem to not have any effect. I also found this stackoverflow question, but I cannot figure out where to put the code. How can I configure the proxy for my spring boot container?
thanks
Not sure if this is of any use, but I'm just working through a Spring Boot tutorial currently (https://spring.io/guides/gs/integration/) and hit a similar network proxy issue. This was resolved just by providing the JVM arguments
-Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080
Adding just the two provided arguments didn't work for me.
Full list that did it is this:
-Dhttp.proxyHost=somesite.com -Dhttp.proxyPort=4321
-Dhttps.proxyHost=somesite.com -Dhttps.proxyPort=4321 -Dhttps.proxySet=true
-Dhttp.proxySet=true
If you need this to make a call to an external service, then try to set proxy to the Client you are using (RestTemplate, etc), as below:
HttpComponentsClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory();
DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
HttpHost proxy = new HttpHost("proxtserver", port);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
restTemplate.setRequestFactory(requestFactory);
For me, server.use-forwarded-headers=true in application.properties solved the problem.
I could able to solve the problem in two methods
Through JVM args (both http & https)
-Dhttp.proxyHost=your-http-proxy-host -Dhttp.proxyPort=8080
-Dhttps.proxyHost=your-https-proxy-host -Dhttps.proxyPort=8080
Or Programatically
public static void setProxy() {
System.setProperty("http.proxyHost", "your-http-proxy-host");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "your-http-proxy-host");
System.setProperty("https.proxyPort", "8080");
}
How to connect properly using HTTP proxy in java? I can do it with sockProxyHost, but not with http.proxyHost.
If i do with socks, it will wait out for the proxy to connect and then it will create the web request, but if i do it with http it will just create the request and dont connect to the proxy.
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "validproxyip");
System.setProperty("http.proxyPort", "validproxyport");
is my code. but it wont connect to the proxy. Please help!
Check out the proxy documentation at Oracle
Either you have a typo, or you are using https and not http, or you access a "No-proxy" host. If you want to be sure, use the ProxySelector.
Try set it in the vm args
java -DproxyHost=proxy.mydomain.com -DproxyPort=3128 your.Main
Could someone help me or suggest a solution? I want to connect from a computer that has firewall to other where the postgres server run. The problem is that computer (client) has a firewall and I don't have access to configure it, or open ports, ping does not respond. The computer (server) where PostgreSQL has open ports but I cannot connect to it from another because of a firewall. I can only access the computer through proxy.
How I could with Java programming access remotely through proxy to postgres forgetting firewall?
Java has a connection with proxies. But I don't know how to put it together with postgres connection.
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "67.210.82.198" );
System.getProperties().put( "proxyPort", "80" );
URL validateURL = new URL("http://domain.com");
URLConnection urlConnection = validateURL.openConnection();
//how put together ???
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://ipPublica:5432/DataBase","user", "pass");
That can't be done. PostgreSQL connections are not HTTP connections. Yo cannot use an HTTP proxy for PostgreSQL. Maybe a socks proxy will do the work.
Try
System.setProperty("http.proxyHost", "67.210.82.198");
System.setPropery("http.proxyPort", "80");
String url = "jdbc:postgresql://host:port/database";
Properties props = new Properties();
props.setProperty("user","myUsername");
props.setProperty("password","myPassword");
props.setProperty("ssl","true");
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, props);
For more, see java networking & proxies.
This question is quite old. It also has been asked on the PostgreSQL mailing list:
http://archives.postgresql.org/pgsql-jdbc/2010-08/msg00021.php
The answers over there boils down to:
Use SOCKs
If (big IF!) you want to hack the JDBC driver source, you might (big "might"!) be able to make it work over a HTTP-Proxy using SSL.
Sidekick: Someone proposed an ssh tunnel.
You'll need a SOCKS proxy, rather than a HTTP proxy.
Then your code will look something like this:
System.setProperty("socksProxyHost", host);
System.setProperty("socksProxyPort", "1080");
Details:
http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html
I'm trying to get page content from a specific URL:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
LOGGER.info("Response code: "+conn.getResponseCode();
... (rest of the code, not relevant at the moment) ...
I'm not able to get conn.getResponseCode(), I have timeout before it gets printed. Surprisingly, when I put the very same URL in my browser, the page will be loaded correctly. I thought it may have something do to with HTTP/HTTPS, unfortunately, after changing HttpURLConnection to HttpsURLConnection there is no difference.
Maybe it has something to do with JBoss configuration? I use JBoss 7.1.1 Final.
Is this an HTTPS url? If it is, you need to account for that. When using an HttpsURLConnection, you must account for the fact that HTTPS certificates need a truststore to compare against. To ignore this sense of trust (and somewhat ruin the point of using HTTPS), see Whats an easy way to totally ignore ssl with java url connections?