简体   繁体   English

java-使用Apache HTTP客户端时未知的主机异常

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

I am trying to make a simple GET request for a website, but I am getting unknown host exception. 我正在尝试对网站进行简单的GET请求,但是我遇到了未知的主机异常。

Given below is my code-- 以下是我的代码-

     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);

It looks like you have a simple problem here. 看来您这里有一个简单的问题。

The URL for your 'HttpHost' object is malformed. 您的“ HttpHost”对象的URL格式错误。 You need to drop the '/' from "google.com/". 您需要从“ google.com/”中删除“ /”。 It should work after that. 它应该在那之后工作。 I used your code with that single modification & it worked. 我使用了您的代码进行了一次修改,就可以了。

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