【问题标题】:Android using cookies with HttpPost and URLconnectionAndroid 使用带有 HttpPost 和 URLconnection 的 cookie
【发布时间】:2012-11-05 12:10:44
【问题描述】:

我正在将 Web 应用程序移植到 android,我有一个问题。我正在从 PHP 脚本下载验证码图像,并且我想检索 cookie:

       Bitmap mIcon11 = null;
          try {
              java.net.URL url = new java.net.URL(urldisplay);

              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.setUseCaches(true);
              connection.connect();
            InputStream in = connection.getInputStream();
            mIcon11 = BitmapFactory.decodeStream(in);

          } catch (Exception e) {
              Log.d("Error", "NEINA SIUSTI IMAGO");
              e.printStackTrace();
          }

并在此处使用相同的 cookie 进行验证:

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://somesite.lt/index.php");

try {
    // Add your data
    List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (2);
    nameValuePairs.add(new BasicNameValuePair("phoneno", ((EditText) findViewById(R.id.number)).getText().toString()));
    nameValuePairs.add(new BasicNameValuePair("phbody", ((EditText) findViewById(R.id.sms)).getText().toString()));
    nameValuePairs.add(new BasicNameValuePair("captcha_code", ((EditText) findViewById(R.id.code)).getText().toString()));
    nameValuePairs.add(new BasicNameValuePair("agree", "on"));
    nameValuePairs.add(new BasicNameValuePair("submit", ""));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    Log.d("ers", "ENtit  " + nameValuePairs.toString());
    // Execute HTTP Post Request

    HttpResponse response = httpclient.execute(httppost, mHttpContext);

我该怎么做?从 URLConnection 检索 cookie 并在该 HTTPPost 请求中使用它们。

谢谢!

【问题讨论】:

  • 在这里尝试接受的答案:stackoverflow.com/questions/8733758/…
  • 好的,现在我知道如何使用 httppost 查询 cookie,但我需要知道如何在 HttpURLConnection 中获取它们。
  • 无法在任何地方找到答案。请帮帮我..

标签: java android http cookies


【解决方案1】:

耶!找到了解决方案。我在验证码中得到:

String urldisplay = urls[0];
              Bitmap mIcon11 = null;
              try {
                  java.net.URL url = new java.net.URL(urldisplay);

                  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                  connection.setUseCaches(true);

                  connection.connect();
                  String cookie = connection.getHeaderField("Set-Cookie");

                  cookie = cookie.substring(0, cookie.indexOf(';'));

                  mSes = cookie;
                InputStream in = connection.getInputStream();
                mIcon11 = BitmapFactory.decodeStream(in);

在页面验证上:

DefaultHttpClient httpclient = new DefaultHttpClient();


                                HttpPost httppost = new HttpPost("http://somesite.lt/index.php");
                                httppost.addHeader("Cookie", mSes);
                                try {
                                    // Add your data
                                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                                    nameValuePairs.add(new BasicNameValuePair("phoneno", ((EditText)findViewById(R.id.number)).getText().toString()));
                                    nameValuePairs.add(new BasicNameValuePair("phbody", ((EditText)findViewById(R.id.sms)).getText().toString()));
                                    nameValuePairs.add(new BasicNameValuePair("captcha_code", ((EditText)findViewById(R.id.code)).getText().toString()));
                                    nameValuePairs.add(new BasicNameValuePair("agree", "on"));
                                    nameValuePairs.add(new BasicNameValuePair("submit", ""));
                                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                    Log.d("ers","ENtit  "+nameValuePairs.toString());
                                    // Execute HTTP Post Request

                                    HttpResponse response = httpclient.execute(httppost, mHttpContext);

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多