java - 从 HttpClient 获取 500 错误,在浏览器中工作

标签 java apache httpclient http-redirect

我正在使用 Apache HttpClient 尝试向服务器提交一些发布数据。不幸的是,我无权访问服务器以获取任何日志信息,因此这是不可能的。

如果我用 Firefox 完成这个过程,它工作正常。 (我确实在这个特定页面上收到了 302 警告)

我已经匹配了 Firefox 和我的程序的请求 header 。

Firefox 请求 header :

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

我的程序请求 header 显示在 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 工具的内置工具来匹配请求的主体在请求正文中,它们几乎一个字符一个字符地匹配。 ( session ID 略有不同,这是预期的,因为它没有使用相同的 session 。)

我不确定还要检查什么。什么可能导致服务器在浏览器和程序之间表现不同?

下面是我的 POST 请求代码。

HttpPost httpPost = new HttpPost("https://" + getIp() + "");

List<NameValuePair> params = new ArrayList<NameValuePair>();
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 是服务器错误,但这一定是我提交的错误或者我遗漏了什么,因为它在浏览器中运行得很好。

最佳答案

302“警告”实际上是重定向。 HTTP 客户端确实会自动重定向,您必须标记 RedirectStrategy,对于 HttpClient 4.3:

HttpClient instance = HttpClientBuilder.create()
                     .setRedirectStrategy(new LaxRedirectStrategy()).build();

参见 examples in answerw3 docs :

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user

关于java - 从 HttpClient 获取 500 错误,在浏览器中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552164/

相关文章:

java - 使用 HttpClient 4.0.1 使用 x509 证书进行相互身份验证

java - java中数组列表的配对值

java.lang.IllegalStateException : Fragment

java - 不幸的 Java 异常 : java. lang.NoSuchMethodError

java - 为什么这段简单的代码会输出损坏的类文件?

apache - 如何停止在 tuleap 9.1 上使用 ssl?

Apache Main DocumentRoot 不存在

php - 在没有Apache的情况下将php作为服务器运行

java - 销毁或关闭 HttpClient Android

java - 签名 Applet 和服务器端 Controller 之间的通信