簡體   English   中英

java-使用Apache HTTP客戶端時未知的主機異常

[英]java- unknown host exception when using apache http client

我正在嘗試對網站進行簡單的GET請求,但是我遇到了未知的主機異常。

以下是我的代碼-

     DefaultHttpClient client = new DefaultHttpClient();
     HttpHost targetHost=null;
     targetHost= new HttpHost("google.com/", 80, "http");
     HttpGet httpget = new HttpGet("about-us.html");
     BasicHttpContext localcontext = new BasicHttpContext();
     try {
        HttpResponse response = client.execute(targetHost, httpget, localcontext);

看來您這里有一個簡單的問題。

您的“ HttpHost”對象的URL格式錯誤。 您需要從“ google.com/”中刪除“ /”。 它應該在那之后工作。 我使用了您的代碼進行了一次修改,就可以了。

DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("google.com", 80, "http"); 
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localContext = new BasicHttpContext();
HttpResponse response = null;

try { response = client.execute(targetHost, httpget, localContext); 
      System.out.println(response.getStatusLine()
}
catch(Exception e){
    // Enter error-handling code here.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM