DefaultHttpClient GET 和 POST 命令 Java Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2511145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:38:51  来源:igfitidea点击:

DefaultHttpClient GET and POST commands Java Android

javaandroidhttpposthttpwebrequest

提问by RenegadeAndy

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.

一个 Android 应用程序,允许我从移动应用程序而不是网站将 CokeZone 代码提交到 CokeZone.co.uk。

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.

问题是 - 我在发送 post 命令后从主页获得的 html 是默认的 - 好像我没有登录 - 所以出了点问题。

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.

任何人都可以请帮忙!它可能是我将 POST 发送到的 URL,或者 POST 命令中的参数——我没有做过很多这样的事情,所以它可能是显而易见的。

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());
        }
    }

采纳答案by Sean Owen

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.

对登录请求的响应包含一个带有登录会话的会话 ID 的 Cookie。您必须在后续请求中将其发回服务器,否则那些其他请求与您的登录无关。

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.

编辑:如果 HttpClient 实际上正在管理您的 cookie(对照http://hc.apache.org/httpcomponents-client/tutorial/html/statemgmt.html 进行检查),那么...登录后可能登录将您重定向到其他地方,并且您需要遵循该重定向以获取 cookie 吗?这里有点猜测,但听起来你真的没有完成登录。