- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用我的 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 被传递到 HttpPost
但 HttpGet
被用于请求,因此用户名/密码不会' 已经传递给服务器了。
虽然有时想得太多确实会把人引向正确的方向
关于AndroidHttpClient 期望响应代码 302 但收到 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669789/
一位客户报告了一个奇怪的错误。在 AsyncTask 中执行正常的 AndroidHttpClient.execute() 时,应用程序崩溃,他得到以下堆栈跟踪 java.lang.RuntimeEx
我使用 AndroidHttpClient 从我的代码中收到 IOException。代码有问题吗?无论我使用端口 80 还是 端口 443,以及使用 Http 架构 http 或 https,都会引
我对 Android 开发很陌生(今天才开始),对 Java 开发也很陌生,尽管我已经使用 C# 编程好几年了。 我正在尝试使用 AndroidHttpClient 发出 HTTP 请求,但我的代码甚
我正在使用 Lynda 的 2014 年教程学习 android restful web 服务。代码使用这个 httpManager 类: import android.net.http.And
我正在使用 Android 3.0 和 3.1。我在我的应用程序中使用 AndroidHttpClient 类,我使用 execute(HttpUriRequest) 执行。 我想在发送数据时更新 U
public InputStream getInputStream() { AndroidHttpClient client = AndroidHttpClient.newInstance(U
我已按照 kuester2000's answer 的说明进行操作,但我的超时设置似乎不起作用。 try { int timeout = 3000; URL myURL = //som
我的代码有问题,希望得到帮助。我首先使用的是这段代码: new DefaultHttpClient().execute(new HttpGet(linkk)).getEntity().
这两者之间有很大区别吗?最好使用哪一个? 最佳答案 基于Android源码: https://github.com/CyanogenMod/android_frameworks_base/blob/g
我写了下面的代码,它工作正常: private static final String URL = "http://test.net/api/mobile/logininfo"; private An
我对此比较陌生,我一直在使用 AndroidHttpClient 来帮助通过 Parse 将图像下载到我的应用程序。现在使用 Sdk 23,我必须重写一些类。我的问题很简单。 我们以下面这段没有做任何
我正在尝试使用我的 Android 应用程序登录 https 网站。如果登录成功,网站返回响应代码 302,如果登录不成功,则返回 200。我研究了如何使用 AndroidHttpClient 并查看
我在我的应用程序中使用 AndroidHttpClient。现在我需要对请求使用基本授权。 http://developer.android.com/reference/android/net/htt
关于 AndroidHttpClient 的信息非常少,特别是我找不到任何好的例子。从我读到的内容来看——我可以使用这个客户端,它是为 SSL 预先配置的。我的目标是 2.2+,所以它很适合我。 关于
我建立了一个非常简单的项目来测试 Robolectric + Data Binding + Retrolambda 的集成。当我运行测试服时,我收到以下消息: Error:(30, 30) Gradl
我并没有真正实例化 AndroidHttpClient在我的代码中的任何位置,但我确实启动了一个识别器 Intent ,它在我的应用程序运行时的某个时刻产生了这个异常: Leak found java
我有一个 AsyncTask 设置来创建一个 AndroidHttpClient 并使用 HttpPost 调用 Execute和我提供的 BasicHttpContext 对象。我像这样设置了 Ht
API 级别 9( Gingerbread )哪个更好,为什么?使用 Android HttpClient 而不是 HttpUrlConnection 有什么好处吗? 我们将不胜感激任何帮助或指导。
当我混淆构建和运行时,我当然得到了这个,但我不知道如何修复它。 我尝试添加所有这些我的 proguard 配置文件,但没有帮助: -keep class android.net.http.** { *
目前我正在使用 DefaultHttpClient 和 ThreadSafeClientConnManager。这工作正常,但我想用 AndroidHttpClient 替换它。不幸的是,我无法添加目
我是一名优秀的程序员,十分优秀!