zoukankan      html  css  js  c++  java
  • Target host is not specified错误

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

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

    或者在设置cookie时带上domain: cookie.setDomain(domain);

    或者:cookie.setDomain("0.0.0.0");

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

  • 相关阅读:
    [自娱自乐] 4、超声波测距模块DIY笔记(四)——终结篇·基于C#上位机软件开发
    [新概念51单片机C语言教程·郭天祥] 1、 基础知识必备
    [起重机监测系统] 2、国标、架构及待测数据方案
    [自娱自乐] 3、超声波测距模块DIY笔记(三)
    [起重机监测系统] 1、基于无线传输的桥式起重机的安全监测方案
    [OpenCV] 3、直线提取 houghlines
    关于MySQL的行转列的简单应用
    java 模拟发送post请求测试
    拦截器通过Spring获取工厂类,注入bean对象
    springboot 表单校验
  • 原文地址:https://www.cnblogs.com/timssd/p/5811461.html
Copyright © 2011-2022 走看看