获得HTTP 407错误作为一个IOException [英] Getting http 407 error as an IOException
问题描述
我用在我的Android应用程序的 HttpURLConnection类
通过在需要身份验证的代理。这里是我的code和我的问题后,我将解释。
I'm using in my android application the HttpURLConnection
through a proxy where an authentication is needed. Here is my code and I will explain you after my problem.
HttpURLConnection connection = null;
int responseCode = -1;
try {
connection = (HttpURLConnection) myUrl.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout(DEFAULT_TIMEOUT);
connection.setReadTimeout(DEFAULT_TIMEOUT);
responseCode = connection.getResponseCode();
System.out.println("ResponseCode = " + responseCode);
} catch (IOException e) {
System.out.println("Exception : " + e.getMessage());
}
我的问题是,我得到它具有以下消息GETRESPONSE code()方法的一个例外:无法与代理验证
。
通常情况下,此特定错误有一个HTTP错误code:407。但在这里我刚刚得到一个例外,但不响应code与407值
My problem is that I get an exception on the getResponseCode() method which has the following message : Failed to authenticate with proxy
.
Usually, this specific error has an http error code : 407. But here I just got an exception but not a response code with the 407 value.
我要申请的登录名和密码连接到代理服务器的解决方案,但我只想要的情况下适用该解决方案有一个407错误(而不是每次都让我赶上一个例外)。
I have the solution to apply the login and password to connect to the proxy, but I want to apply this solution only in the case there is a 407 error (and not each time I catch an exception).
任何想法将AP preciated。
谢谢你。
Any idea will be appreciated. Thanks.
推荐答案
尝试追赶的Htt presponseException
代替,并检查其状态code( .getStatus code()
),看看它是否是407(或更好,但 HttpURLConnection.HTTP_PROXY_AUTH
,更具可读性)
Try catching HttpResponseException
instead, and checking its status code (.getStatusCode()
) to see if it's 407 (or better yet, HttpURLConnection.HTTP_PROXY_AUTH
, more readable).
的Htt presponseException
是 IOException异常
的一个子类,我认为这是一个的抛出的实际的异常
HttpResponseException
is a subclass of IOException
, and I think that's the actual exception that's being thrown.
(http://developer.android.com/reference/org/apache/http/client/Htt$p$psponseException.html获取更多信息。)
这篇关于获得HTTP 407错误作为一个IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!