gpt4 book ai didi

AndroidHttpClient 期望响应代码 302 但收到 200

转载 作者:太空狗 更新时间:2023-10-29 15:21:13 26 4
gpt4 key购买 nike

我正在尝试使用我的 Android 应用程序登录 https 网站。如果登录成功,网站返回响应代码 302,如果登录不成功,则返回 200。我研究了如何使用 AndroidHttpClient 并查看了示例,但我看不出我的代码与他们的代码有任何区别。无论我向网站发送什么用户名和密码,我都会收到 200 的响应代码——即使组合是正确的。由于该网站使用安全 http,我是否必须做一些特别的事情?这是我的代码。非常感谢任何帮助。

public void login(String url, String username, String password){
CookieStore cookieStore;
HttpContext httpContext;
HttpGet httpGet;
HttpResponse httpResponse;
HttpPost post;
AndroidHttpClient httpClient;

cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpClient = AndroidHttpClient.newInstance("Android");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", username));
params.add(new BasicNameValuePair("pass", password));
params.add(new BasicNameValuePair("form_id", "user_login"));

httpGet = new HttpGet(url);
post = new HttpPost(url);
try {
post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
httpResponse = httpClient.execute(httpGet, httpContext);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("My App", httpResponse.getStatusLine().getStatusCode());
}

最佳答案

您应该使用 POST 而不是 GET,因为 GET 会自动处理重定向,但是:

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

引用:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

编辑:在进一步检查中,看起来 KVP 被传递到 HttpPostHttpGet 被用于请求,因此用户名/密码不会' 已经传递给服务器了。

虽然有时想得太多确实会把人引向正确的方向

关于AndroidHttpClient 期望响应代码 302 但收到 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com