Android 登录网站

Android 登录网站,android,http,http-post,http-get,Android,Http,Http Post,Http Get,我正在制作一个需要登录网站的应用程序。为此,我创建了一个HttpClient并对url执行HttpPost。据我所知,这是可行的。状态码返回200。在此之后,我对受限url执行HttpGet(使用相同的客户端)。然而,我总是在我的LogCat中返回一个禁止的异常或登录屏幕。通过在web上搜索,我发现有时需要在HttpPost之前先对站点执行HttpGet,还需要附加CookieStore。两者都做了,但仍然没有成功。。有人能帮我吗 HttpClient client = new

我正在制作一个需要登录网站的应用程序。为此,我创建了一个HttpClient并对url执行HttpPost。据我所知,这是可行的。状态码返回200。在此之后,我对受限url执行HttpGet(使用相同的客户端)。然而,我总是在我的LogCat中返回一个禁止的异常或登录屏幕。通过在web上搜索,我发现有时需要在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();

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

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


您能否通过webbrowser直接导航到url,并查看它是否也显示禁止的消息?

您可以使用此条件

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

                } else {

                }

甜蜜的成功!虽然我可以发布到URL并获得200个状态码,但它不是“完整”的URL。必须向其中添加index.php。此外,表单数据不完整。需要再添加两个参数

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

通过检查网站上发布的表单数据发现了这一点。使用Chrome的内置开发工具(网络选项卡)。谢谢你的回复

我可以在登录时在浏览器中直接导航到url。如果我没有登录,它会显示一个可选的登录页面,上面有一个气泡,说我必须先登录。如果我再次将HttpGet中的url更改为主页(这将引导我进入浏览器中的登录页面),它将以注销设置返回主页。因此,我似乎无法通过应用程序保持登录或其他功能。当我使用postman并尝试使用chrome网络选项卡中的表单数据执行POST请求时,它总是链接回默认的欢迎页面,而不是实际登录。你是怎么修好的?
                params.add(new BasicNameValuePair("action", "login"));
                params.add(new BasicNameValuePair("login_submit", "Inloggen"));