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

标签 android http

我正在尝试使用我的 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/

相关文章:

android - 无法运行adb反向: spawnSync React-Native ENOENT

Android:如何知道应用程序类中的组件类型?

rest - Go Gorilla Mux 没有按预期路由

rest - 404 状态代码是否适用于插入时不存在的实体?

java - 如何在 ImageView 中显示捕获的图像?

Android 删除的 View 不会被垃圾收集

java - Android JUnit 未捕获来自 Runtime.getRuntime().exec() 的 IOException

angular - 我如何强制 Angular Universal 预渲染来自外部 api 的内容?

http - 将 mux.Walk 的结果发送到 ResponseWriter

c++ - WinHTTP 和 Websocket