有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用孵化的Java10HttpClient重建HTTP流

我试图通过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(标题键:“location”),我接下来需要抓取并调用它。我的客户机状态几乎为400,但我的http代码几乎没有失败

这是我的密码

       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列表中的标题完全相同


共 (1) 个答案

  1. # 1 楼答案

    我可以用ApacheHttpClient运行它。我说不出孵化出来的HttpClient有什么问题——但肯定有一个原因,那就是它不是Java 9/10交付中完全集成的一部分