繁体   English   中英

java HttpClient.execute(get)中的Apache HttpComponents什么都不做

[英]Apache HttpComponents in java HttpClient.execute(get) not doing anything

我正在使用Apache HttpComponents创建与网站的http连接。 我已经采取了一些方法来使用post / get获取网站内容,发送cookie,接收cookie并将它们存储在我创建的名为CookieManager的类中。 一切正常,但是当我尝试使用GET方法获取页面内容时,程序将继续运行,但不会执行任何操作。

public HttpResponse sendRequestGet(String url, List<NameValuePair> headers) throws IOException{

    HttpGet get = new HttpGet(url);

    for (NameValuePair header : headers){
        get.setHeader(header.getName(), header.getValue());
    }

    HttpResponse response = client.execute(get);

    System.out.println("----------- STATUS CODE -------------");
    System.out.println(response.getStatusLine().getStatusCode() + ": " + url);
    System.out.println("-------------------------------------");

    return response;

}

该代码是上面显示的代码。 字符串url包含我要访问的url,比如说http://mylink.com/market ,并且headers参数如下所示:

List<NameValuePair> headerList = new ArrayList<NameValuePair>();
    headerList.add((NameValuePair) new BasicNameValuePair("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));
    headerList.add((NameValuePair) new BasicNameValuePair("Accept-Language", "en-US;q=1,en;q=0.8"));
    headerList.add((NameValuePair) new BasicNameValuePair("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"));
    headerList.add((NameValuePair) new BasicNameValuePair("Cookie", getCookies()));
    headerList.add((NameValuePair) new BasicNameValuePair("Referer", "http://mylink.com/profile/"));
    headerList.add((NameValuePair) new BasicNameValuePair("Upgrade-Insecure-Requests", "1"));

如果我使用getCookies()返回一个空字符串来调用该函数,则该函数有效,但是问题是我必须发送cookie中的会话ID。 我尝试调试,发现由于HttpResponse response = client.execute(get);这一行HttpResponse response = client.execute(get); 它什么也没做,程序仍在执行,但是卡在那一行。 另外,我应该提到,我可以让其他页面发送所需的Cookie,但是http://mylink.com/market/却给我带来了这个问题。

我已经使用chrome“网络”标签来查看浏览器和服务器之间的交互,唯一的是我没有包含一些标头(例如Host)。 有人知道我在做什么错吗? 谢谢

我可以通过在client.execute(...)调用之前在函数内部添加此行来修复此问题: client = HttpClientBuilder.create().build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM