简体   繁体   中英

getting error Unknow Host : www.google.com

i want to parse the webpage, iam using htmlunit, when iam running the code, iam getting below error.

import java.net.URL;
import java.util.List;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class scrapImage {

      public static void main(String[] args) throws Exception      { 
          URL url = new URL("http://www.google.com");
          //WebClient webClient = new WebClient(Opera);     
          WebClient webClient = new WebClient();    
          HtmlPage currentPage = (HtmlPage) webClient.getPage(url);    
          //get list of all divs      
          final List<?> images = currentPage.getByXPath("//img");      
          for (Object imageObject : images) {           
              HtmlImage image = (HtmlImage) imageObject;            
              System.out.println(image.getSrcAttribute());       
              }         //webClient.closeAllWindows();           } } 
          }
      }

Error Message:

Exception in thread "main" java.net.UnknownHostException: www.google.com
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:196)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
    at java.net.Socket.connect(Socket.java:530)
    at java.net.Socket.connect(Socket.java:480)
    at java.net.Socket.<init>(Socket.java:377)
    at java.net.Socket.<init>(Socket.java:251)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:346)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:97)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1430)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1388)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:325)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:386)
    at htmlunit.scrapImage.main(scrapImage.java:16)

Can anyone let me know the solution for above exception.

I think its problem with your.net connection or your firewall may block the java program to access inte.net.

I think you are behind the proxy or firewall. Check your current firewall status in your system.Also, if it is related to proxy, you can modify the code like this.

System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "your proxy host name" );
System.getProperties().put( "proxyPort", "85" );

May be this will help you.

Seems there is some trouble with the Inte.net connection or you are behind a proxy,

set the proxy settings (Host/Port/Username/Password) if this is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM