[英]Why is my basic authentication working on POST, but not GET requests?
Below is a method I'm using to make calls to a REST API.下面是我用来调用 REST API 的方法。 It works fine with POST (ie param isHttpPOST=true), and returns results from the server (which requires basic authentication).它适用于 POST(即参数 isHttpPOST=true),并从服务器返回结果(需要基本身份验证)。
But when using the GET code path (isHttpPOST=false), the authentication fails, as if I provided no credentials at all.但是当使用 GET 代码路径 (isHttpPOST=false) 时,身份验证失败,就好像我根本没有提供任何凭据一样。 I cannot see why, as the auth code applies to POST and GET.我不明白为什么,因为身份验证代码适用于 POST 和 GET。
What else needs doing to authenticate on a GET request?还需要做什么来对 GET 请求进行身份验证?
private static HttpResponse makeHttpApiCall(String url, String json, boolean isHttpPOST, String username, String password)
{
DefaultHttpClient httpClient = new DefaultHttpClient();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
httpClient.getCredentialsProvider().setCredentials(new AuthScope("blah.com", 80), creds);
HttpResponse response;
try {
if ( isHttpPOST )
{
HttpPost httppost = new HttpPost(url);
StringEntity se = new StringEntity(json);
se.setContentEncoding("UTF-8");
httppost.setHeader("Content-Type", "application/json");
httppost.setEntity(se);
response = httpClient.execute(httppost);
}
else
{
HttpGet get = new HttpGet(url);
response = httpClient.execute(get);
}
} catch (ClientProtocolException e) {
Trace.e(TAG, "There was a protocol based error making API call", e);
return null;
} catch (IOException e) {
Trace.e(TAG, "There was an IO Stream related error making API call", e);
return null;
} catch (Exception e) {
Trace.e(TAG, "Failed to get a response from API call (unknown error)", e);
return null;
}
return response;
}
i got the same problem, but the other way around, it works on GET but not on POST.我遇到了同样的问题,但反过来说,它适用于 GET 但不适用于 POST。
if the server is yours, i would recommend to use a freeware called Wireshark, to check what's going on in the transmition itself.如果服务器是您的,我建议使用名为 Wireshark 的免费软件来检查传输本身的情况。
using that tool, showed me that the client sometimes makes a first annonymous request before making a second authorized request, but then the server kinda loses it and doesn't respond.. i still can't solve this issue.. it's driving me nuts.使用该工具,向我展示了客户端有时会在发出第二个授权请求之前发出第一个匿名请求,但随后服务器有点丢失它并且没有响应..我仍然无法解决这个问题..它让我发疯.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.