简体   繁体   中英

Android using cookies with HttpPost and URLconnection

I'm porting web application to android, and i have a question there. I'm downloading a captcha image from PHP script, and I want to retrieve cookies :

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

and use same cookies in verification here:

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

How can i do it? Retrieve the cookies from URLConnection and use them in that HTTPPost request.

Thanks!

yay! Found the solution. I do in captcha get that:

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

And on page verification that :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM