DefaultHttpClient GET和POST命令的Java的Andr​​oid命令、POST、GET、DefaultHttpClient

2023-09-05 10:39:36 作者:爱是寂寞撒的谎

好吧,这是我的应用程序:

这是Android应用程序,让我从一个移动应用程序,而不是从网站提交CokeZone codeS到CokeZone.co.uk。

所以我写了本节code做后登录命令,然后检查是否IM登录后。

问题是 - HTML我从网页获得后,我送post命令是默认的 - 就好像我不是登录 - 这样的东西是怎么了?

任何人都可以请帮助!它可能是网址IM发送POST,或POST命令中的PARAMS - 我还没有做了很多这方面的东西所以它可能是一些明显

下面是我的code到目前为止:

  DefaultHttpClient的HttpClient =新DefaultHttpClient();

    HTTPGET HTTPGET =新HTTPGET(URL);

    HTT presponse响应= httpclient.execute(HTTPGET);
    HttpEntity实体= response.getEntity();
    thisResponse =的PrintPage(entity.getContent());
    Log.e(调试,thisResponse);
    的System.out.println(登录形式得到:+ response.getStatusLine());
    如果(实体!= NULL){
        entity.consumeContent();
    }
    的System.out.println(初始设置的饼干:);
    名单<饼干>饼干= httpclient.getCookieStore()的getCookies()。
    如果(cookies.isEmpty()){
        的System.out.println(无);
    } 其他 {
        的for(int i = 0; I< cookies.size();我++){
            的System.out.println( - + cookies.get(ⅰ)的ToString());
        }
    }

    HttpPost httpost =新HttpPost(https://secure.cokezone.co.uk/home/blank.jsp?_DARGS=/home/login/login.jsp);

    名单<的NameValuePair> nvps =新的ArrayList<的NameValuePair>();
    nvps.add(新BasicNameValuePair(_ dyncharset,ISO-8859-1));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.loginFormBean.name,renegadeandy%40gmail.com));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.loginFormBean.name,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.cookiedUser,假));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.cookiedUser,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.loginFormBean.password,passwordval));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.loginFormBean.password,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.rememberMe,是));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.rememberMe,假));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.aSuccessURL,http://www.cokezone.co.uk/home/index.jsp));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.aSuccessURL,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.aErrorURL,http://www.cokezone.co.uk/home/index.jsphttps://secure.cokezone.co.uk/home/index的.jsp));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.aErrorURL,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.explicitLogin,真));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.explicitLogin,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.fICLogin,登录));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.fICLogin,+));
    nvps.add(新BasicNameValuePair(/ GRLP /登录/ LoginHandler.fICLogin,登录));
    nvps.add(新BasicNameValuePair(_ D:/grlp/login/LoginHandler.fICLogin,+));
    nvps.add(新BasicNameValuePair(_ DARGS,/home/login/login.jsp));

    httpost.setEntity(新UrlEn codedFormEntity(nvps,HTTP.UTF_8));

    响应= httpclient.execute(httpost);
    实体= response.getEntity();

    的System.out.println(登录形式得到:+ response.getStatusLine());
    如果(实体!= NULL){
      thisResponse =的PrintPage(entity.getContent());
      entity.consumeContent();
    }

    Log.e(调试,thisResponse);
    Log.e(调试,完成);

    HTTPGET =新HTTPGET(http://www.cokezone.co.uk/home/index.jsp);

    响应= httpclient.execute(HTTPGET);
    实体= response.getEntity();

  TextView的点=(TextView中)findViewById(R.id.points);
  (getPoints(entity.getContent())的toString())points.setText;
  debug.setText(thisResponse);
    的System.out.println(后登录的Cookie:);
    饼干= httpclient.getCookieStore()的getCookies()。
    如果(cookies.isEmpty()){
        的System.out.println(无);
    } 其他 {
        的for(int i = 0; I< cookies.size();我++){
            的System.out.println( - + cookies.get(ⅰ)的ToString());
        }
    }
 

解决方案

登录请求的响应包含与您的登录会话的会话ID的Cookie。你必须张贴此回与其他请求不涉及您的登录在后续请求,否则服务器。

编辑:如果HttpClient的其实是管理你的cookies(核对 HTTP: //hc.apache.org/httpcomponents-client/tutorial/html/statemgmt.html ),那么......也许登录名登录后重定向你到别的地方,你需要遵循重定向为了抢曲奇饼?一种猜测在这里,但它确实听起来像你没有完成登录。

Ok this is my application :

An Android app to allow me to submit CokeZone codes into CokeZone.co.uk from a mobile app instead of from the website.

So I wrote this section of code to do the post logon command and then check to see if im logged in after.

Problem is - the html I get from the homepage after I send the post command is the default - as if im not logged in - so something is going wrong.

Can anyone please help! Its probably the URL im sending the POST to, or the params within the POST command - I havent done much of this stuff so its probably something obvious.

Below is my code so far:

 DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet(url);

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    thisResponse = printPage(entity.getContent());
    Log.e("debug",thisResponse);
    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpPost httpost = new HttpPost("https://secure.cokezone.co.uk/home/blank.jsp?_DARGS=/home/login/login.jsp");

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("_dyncharset", "ISO-8859-1"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.name","renegadeandy%40gmail.com"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.name", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.cookiedUser", "false"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.cookiedUser", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.password", "passwordval"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.password", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.rememberMe", "yes"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.rememberMe", "false"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aSuccessURL", "http://www.cokezone.co.uk/home/index.jsp"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.aSuccessURL", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "http://www.cokezone.co.uk/home/index.jsphttps://secure.cokezone.co.uk/home/index.jsp"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.explicitLogin", "true"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.explicitLogin", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "login"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
    nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "LOGIN"));
    nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
    nvps.add(new BasicNameValuePair("_DARGS", "/home/login/login.jsp"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = httpclient.execute(httpost);
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
      thisResponse = printPage(entity.getContent());
      entity.consumeContent();
    }

    Log.e("debug",thisResponse);
    Log.e("debug","done");

    httpget = new HttpGet("http://www.cokezone.co.uk/home/index.jsp");

    response = httpclient.execute(httpget);
    entity = response.getEntity();

  TextView points = (TextView)findViewById(R.id.points);
  points.setText(getPoints(entity.getContent()).toString());
  debug.setText(thisResponse);
    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

解决方案

The response to the logon request contains a Cookie with the session ID for your logged-in session. You must post this back to the server in subsequent requests or else those other requests are not related to your logon.

EDIT: If HttpClient is actually managing your cookies (check against http://hc.apache.org/httpcomponents-client/tutorial/html/statemgmt.html) then... maybe the login is redirecting you somewhere else after login, and you need to follow that redirect in order to grab cookies? Kind of guessing here but it really sounds like you're not completing login.