简体   繁体   English

如何在Android的ImageView中显示验证码?

[英]How to display captcha in ImageView in Android.?

I have a PNR Inquiry app on Google Play. 我在Google Play上有一个PNR查询应用程序。 It was working very fine. 运行得很好。 But recently Indian Railwys added captcha to their PNR Inquiry section and because of this I am not able to pass proper data to the server to get proper response. 但是最近Indian Railwys在他们的PNR查询部分添加了验证码,因此,我无法将正确的数据传递给服务器以获取正确的响应。 How to add this captcha in my app in form of an imageview and ask the users to enter captcha details also so that I can send proper data and get proper response. 如何以imageview的形式在我的应用程序中添加此验证码,并要求用户也输入验证码详细信息,以便我可以发送适当的数据并获得适当的响应。

Indian Railways PNR Inquiry Link 印度铁路PNR查询链接

在此处输入图片说明

If you check the html code, its actualy pretty bad captcha. 如果您检查html代码,它实际上是非常糟糕的验证码。 Background of captcha is: http://www.indianrail.gov.in/1.jpg Those numbers are actualy in input tag: 验证码的背景是: http : //www.indianrail.gov.in/1.jpg这些数字实际上在输入标签中:

<input name="lccp_cap_val" value="14167" id="txtCaptcha" type="hidden">

What they are doing is, via javascript, use numbers from that hidden input tag and put them on that span with "captcha" background. 他们正在通过JavaScript使用隐藏输入标签中的数字,并将其放在具有“验证码”背景的范围内。

So basicaly your flow is: 因此,基本上,您的流程是:

  • read their html 阅读他们的html

  • get "captcha" (lol, funny captcha though) value from input field 从输入字段中获取“验证码”(大声笑,虽然有趣的验证码)值

  • when user puts data in your PNR field and presses Get Status 当用户将数据放入您的PNR字段并按获取状态时

  • post form field, put PNR in proper value, put captcha in proper value 发布表格字段,将PNR设置为适当的值,将验证码设置为适当的值

  • parse response 解析响应

Oh yeah, one more thing. 哦,是的,还有一件事。 You can put any value in hidden input and "captcha" input, as long as they are the same. 您可以在隐藏输入和“验证码”输入中输入任何值,只要它们相同即可。 They aren't checking it via session or anything. 他们没有通过会话或其他任何方式对其进行检查。

EDIT (code sample for submiting form): To simplify posting form i recommend HttpClient components from Apache: http://hc.apache.org/downloads.cgi Lets say you downloaded HttpClient 4.3.1. 编辑(提交表单的代码示例):为了简化发布表单,我建议从Apache获得HttpClient组件: http : //hc.apache.org/downloads.cgi假设您下载了HttpClient 4.3.1。 Include client, core and mime libraries in your project (copy to libs folder, right click on project, properties, Java Build Path, Libraries, Add Jars -> add those 3.). 在您的项目中包括客户端库,核心库和mime库(复制到libs文件夹,右键单击项目,属性,Java Build Path,Libraries,Add Jars->添加那些3.)。

Code example would be: 代码示例为:

private static final String FORM_TARGET = "http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi.cgi";
    private static final String INPUT_PNR = "lccp_pnrno1";
    private static final String INPUT_CAPTCHA = "lccp_capinp_val";
    private static final String INPUT_CAPTCHA_HIDDEN = "lccp_cap_val";

    private void getHtml(String userPnr) {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody(INPUT_PNR, userPnr); // users PNR code
        builder.addTextBody(INPUT_CAPTCHA, "123456");
        builder.addTextBody("submit", "Get Status");
        builder.addTextBody(INPUT_CAPTCHA_HIDDEN, "123456"); // values don't
                                                                // matter as
                                                                // long as they
                                                                // are the same

        HttpEntity entity = builder.build();

        HttpPost httpPost = new HttpPost(FORM_TARGET);
        httpPost.setEntity(entity);

        HttpClient client = new DefaultHttpClient();

        HttpResponse response = null;
        String htmlString = "";
        try {
            response = client.execute(httpPost);
            htmlString = convertStreamToString(response.getEntity().getContent());
                    // now you can parse this string to get data you require.
        } catch (Exception letsIgnoreItForNow) {
        }
    }

    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException ignoredOnceMore) {
        } finally {
            try {
                is.close();
            } catch (IOException manyIgnoredExceptions) {
            }
        }

        return sb.toString();
    }

Also, be warned i didn't wrap this in async call, so you will have to do that. 另外,请注意,我没有将其包装在异步调用中,因此您必须这样做。

Image from the network can be displayed in android via efficient image loading api's like Picasso/volley or simply image view via async task. 来自网络的图像可以通过高效的图像加载API(例如Picasso / volley)显示在android中,也可以通过异步任务仅显示图像。

considering all above things as basic build a logic such that you should need a image URL for that captcha if user resets or refresh the captcha it should reload new image via network call requesting the new request implementation, you have to get REST api access to the Indian railway and check in that any image uri available in that (it may be in base64 format ) 考虑到以上所有内容都是基本的构建逻辑,因此如果用户重置或刷新验证码,则需要该验证码的图像URL,如果用户应通过请求新请求实现的网络调用重新加载新图像,则必须获得REST api访问权限印度铁路,并检查是否有任何可用的图像uri(可能为base64格式)

if REST API is not available you may think of building your own server with this code 如果REST API不可用,您可能会考虑使用此代码构建自己的服务器

RESTful API to check the PNR Status RESTful API检查PNR状态

pnrapi pnrapi

Update: you don't need to do this complex hacks , just implement Drago's answer ! 更新:您不需要执行这种复杂的技巧,只需实现Drago的答案即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM