当我调用 connect() 时,Java HttpURLConnection 无法连接

发布于 2024-08-19 06:03:36 字数 1083 浏览 5 评论 0 原文

我正在尝试编写一个程序来对我的网络应用程序进行自动化测试。为了实现此目的,我使用 HttpURLConnection 打开一个连接。

我尝试测试的页面之一执行 302 重定向。我的测试代码如下所示:

URL currentUrl = new URL(urlToSend);
HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection();
connection.connect();
system.out.println(connection.getURL().toString());

那么,假设 urlToSend 是 http://www.foo.com/bar .jsp,并且此页面会将您重定向到 http://www.foo.com/quux .jsp。我的 println 语句应该打印出 http://www.foo.com/quux.jsp,对吧?

错误的。

重定向永远不会发生,它会打印出原始 URL。但是,如果我通过调用 connection.getResponseCode() 来更改 switch out the connection.connect() 行,它会神奇地起作用。

URL currentUrl = new URL(urlToSend);
HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection();
//connection.connect();
connection.getResponseCode();
system.out.println(connection.getURL().toString());

为什么我会看到这种行为?我做错了什么吗?

感谢您的帮助。

I'm trying to write a program to do automated testing on my webapp. To accomplish this, I open up a connection using HttpURLConnection.

One of the pages that I'm trying to test performs a 302 redirect. My test code looks like this :

URL currentUrl = new URL(urlToSend);
HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection();
connection.connect();
system.out.println(connection.getURL().toString());

So, let's say that urlToSend is http://www.foo.com/bar.jsp, and that this page redirects you to http://www.foo.com/quux.jsp. My println statement should print out http://www.foo.com/quux.jsp, right?

WRONG.

The redirect never happens, and it prints out the original URL. However, if I change switch out the connection.connect() line with a call to connection.getResponseCode(), it magically works.

URL currentUrl = new URL(urlToSend);
HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection();
//connection.connect();
connection.getResponseCode();
system.out.println(connection.getURL().toString());

Why am I seeing this behavior? Am I doing anything wrong?

Thanks for the help.

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

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

发布评论

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

评论(5

一桥轻雨一伞开 2024-08-26 06:03:36

connect() 方法只是创建一个连接。您必须提交请求(通过调用 getInputStream()getResponseCode()getResponseMessage())才能返回响应,并且已处理。

The connect() method just creates a connection. You have to commit the request (by calling getInputStream(), getResponseCode(), or getResponseMessage()) for the response to be returned and processed.

平生欢 2024-08-26 06:03:36

connect() 方法在 URLConnection 类中实现,并且不会被 HttpURLConnection 类。

URLConnection 类不知道 HTTP,因此即使它正在创建真正的连接,也不应该遵循 HTTP 重定向。

如果您想要 HTTP 协议固有的行为,您可能需要坚持使用 HttpURLConnection 类中实现的方法,如 getResponseCode() 方法。

The connect() method is implemented in the URLConnection class, and is not overridden by the HttpURLConnection class.

The URLConnection class is not aware of HTTP, and so should not follow the HTTP redirect even if it was creating a real connection.

If you want behaviour intrinsic to the HTTP protocol, you probably want to stick to methods implemented in the HttpURLConnection class, as the getResponseCode() method is.

愿与i 2024-08-26 06:03:36

尝试 setFollowRedirects,因为它可能有帮助。实际上,首先尝试 getFollowRedirects 看看是否是问题(不太可能,因为默认情况下是这样)。

编辑:如果这不是您的问题,我会尝试从连接中读取一些内容(就像您对 getResponseCode 所做的那样,但我也会尝试 getHeaderField查看阅读任何内容是否会导致重定向受到尊重)。

Try setFollowRedirects, as it might help. Actually, try getFollowRedirects to see if it's the problem, first (unlikely since it's true by default).

Edit: If this is not your problem, I would try reading something from the connection (as you do with getResponseCode but I would try also getHeaderField to see if reading anything at all causes the redirect to be respected).

相对绾红妆 2024-08-26 06:03:36

在 onCreate 中添加以下内容

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

Add the following in your onCreate

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 
皓月长歌 2024-08-26 06:03:36

为什么不使用 apache 的 HttpClient

Why don't you use HttpClient from apache.

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