Android: log into website and preserve session/cookie using DefaultHttpClient

前端 未结 4 1670
死守一世寂寞
死守一世寂寞 2021-02-08 11:08

I\'ve been through different tutorials and this website, but couldn\'t find a proper solution. On the other hand, I\'ve seen apps logging into websites and requesting further in

4条回答
  •  隐瞒了意图╮
    2021-02-08 12:07

    You have to make DefaultHttpClient httpclient with singleton pattern so sessioncookie that you have still hold session from login.

    This is the Mainactivity class :

    public static DefaultHttpClient httpClient;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RequestPage request = new RequestPage();
        request.post("http://www.example.com/login.php");
    
        RequestPage requestProfile =new RequestPage();
        requestProfile.post("http://www.example.com/profile.php");
    }
    

    and this is the RequestPage class:

    private InputStream post(String url){
        String paramUsername = "username";
        String paramPassword = "pass";
    
        if(MainActivity.httpClient==null){
            MainActivity.httpClient = new DefaultHttpClient();
        }
        DefaultHttpClient httpClient = MainActivity.httpClient;
    
        // In a POST request, we don't pass the values in the URL.
        //Therefore we use only the web page URL as the parameter of the HttpPost argument
        HttpPost httpPost = new HttpPost(url);
    
                // Because we are not passing values over the URL, we should have a mechanism to pass the values that can be
        //uniquely separate by the other end.
        //To achieve that we use BasicNameValuePair             
        //Things we need to pass with the POST request
        BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("username", paramUsername);
        BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("password", paramPassword);
    
        // We add the content that we want to pass with the POST request to as name-value pairs
        //Now we put those sending details to an ArrayList with type safe of NameValuePair
        List nameValuePairList = new ArrayList();
        nameValuePairList.add(usernameBasicNameValuePair);
        nameValuePairList.add(passwordBasicNameValuePAir);
    
        try {
            // UrlEncodedFormEntity is an entity composed of a list of url-encoded pairs. 
            //This is typically useful while sending an HTTP POST request. 
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
    
            // setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
            httpPost.setEntity(urlEncodedFormEntity);
    
            try {
                // HttpResponse is an interface just like HttpPost.
                //Therefore we can't initialize them
                HttpResponse httpResponse = httpClient.execute(httpPost);
    
                // According to the JAVA API, InputStream constructor do nothing. 
                //So we can't initialize InputStream although it is not an interface
    
    
                return httpResponse.getEntity().getContent();
    
            } catch (ClientProtocolException cpe) {
                System.out.println("First Exception caz of HttpResponese :" + cpe);
                cpe.printStackTrace();
            } catch (IOException ioe) {
                System.out.println("Second Exception caz of HttpResponse :" + ioe);
                ioe.printStackTrace();
            }
    
        } catch (UnsupportedEncodingException uee) {
            System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
            uee.printStackTrace();
        }
    
        return null;
    }
    

提交回复
热议问题