【问题标题】:Rebuild HTTP flow with incubated Java 10 HttpClient使用孵化的 Java 10 HttpClient 重建 HTTP 流
【发布时间】:2018-06-08 15:55:09
【问题描述】:

我正在尝试通过 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(标头键:“位置”),接下来我需要抓取并调用它。但是对于我的 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());

我不知道可能出了什么问题以及如何继续完成这项工作。我希望有人能告诉我接下来要玩什么。

我也不明白的地方:我不得不删除标题“主机” - 因为我得到了响应:“您的浏览器发送了一个此服务器无法理解的请求。”

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

【问题讨论】:

  • @Michael 它只是孵化的。 Java 10:jdk.incubator.http.HttpClient
  • @RoddyoftheFrozenPeas 我正在确认 Michael 的 “我认为 HttpClient 只是孵化”,我说“它是”。先前的评论已编辑以澄清。
  • @Andreas 谢谢。我已经编辑了问题。
  • 现在已经设置 Fiddler 来捕获请求。对比结果发现httpclient没有提供cookie给服务端

标签: java http httpclient


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2018-05-08
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多