访问yahoo Web服务会抛出UnknownHostException

时间:2013-12-27 09:16:06

标签: java

我正在尝试解析Yahoo!使用XPath的Web Service XML结果。

我正在使用的代码是

    String request = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=umbrella&results=10";
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(request);        
    // Send GET request
    int statusCode = client.executeMethod(method);        
    if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
    }
    InputStream rstream = null;        
    // Get the response body
    rstream = method.getResponseBodyAsStream();     
    // Process response
    Document response = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(rstream);        
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath=factory.newXPath();        
    //Get all search Result nodes
    NodeList nodes = (NodeList)xPath.evaluate("/ResultSet/Result", response, XPathConstants.NODESET);
    int nodeCount = nodes.getLength();

我得到这样的例外

Exception in thread "main" java.net.UnknownHostException: api.search.yahoo.com
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at                            org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultPro   tocolSocketFactory.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.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:323)
    at org.tcs.com.newsfeed.YahooWebServiceParseResults.main(YahooWebServiceParseResults.java:38)

1 个答案:

答案 0 :(得分:-1)

此异常表示java客户端无法解析DNS名称api.search.yahoo.com。

javadoc:“抛出,表示无法确定主机的IP地址。”

无论出于何种原因,您的程序无法解析DNS名称。当您尝试从命令行ping此主机名时会发生什么?