当我调用 connect() 时 Java HttpURLConnection 未连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2151359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:43:10  来源:igfitidea点击:

Java HttpURLConnection doesn't connect when I call connect()

javaurlconnectionhttp-status-code-302httpurlconnection

提问by sangfroid

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

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

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

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

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?

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

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。但是,如果我通过调用 connection.getResponseCode() 更改了 connection.connect() 行,它会神奇地工作。

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.

谢谢您的帮助。

回答by erickson

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.

connect()方法只是创建一个连接。你必须提交请求(通过调用getInputStream()getResponseCode()getResponseMessage()),用于将被返回并处理该响应。

回答by brabster

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

连接()方法在URLConnection类实现,并且不被覆盖HttpURLConnection的类。

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

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

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.

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

回答by g89n7man

Add the following in your onCreate

在您的 onCreate 中添加以下内容

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

回答by Dan Rosenstark

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

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

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

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

回答by fastcodejava

Why don't you use HttpClientfrom apache.

为什么不使用Apache 的HttpClient