org.apache.http.ProtocolException: Target host is not specified

简介: 对于httpClient4.3访问指定页面,可以从下面的demo抽取方法使用。 注意:对于URL必须使用 http://开始,否则会有如下报错信息: Caused by: org.apache.

 

对于httpClient4.3访问指定页面,可以从下面的demo抽取方法使用。

注意:对于URL必须使用 http://开始,否则会有如下报错信息:

Caused by: org.apache.http.ProtocolException: Target host is not specified
    at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.Java:69)
    at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
    ... 5 more

demo代码:

public class ClientWithResponseHandler {
public static String url = "http://www.baidu.com";
    public final static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet(url);

            System.out.println("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                public String handleResponse(
                        final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
        } finally {
            httpclient.close();
        }
    }

}

 

相关文章
|
安全 应用服务中间件 Apache
目标URL存在http host头攻击漏洞
目标URL存在http host头攻击漏洞
1263 0
目标URL存在http host头攻击漏洞
|
16天前
|
JSON 缓存 JavaScript
【HTTP】请求“报头”(Host、Content-Length/Content-Type、User-Agent(简称 UA))
【HTTP】请求“报头”(Host、Content-Length/Content-Type、User-Agent(简称 UA))
56 1
|
1月前
|
缓存 安全 应用服务中间件
Web安全-HTTP Host头攻击
Web安全-HTTP Host头攻击
98 7
|
1月前
|
缓存 安全 应用服务中间件
Web安全-HTTP Host头攻击
Web安全-HTTP Host头攻击
95 3
|
2月前
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
|
缓存 安全 网络协议
Web Security 之 HTTP Host header attacks(下)
Web Security 之 HTTP Host header attacks
128 0
|
SQL 缓存 负载均衡
Web Security 之 HTTP Host header attacks(上)
Web Security 之 HTTP Host header attacks
700 0
|
安全 应用服务中间件 Apache
|
分布式计算 Hadoop 程序员
Apache Hadoop 启动报错:masternode:ssh: connect to host master port 22: Connection timed out 总结
Apache Hadoop 启动报错:masternode:ssh: connect to host master port 22: Connection timed out 总结
428 0
Apache Hadoop 启动报错:masternode:ssh: connect to host master port 22: Connection timed out 总结
|
SQL Java 应用服务中间件
ssm管理系统中出现HTTP Status 403-Forbidden .Access to the specified resource has been forbidden
ssm管理系统中出现HTTP Status 403-Forbidden .Access to the specified resource has been forbidden
ssm管理系统中出现HTTP Status 403-Forbidden .Access to the specified resource has been forbidden

推荐镜像

更多