从网页获取验证码图像[android]

时间:2015-03-29 07:09:32

标签: android http response captcha

我试图创建一个与此页面具有相同功能的Android应用程序>> http://goo.gl/6pV6Zr

但是我无法让HttpPost工作,我无法获得精确的Captcha图像。

我试图使用jsoup检索验证码。

try {

            Document doc = Jsoup.connect(URL).get();

            Element img = doc.select("img[id=siimage]").first();
            String imgChar = img.attr("src");
            captchaUrl = SECURE_IMAGE_URL + imgChar.substring(1);

            InputStream input = new java.net.URL(captchaUrl).openStream();
            bitmap = BitmapFactory.decodeStream(input);

        } catch (IOException e) {

            e.printStackTrace();
        }

并在ImageView中设置位图。

captchaImage.setImageBitmap(bitmap);

这是我的HttpPost。

try{
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(POST_URL);


        ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair("number", number));
        nameValuePair.add(new BasicNameValuePair("message", message));
        nameValuePair.add(new BasicNameValuePair("ct_captcha", captcha));
        nameValuePair.add(new BasicNameValuePair("do", "send"));
        nameValuePair.add(new BasicNameValuePair("apicode", API_CODE));


        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null){

            String responseString = EntityUtils.toString(resEntity);
            Log.v(TAG, "Response: " + responseString);
        }


    }catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }
}

请指导我如何以正确的方式......

  1. 检索页面上的验证码图片。
  2. 制作一个帖子方法。

1 个答案:

答案 0 :(得分:0)

替换: -

  • this - Element img = doc.select("img[id=siimage]").first();

  • this - Element img = doc.select("img#siimage").first();