Klaasvaak
Klaasvaak

Reputation: 5644

Android: showing a captcha from server

For my application I need to use a captcha for verification. I am using this link for it: http://vocublablair.nl/webservices/send.php

It returns a string something like this: /webservices/simple-php-captcha.php?_CAPTCHA&t=0.59145200+1338304461

Then that link should be called, being: http://vocublablair.nl/webservices/simple-php-captcha.php?_CAPTCHA&t=0.59145200+1338304461

When I call this with the same HttpClient (so with the right session cookie) it gives the following error:

java.lang.IllegalArgumentException: Illegal character in query at index 94: http://vocublablair.nl/webservices/simple-php-captcha.php?_CAPTCHA&t=0.59145200+1338304461

Upvotes: 0

Views: 2145

Answers (2)

Klaasvaak
Klaasvaak

Reputation: 5644

I solved the problem doing the following:

First of all the link I got back had to be HTML encoded using:

Html.fromHtml(String)

Calling the correct URL:

content = sb.toString(); //the fetched link
String myCookie = "";
//get session cookie from other call.

List<Cookie> cookies = ((DefaultHttpClient)httpClient).getCookieStore().getCookies();
for(Cookie c : cookies){
if(c.getName().equals("PHPSESSID")){
    myCookie = c.getName() + "=" + c.getValue();
    break;
   }
   }

URL url = new URL(Html.fromHtml("http://vocublablair.nl" + content).toString());
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setRequestProperty("Cookie", myCookie);
connection.connect();
Bitmap bit = BitmapFactory.decodeStream(new FlushedInputStream((InputStream) connection.getContent()));

I am using a FlushedInputStream. Read about it here: http://twigstechtips.blogspot.com/2011/10/android-loading-jpgpng-images-from-url.html

Upvotes: 0

Duke
Duke

Reputation: 1731

best way is to generate your own captcha image because using third party resources may cause inconvenient for your customers. for generating capthcha in android you could use this simple library : http://simplecaptcha.sourceforge.net/

Upvotes: 1

Related Questions