我正在制作一个需要登录网站的应用程序。为此,我创建了一个 HttpClient 并对 url 执行 HttpPost。据我所知,这是有效的。StatusCodes 返回 200。在此之后,我对受限 url 执行 HttpGet(使用相同的客户端)。但是,我总是在我的 LogCat 中返回 Forbidden Exception 或登录屏幕。从网上搜索我发现有时需要在 HttpPost 之前先对站点执行 HttpGet,并且还要附加一个 CookieStore。两者都完成了,但仍然没有成功..这里有人可以帮助我吗?
HttpClient client = new DefaultHttpClient();
HttpGet initiate = new HttpGet(url);
HttpPost post = new HttpPost(url);
HttpGet page = new HttpGet(restrictedurl);
HttpContext localContext = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = client.execute(initiate, localContext);
Log.i(Tag, " " + response.getStatusLine().getStatusCode());
response.getEntity().consumeContent();
response = client.execute(post, localContext);
Log.i(Tag, " " + response.getStatusLine().getStatusCode());
response.getEntity().consumeContent();
ResponseHandler<String> handler = new BasicResponseHandler();
String htmlpage = client.execute(page, handler, localContext);
Log.i(Tag, " " + response.getStatusLine().getStatusCode());
Log.i(Tag, htmlpage);
response.getEntity().consumeContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
这是我的 LogCat:
06-06 13:38:47.033: I/LogTag (27839): StatusCode HttpGet: 200
06-06 13:38:47.197: I/LogTag (27839): StatusCode HttpPost: 200
06-06 13:38:47.361: W/System.err(27839): org.apache.http.client.HttpResponseException: Forbidden
06-06 13:38:47.377: W/System.err(27839): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
06-06 13:38:47.384: W/System.err(27839): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
06-06 13:38:47.384: W/System.err(27839): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
06-06 13:38:47.384: W/System.err(27839): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-06 13:38:47.384: W/System.err(27839): at com.dakro.wiebetaaltwat.MainActivity$AsyncPost.doInBackground(MainActivity.java:136)
06-06 13:38:47.384: W/System.err(27839): at com.dakro.wiebetaaltwat.MainActivity$AsyncPost.doInBackground(MainActivity.java:1)
06-06 13:38:47.392: W/System.err(27839): at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-06 13:38:47.392: W/System.err(27839): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-06 13:38:47.392: W/System.err(27839): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-06 13:38:47.392: W/System.err(27839): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-06 13:38:47.392: W/System.err(27839): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-06 13:38:47.400: W/System.err(27839): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-06 13:38:47.400: W/System.err(27839): at java.lang.Thread.run(Thread.java:856)