首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用孵化的Java10 HttpClient重建HTTP流

用孵化的Java10 HttpClient重建HTTP流
EN

Stack Overflow用户
提问于 2018-06-08 23:55:09
回答 1查看 187关注 0票数 0

我正在尝试通过一个Java应用程序重建到HttpClient服务器的会话设置。我选择了incubated HttpClient provided with Java 9 and Java 10

使用Chrome时,我从一个请求中捕获了这个头文件:

代码语言:javascript
复制
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 ( Header -key:"location"),接下来我需要获取并调用它。但是对于我的http客户端,我失败了,状态码为400,几乎什么也得不到。

这是我的代码

代码语言:javascript
复制
       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());

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

我也不明白的是:我必须删除头文件"Host“--因为我得到的响应是:”您的浏览器发送了一个此服务器无法理解的请求。“

与Chrome-listing中的标题完全相同

EN

回答 1

Stack Overflow用户

发布于 2018-06-11 01:09:51

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50764345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档