对于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();
        }
    }

}

org.apache.http.ProtocolException: Target host is not specified的更多相关文章

  1. Target host is not specified错误

    对于httpClient4.3访问指定页面,可以从下面的demo抽取方法使用. 注意:对于URL必须使用 http://开始,否则会有如下报错信息: 或者在设置cookie时带上domain: coo ...

  2. org.apache.flume.FlumeException: NettyAvroRpcClient { host: xxx.xxx.xxx.xxx, port: 41100 }: RPC

    2014-12-19 01:05:42,141 (lifecycleSupervisor-1-1) [WARN - org.apache.flume.sink.AbstractRpcSink.star ...

  3. ORA-12545:Connect failed beacuse target host or object does not exist

    更换计算机名,重新启动系统后 oracle 的监听器就无法正常启动, 总是提示ORA-12545:Connect failed beacuse target host or object does n ...

  4. ORA-12545: Connect failed because target host or object does not exist

    [oracle@ybdbtest oradata]$ sqlplus /nolog SQL*Plus: Release 9.2.0.4.0 - Production on Thu Jun 26 10: ...

  5. Android开发中与服务器交互时,遇到java.io.IOException: Target host must not be null的问题

    当我遇到这个问题的时候,也在网上查找好半天.找到了一个和这个问题很类似的问题——java.lang.IllegalStateException: Target host must not be nul ...

  6. org.apache.flume.EventDeliveryException: NettyAvroRpcClient { host: hadoop1, port: 41414 }: Failed to send event

    org.apache.flume.EventDeliveryException: NettyAvroRpcClient { host: hadoop1, port: 41414 }: Failed t ...

  7. TNS-12545: Connect failed because target host or object does not exist

    问题描述 $ lsnrctl startLSNRCTL for Linux: Version 12.1.0.2.0 - Production on 26-JUL-2017 09:53:42Copyri ...

  8. 为Apache配置虚拟机Virtual Host

    配置虚拟机最少需要修改两个文件,即httpd-vhosts.conf 和 hosts (记住修改任何配置前先备份哦,这才是个好习惯) 第一步,修改httpd-vhosts.conf文件, 文件路径 \ ...

  9. java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=Aict/listPagedAict.action

    原因:请求的URL地址不完整,没有找到host. 排查解决:发现HTTP请求的URL少加了项目名,导致URL地址不完整.

随机推荐

  1. ZigBee协议学习之网络层

    ZigBee的体系结构中,底层采用IEEE 802.15.4的物理层和媒介层,再次基础上,ZigBee联盟建立了自己的网络层(NWL)和应用层框架. ZigBee网络层的主要功能包括设备的连接和断开. ...

  2. python练习 根据日志中的ip和url排序

    #!/usr/bin/env python #coding:utf-8 def open_file(file_name): res={} with open(file_name) as f: for ...

  3. L002-oldboy-mysql-dba-lesson02

            L002-oldboy-mysql-dba-lesson02 [root@web01 ~]# mysql -uroot -ptestpassword mysql> use mys ...

  4. 《APUE》第6章笔记

    这一章主要介绍了口令文件和组文件的结构和一些围绕这些结构的函数. 口令文件即passwd就是在/etc/passwd中可以查阅.其结构是: 上图四个平台能支持的就用黑点表示. 因为加密口令这一项放在p ...

  5. response返回随笔

    response.setHeader("Content-type", "text/html;charset=UTF-8");//这句话的意思,是让浏览器用utf ...

  6. 如何用命令的方式查看你的Office2010密钥是否是永久的有效

    首先,ctrl+R ,  然后输入cmd,  回车, 进入黑框框 其次,在你的office安装位置下找到这个文件OSPP.VSB,对其右键,查看其属性,复制下它的位置.,接着  就照着下图上的操作吧~ ...

  7. 转 Linux命令及Linux终端的20个趣事

    https://linux.cn/article-2831-1.html 1. 命令:sl (蒸汽机车) 你可能了解 ‘ls’ 命令,并经常使用它来查看文件夹的内容.但是,有些时候你可能会拼写成 ‘s ...

  8. Dapper full example

    Skip to content Sign up Sign in This repository Explore Features Enterprise Blog Watch Star , Fork S ...

  9. 域名转化到IP地址的实现

    在linux中,有一些函数可以实现主机名和地址的转化,最常见的有gethostbyname().gethostbyaddr()等,它们都可以实现IPv4和IPv6的地址和主机名之间的转化.其中geth ...

  10. Head First设计模式悟道

    暂时包括 策略模式,观察者,装饰模式,工厂模式,抽象工厂模式,后续会继续补充中,纯属个人总结用,不喜勿喷, 源代码见: 传送门 public class NYPizzaIngredientFactor ...