作者:lifetime8_797 | 来源:互联网 | 2023-01-15 16:26
我正在使用Apache HttpClient尝试将一些帖子数据提交给服务器.不幸的是,我无法访问服务器以获取任何日志信息,因此无法实现.
如果我使用Firefox完成此过程,它可以正常工作.(我在这个特定页面上收到302警告)
我已经匹配了Firefox和我的程序的Request标头.
Firefox请求标头:
Host: server ip
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://server ip/
Content-Type: application/x-www-form-urlencoded
Content-Length: 407
COOKIE: sessiOnId=blahblah
Connection: keep-alive
Upgrade-Insecure-Requests: 1
我的程序请求显示的标题 context.getRequest().getAllHeaders();
Host: server ip
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://server ip/
Content-Type: application/x-www-form-urlencoded
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Content-Length: 406
COOKIE: sessiOnId=blahblah
我已经通过比较EntityUtils.toString(httpPost.getEntity(), "UTF-8");
Firefox工具的输出和内置工具来查看请求主体,以查看请求正文,并且它们几乎匹配字符.(只是会话ID略有不同,因为它没有使用相同的会话.)
我不确定还有什么要检查的.什么可能导致服务器在浏览器和程序之间表现不同?
下面是我的POST请求代码.
HttpPost httpPost = new HttpPost("https://" + getIp() + "");
List params = new ArrayList();
params.add(new BasicNameValuePair("FTPUsername", "blah"));
params.add(new BasicNameValuePair("FTPPassword", "blah"));
params.add(new BasicNameValuePair("FormButtonSubmit", "OK"));
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpPost.setHeader("Host", ip);
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0");
httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Accept-Language", "en-US,en;q=0.5");
httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
httpPost.setHeader("Referer", referer);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("Upgrade-Insecure-Requests", "1");
//Response
HttpResponse respOnse= getHttpClient().execute(httpPost, LoginRequest.context);
int statusCode = response.getStatusLine().getStatusCode();
httpPost.releaseConnection();
我意识到这可能是很多事情,因为500是一个服务器错误,但它必须是我提交的错误或我错过了一些东西,因为它在浏览器中完美运行.
1> user7294900..:
302"警告"实际上是一个重定向.HTTP客户端确实自动重定向,您必须标记RedirectStrategy
,对于HttpClient 4.3:
HttpClient instance = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy()).build();
请参阅答案和w3文档中的示例:
如果收到302状态代码以响应GET或HEAD以外的请求,则用户代理不得自动重定向请求,除非用户可以确认