4

我正在制作一个需要登录网站的应用程序。为此,我创建了一个 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)
4

3 回答 3

1

禁止错误通常表示您在 Web 服务器上访问的目录/文件存在权限问题。

检查所有者和组权限。还要检查文件上的写/读/执行标志。

您可以通过网络浏览器直接导航到该网址并查看它是否也显示禁止消息吗?

于 2012-06-06T12:07:10.140 回答
1

你可以使用这个条件

 StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {

                } else {

                }
于 2012-06-06T12:10:05.617 回答
1

甜蜜的成功!虽然我可以 POST 到 URL 并获得 200 StatusCode,但它不是“完整”的 URL。必须向其中添加 index.php。表格数据也不完整。需要再添加两个参数。

                params.add(new BasicNameValuePair("action", "login"));
                params.add(new BasicNameValuePair("login_submit", "Inloggen"));

通过检查该站点以获取它发布到该站点的表单数据来发现这一点。使用 Chrome 的开发者工具(网络选项卡)中的构建。感谢您的回复!

于 2012-06-06T23:15:02.270 回答