Connecting to a site using a proxy code in java

I want to connect to the site through a proxy in java. This is the code I wrote:

public class ConnectThroughProxy { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy ip", 8080)); public static void main(String[] args) { try { URL url = new URL("http://www.rgagnon.com/javadetails/java-0085.html"); URLConnection connection=url.openConnection(); String encoded = new String(Base64.encode(new String("user_name:pass_word").getBytes())); connection.setDoOutput(true); connection.setRequestProperty("Proxy-Authorization","Basic "+encoded); String page=""; String line; StringBuffer tmp = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line=in.readLine()) != null) { page.concat(line + "\n"); } System.out.println(page); } catch(Exception ex) { ex.printStackTrace(); } } 

When trying to run this code, it produces the following error:

java.lang.IllegalArgumentException: Invalid character in message header value: Basic dXNlcl9uYW1lOnBhc3Nfd29yZA ==
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader (HttpURLConnection.java:323)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty (HttpURLConnection.java:2054)
on test.ConnectThroughProxy.main (ConnectThroughProxy.java:30)

Any idea how to do this?

+4
source share
3 answers

If you are just trying to make HTTP requests through an HTTP proxy, you don’t need to make so much effort. There is an entry here: http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

But basically it comes down to simply setting the properties of the http.proxyHost and http.proxyPort environment, either on the command line or in the code:

  // Set the http proxy to webcache.mydomain.com:8080
 System.setProperty ("http.proxyHost", "webcache.mydomain.com");
 System.setProperty ("http.proxyPort", "8080");

 // Next connection will be through proxy.
 URL url = new URL ("http://java.sun.com/");
 InputStream in = url.openStream ();

 // Now, let 'unset' the proxy.
 System.clearProperty ("http.proxyHost");

 // From now on HTTP connections will be done directly.
+10
source

It seems to me that you are not using your instance of Proxy at all. I think you should pass it when you instantiate the URLConnection:

 URLConnection connection=url.openConnection(proxy); 

Setting the properties of the http.proxy environment is simpler, and when using some third-party libraries without a proxy server, transmission can only support a possible solution, but its disadvantage is that it is configured globally for the whole process.

+4
source

I used the Google Data API, and the only way I got the proxy settings was to provide ALL parameters related to the proxy server, even if they are set as empty:

 /usr/java/jdk1.7.0_04/bin/java -Dhttp.proxyHost=10.128.128.13 -Dhttp.proxyPassword -Dhttp.proxyPort=80 -Dhttp.proxyUserName -Dhttps.proxyHost=10.128.128.13 -Dhttps.proxyPassword -Dhttps.proxyPort=80 -Dhttps.proxyUserName com.stackoverflow.Runner 

If the username and password are NOT required, and the same http and https servers should be the same, as well as the port number (if this is your case). Please note that the same HTTP proxy is also provided as an HTTPS server, as well as its port number (link from https://code.google.com/p/syncnotes2google/issues/detail?id=2#c16 ).

If your Java class has an instance of the "URL" class, it must select these configurations ...

+2
source

Source: https://habr.com/ru/post/1312736/


All Articles