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