使用孵化的Java 10 HttpClient

时间:2018-06-08 15:55:09

标签: java http httpclient

我试图通过Java HttpClient应用程序重建Web服务器的会话设置。我选择了incubated HttpClient provided with Java 9 and Java 10

使用Chrome,我从单个请求中捕获了此标头:

General
Request URL: https://<some_url>?user_id=1176&onlyDirectUserItems=true&onlyAssignedToUser=true&show=Unresolved&itemsFilter=0
Request Method: GET
Status Code: 302 Found
Remote Address: <theProxy>:8000
Referrer Policy: no-referrer-when-downgrade

Response Headers
Connection: Keep-Alive
Content-Length: 164
Content-Type: text/html; charset=iso-8859-1
Date: Fri, 08 Jun 2018 14:33:16 GMT
Keep-Alive: timeout=300, max=100
Location: https://<another_url>:443/nesp/app/plogin?agAppNa=app_me_company_ext&c=secure/name/password/uri&target=%22https://<another-usr>/browseIssues.spr?user_id=1176&onlyDirectUserItems=true&onlyAssignedToUser=true&show=Unresolved&itemsFilter=0%22
P3p: CP="NOI"
Server: Apache
Set-Cookie: IPCZQX03224bfb75=030003000000000000000000000000008f7aed69; path=/; domain=.me.de
Via: 1.1 <host> (Access Gateway-ag-7169149846802036-13837511)

Request Headers
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Connection: keep-alive
Cookie: org.ditchnet.jsp.tabs.wiki=wiki-wysiwyg; ZNPCQ003-31393000=6c2f99a3; ZNPCQ003-32323200=cd188fdd
DNT: 1
Host: <host>
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36

Query String Parameters
user_id: 1176
onlyDirectUserItems: true
onlyAssignedToUser: true
show: Unresolved
itemsFilter: 0

可以看到响应标题提供了一个URL(标题键:&#34;位置&#34;),我需要抓取并调用下一个。但是对于我的http客户端,我失败了状态代码400并且几乎没有任何东西

这是我的代码

       url = "https://<some_url>?user_id=1176&onlyDirectUserItems=true&onlyAssignedToUser=true&show=Unresolved&itemsFilter=0";

       HttpClient client = HttpClient.newBuilder()
               .proxy(ProxySelector.of(new InetSocketAddress("<theProxy>", 8000)))
               .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
               .followRedirects(HttpClient.Redirect.SAME_PROTOCOL)
               .build();

       HttpRequest request = HttpRequest.newBuilder()
               .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0")
               .header("Upgrade-Insecure-Requests", "1")
//             .header("Host", "<host>")
               .header("Connection", "keep-alive")
               .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
               .header("Accept-Language", "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7")
               .header("Accept-Encoding", "gzip, deflate, br")
               .uri(new URI(url))
               .build();

       HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
       HttpHeaders headers = response.headers();
       Map<String, List<String>> headerMap= headers.map();
       for (String key : headerMap.keySet()) {
           System.out.println(">"+key+"<");
           for (String value : headerMap.get(key)) {
               System.out.println("   " + value); 
           }
       }
       System.out.println(response.statusCode());
       System.out.println(response.body());

我不知道可能出现什么问题,以及如何继续完成这项工作。我希望有人可以告诉我下一步该做什么。

我也不明白:我必须删除标题&#34; Host&#34; - 因为我收到了回复:&#34;您的浏览器发送了此服务器无法理解的请求。&#34;

与Chrome列表中的标题相同的标题

1 个答案:

答案 0 :(得分:0)

我可以使用Apache HttpClient运行它。我不知道孵化的HttpClient有什么问题 - 但肯定有一个原因,它不是Java 9/10交付的完全集成部分