其他分享
首页 > 其他分享> > android – 登录网站

android – 登录网站

作者:互联网

我正在制作一个需要登录网站的应用程序.为此,我创建一个HttpClient并对URL执行HttpPost.据我所知,这是有效的. StatusCodes回馈200.在此之后,我执行HttpGet到受限制的URL(使用相同的客户端).但是,我总是在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)

解决方法:

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

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

你可以通过webbrowser直接导航到网址,看看它是否也显示禁止的消息?

标签:http,android,http-get,http-post
来源: https://codeday.me/bug/20190630/1333301.html