,
Share with your friends 

Reading Website Content

8 ratings Views 65 
Author: Rambabu_67f2a531 (Rambabu Thota)  View Profile |  View other solutions by this author

Question / Problem


The following error occurs while reading a website content through java code. java.net.UnknownHostException: <website name> at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.<init>(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at websiteReader.URLUtils.dump(URLUtils.java:89) at websiteReader.URLUtils.main(URLUtils.java:9)

Solution

This is because your proxy server requires the authentication. You can use the following code and can check with your administrator for any detailed information regarding the proxy settings.

try
      {
      System.setProperty("java.net.useSystemProxies", "true");
      List l = null;
      try {
        l = ProxySelector.getDefault().select(new URI("http://www.yahoo.com"));
      }
      catch (URISyntaxException e) {
        e.printStackTrace();
      }

      if (l != null) {
         for (Iterator iter = l.iterator(); iter.hasNext();) {
            java.net.Proxy proxy = (java.net.Proxy) iter.next();
            System.out.println("proxy hostname : " + proxy.type());
            InetSocketAddress addr = (InetSocketAddress) proxy.address();
            if (addr == null) {
              System.out.println("No Proxy");
            }
            else {
              System.out.println("proxy hostname : " + addr.getHostName());
              System.out.println("proxy port : " + addr.getPort());
           /*   URL url = new URL("http://internal.server.local/");
              URLConnection conn = url.openConnection(Proxy.NO_PROXY);*/
             
              System.getProperties().put("http.proxyHost", "ipaddress");
              System.getProperties().put("http.proxyPort", addr.getPort());
              System.getProperties().put("http.proxyUser", "username");
              System.getProperties().put("http.proxyPassword", "password");
             
             
             
              URL yahoo = new URL("http://www.yahoo.com/");
              BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                          yahoo.openStream()));

              String inputLine;

              while ((inputLine = in.readLine()) != null)
                  System.out.println(inputLine);

              in.close();

            }
         }
      }
     
      }
      catch(Exception e) { e.printStackTrace(); }

Applies to

Core Java

Rank It

Login to rank it

Report


Advertisement