簡體   English   中英

Android使用帶有HttpPost和URLconnection的cookie

[英]Android using cookies with HttpPost and URLconnection

我正在將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請求中使用它們。

謝謝!

好極了! 找到了解決方案。 我在驗證碼中做到了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM