HttpURLConnection 和浏览器连接之间的差异

发布于 2024-09-26 09:24:37 字数 569 浏览 0 评论 0原文

我正在尝试通过 HttpURLConnection 连接到网页,但它的响应与常规浏览器(firefox、chrome)不同。我收到错误 500。

使用相同的代码我可以获得“任何其他”页面(例如 Google)。我的代码发布在下面,但我很确定它没问题。

使用“Live HTTP Headers”firefox 插件我发送了相同的请求并收到了有效的响应。

//...
String urlConsulta = "myURL";
URL url = new URL(urlConsulta);
HttpURLConnection uConsulta = (HttpURLConnection)consulta.openConnection();
uConsulta.setDoOutput(true);
uConsulta.setRequestMethod("POST");
uConsulta.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uConsulta.addRequestProperty("Host", "host");
//...

I'm trying to connect to a web page through a HttpURLConnection but it's not responding the same as a regular browser (firefox, chrome). I'm getting an error 500.

With the same code I can get "anyother" page (google, for example). My code is posted below, but I'm pretty sure it is ok.

Using "Live HTTP Headers" firefox addon I sent the same request and received a valid response.

//...
String urlConsulta = "myURL";
URL url = new URL(urlConsulta);
HttpURLConnection uConsulta = (HttpURLConnection)consulta.openConnection();
uConsulta.setDoOutput(true);
uConsulta.setRequestMethod("POST");
uConsulta.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uConsulta.addRequestProperty("Host", "host");
//...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2024-10-03 09:24:37

这可能是由一切原因引起的。服务器可能依赖于某些请求参数的存在。服务器可能依赖于有效的会话。服务器可以嗅探用户代理。等等..等等.阅读服务器日志以查找错误原因。或者,如果您无权访问服务器日志,请通过 HttpURLConnection#getErrorStream(),它可能包含错误详细信息。

另请参阅:

This can be caused by everything. The server may rely on the presence of certain request parameters. The server may rely on a valid session. The server may sniff the user agent. Etc..etc.. Read the server logs for the cause of the error. Or if you don't have access to server logs, read the body of the error response by HttpURLConnection#getErrorStream(), it may contain the error details.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文