0

我正在将 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 请求中使用它们。

谢谢!

4

1 回答 1

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);
于 2012-11-17T09:58:22.790 回答