zoukankan      html  css  js  c++  java
  • org.apache.http.ProtocolException: Target host is not specified

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

    }

  • 相关阅读:
    POJ 3273 :Monthly Expense【二分+贪心】
    POJ 3104 Drying【二分】
    Codeforces Round #402 (Div. 2)
    PAT 天梯赛真题集
    PAT 乙级 (将剩下的做了)
    CCPC 2016-2017, Finals (慢慢做,慢慢更新)
    Spring注解开发
    SpringMVC+SSM框架
    Spring5 Framework(IOC+AOP+整合Mybatis事务)
    IDEA 使用小技巧
  • 原文地址:https://www.cnblogs.com/softidea/p/5981769.html
Copyright © 2011-2022 走看看