对于存在的网页,但对于没有足够权限的用户(他们未登录或不属于正确的用户组),要提供的正确HTTP响应是什么?401?403?别的什么?到目前为止,我对每个人的看法都不太清楚.哪些用例适合每个响应?
http-status-codes http-response-codes http-headers http-status-code-403 http-status-code-401
我试图通过Android HttpURLConnection(从中导入org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection)发送GET ,并在收到响应时抛出IOException:
在doRequestInternal()中:"收到的身份验证质询为空"
这个错误意味着什么,是什么导致了这个?我正在向授权标题写OAuth参数,但我也在其他场合这样做,没有问题.
if (connection == null) {
connection = (HttpURLConnection) new URL(endpointUrl).openConnection();
connection.setRequestMethod("GET");
}
//... do some OAuth message signing
connection.connect();
int statusCode = connection.getResponseCode(); // throws IOException
Run Code Online (Sandbox Code Playgroud) 我有一些典型的代码使用HttpURLConnection来获取带有URL的文件.他们在android 1.x和2.x中运行良好.但在Android 4.1中失败了!
我在网上搜索但发现的信息很少.有人请帮忙调查这个问题吗?
private String mURLStr;
private HttpURLConnection mHttpConnection;
...
url = new URL(mURLStr);
...
mHttpConnection = (HttpURLConnection) url.openConnection();
mHttpConnection.setDoOutput(true);
mHttpConnection.setRequestMethod("GET");
...
InputStream is = mHttpConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
getInputStream方法抛出异常:
08-01 15:56:48.856: W/System.err(13613): java.io.IOException: No authentication challenges found
08-01 15:56:48.856: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
08-01 15:56:48.866: W/System.err(13613): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
...
Run Code Online (Sandbox Code Playgroud) 我是android的新手,这是我在android上的第一个项目.我在"认证"问题上苦苦挣扎了一天多.我尝试了几个选项,但没有一个可行.
基本上,我想调用REST API并获得响应.我确信API中没有问题,因为我在另一个iOS应用程序中使用相同的API.
我传递授权标题但仍然显示身份验证未找到消息.我发现与stackoverflow相关的问题很少,但是其中一些没有用,有些对我没有意义.
我得到状态代码401.我知道这意味着要么没有传递身份验证,要么传递,那么它们就错了.在这里,我确信我传递的是正确的.
以下是我的代码:
try {
url = new URL(baseUrl);
}
catch (MalformedURLException me) {
Log.e(TAG, "URL could not be parsed. URL : " + baseUrl + ". Line : " + getLineNumber(), me);
me.printStackTrace();
}
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setConnectTimeout(TIMEOUT * 1000);
urlConnection.setChunkedStreamingMode(0);
// Set HTTP headers
String authString = "username:password";
String base64Auth = Base64.encodeToString(authString.getBytes(), Base64.DEFAULT);
urlConnection.setRequestProperty("Authorization", "Basic " + base64Auth);
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("Content-type", "application/json");
if (method.equals("POST") || method.equals("PUT")) {
// Set …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一些遗留代码,并在使用凌空时遇到问题.
我试图找到我们的主站点的API,它适用于一个帐户,但不适用于另一个帐户.我正在尝试弄清楚请求URL /标题中的差异以及响应中的内容,但我似乎无法在排序代码中找到将其打印到日志中的方法.
我得到的错误是
com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found
Run Code Online (Sandbox Code Playgroud)
我已经读到这可能是由于401响应,但我真的不知道这意味着什么,或者至少如何证明/测试它.我真的很困惑,它适用于一个帐户而不是另一个帐户.
网址略有不同,一个是我们的英国网站,另一个是我们的AM,但除此之外没有区别.
谢谢
我需要一个响应代码,但它抛出一个IOException.我不知道发生了什么事!
try
{
url = new URL(urlBuilder.toString());
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(TIME_OUT);
conn.setRequestMethod(METHOD_GET);
conn.setRequestProperty("accept", "*/*");
conn.connect();
int responseCode = conn.getResponseCode(); //throw IOException:Received authentication challenge is null
if (responseCode == HTTP_OK)
{
inStream = conn.getInputStream();
response = getResponse(inStream);
}
else
{
response = "response code?"+responseCode;
}
} catch (Exception e)
{
throw e;
}
finally
{
conn.disconnect();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
IOException是:
05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null
05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1694)
05-03 20:14:01.577: INFO/QQWeiBo(1515): Received …Run Code Online (Sandbox Code Playgroud)