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

  • 相关阅读:
    GNU GRUB 2:系统引导器的领导者
    Javascript 返回上一页并刷新
    返回代表指定日期的指定日期部分的整数。返回当前周
    请问如何获取字符串在数组中的位置
    SQL终极优化(包括很多非索引方面的优化和原理)
    td onmouseover="this.style.cursor='hand'" onmouseout="this.style.cursor='normal'" >小手状
    树形菜单
    分页 的首页 rs.absoluteposition=rs.absoluteposition+2+((abs(wzpage)1)*wzrep) 要加2 ??
    JS只弹出一个居中弹出窗口
    一个真实的项目经历,很多东西大家可以借鉴下
  • 原文地址:https://www.cnblogs.com/timssd/p/5811461.html
Copyright © 2011-2022 走看看