简体   繁体   中英

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

I'm using Apache HttpComponents to create an http connection with a website. I have made some methods to get website content using post/get, to send cookies, recieve them and store them in a class I created called CookieManager. Everything works fine, but when I try to get a page content using the GET method the program keeps running but it doesn't do anything.

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;

}

The code is the one shown above. The string url contains the url which I want to access, lets say http://mylink.com/market and the headers parameters is made like this:

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"));

If I call the function with the getCookies() returning an empty string, it works , but the problem is that I have to send my session Id which is in the cookies. I tried debugging and I found that since the line HttpResponse response = client.execute(get); it doesn't do anything, the program is still executing but it gets stuck in that line. Also, I should mention that I can get other pages sending the needed cookies but http://mylink.com/market/ gives me this problem.

I have already used chrome 'network' tab to see the interaction between the browser and the server, the only thing is that I don't include some headers (like Host). Does someone know what I'm doing wrong? Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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