gpt4 book ai didi

java-使用apache http客户端时出现未知主机异常

转载 作者:行者123 更新时间:2023-12-03 08:18:45 27 4
gpt4 key购买 nike

我正在尝试对网站发出一个简单的 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.
}

关于java-使用apache http客户端时出现未知主机异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9038941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com