Connect to a site using proxy code in java -


I want to connect in proxy through Java as a site. This is the code that I have written:

  public class ConnectThroughProxy {proxy proxy = new proxy (proxy .type.http, new inset socket address ("proxy IP", 8080)); Public static zero main (string [] args) {try url URL = new URL ("http://www.rgagnon.com/javadetails/java-0085.html"); URL Connection Connection = url.openConnection (); String encoded = new string (base 64.exode (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 = new in BufferedReader (new InputStreamReader (connection.getInputStream ())); While ((line = in. Robil ()) = null) {page.concat (line + "\ n"); } System.out.println (page); } Hold (Exception pre) {ex.printStackTrace (); }}  

This throws the following error while trying to run this code:

java.lang.IllegalArgumentException: Invalid character in the message title value ( S): Basic dXNlcl9uYW1lOnBhc3Nfd29yZA ==
at sun.net .www.protocol.http.HttpURLConnection.checkMessageHeader (HttpURLConnection.java:323)
sun.net.www.protocol.http.HttpURLConnection.setRequestProperty (HttpURLConnection. Java: 2054) test.ConnectThroughProxy.main (ConnectThroughProxy.java30)

How to consider any?

If you are just trying to create an HTTP request through an HTTP proxy server, There should not be any effort to make a lot of effort. Here is a writing capability:

But it basically decreases to set http.proxyHost and http.proxyPort environment properties, either on the command line or in the code:

 // Http Proxy is set to webcache.mydomain.com:8080 System.setProperty ("http.proxyHost", "webcache.mydomain.com"); System.setProperty ("http.proxy port", "8080"); // Next will be via the proxy URL url = new URL ("http://java.sun.com/"); InputStream in = url.openStream (); // Now, let's unset 'proxy system.clearProperty ("http.proxyHost"); // will be done directly on the HTTP connection. 

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -