HttpURLConnection 和浏览器连接之间的差异
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由一切原因引起的。服务器可能依赖于某些请求参数的存在。服务器可能依赖于有效的会话。服务器可以嗅探用户代理。等等..等等.阅读服务器日志以查找错误原因。或者,如果您无权访问服务器日志,请通过
HttpURLConnection#getErrorStream()
,它可能包含错误详细信息。另请参阅:
URLConnection
触发并处理 HTTP 请求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:
URLConnection
to fire and handle HTTP requests