3

When i compile and run the below code i am getting unknown host exception thrown.

import java.net.*;
import java.io.*;

public class URLReader {
public static void main(String[] args) throws Exception {

  String result="";
  // Create a URL for the desired page
  URL url = new URL("https://www.google.co.in/?gfe_rd=cr&ei=URzCVNmFKIiBoAPlpoD4CA&gws_rd=ssl#q=jbutton");
  // Read all the text returned by the server
  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  String str;
  while ((str = in.readLine()) != null) {
      // str is one line of text; readLine() strips the newline character(s)
      result += str;
  }
  in.close();             

 System.out.println(result);
 }

}

How do i set the proxy for the above code (Https and HTTP)? I am running the above code in Eclipse Kepler.

TheGaME
  • 443
  • 2
  • 8
  • 21
  • @Steffen Thank you. Probably i made a mistake in assigning proxy at the wrong place (after opening connection). – TheGaME Jan 23 '15 at 10:18

1 Answers1

2

You will face this issue if you are behind a proxy. Whenever you are using browser, are you setting any proxy? If yes, you need to configure for this program as well.

Also, you can try setting proxy in eclipse run configuration:

-DproxySet=true -DproxyHost=<proxy host> -DproxyPort=<port> JavApp
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68