Reputation: 129
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!
Upvotes: 0
Views: 1968
Reputation: 129
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);
Upvotes: 1