我正在創建一個需要登錄到網站的應用程序。要做到這一點,我做了一個HttpClient並執行一個HttpPost的URL。據我所知,這是工作。 StatusCodes返回200.在此之後,我執行一個HttpGet到受限url(使用相同的客戶端)。但是,我總是返回禁用的例外或登錄屏幕回到我的LogCat中。從在網上搜索我發現有時需要首先在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)
http://www.checkupdown.com/status/E403.html –