![](/img/trans.png)
[英]Unknown host exception, Apache HttpClient, Java, wunderground
[英]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.