我有一个谷歌PNR查询应用程式。这是工作非常精细。但是最近印度Railwys添加CAPTCHA他们PNR调查部分,因为这个我不能够适当的数据传递到服务器,以获得适当的反应。如何添加这个验证码在我的应用程序在ImageView的形式,并要求用户也输入验证码资料,以便我可以把正确的数据,并得到适当的反应。
I have a PNR Inquiry app on Google Play. 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. 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.
印度铁路PNR查询链接
推荐答案
如果你检查HTML code,其actualy pretty坏的验证码。验证码的背景是: www.indianrail.gov.in/1.jpg这些数字actualy在输入标签:
If you check the html code, its actualy pretty bad captcha. Background of captcha is: www.indianrail.gov.in/1.jpg Those numbers are actualy in input tag:
<input name="lccp_cap_val" value="14167" id="txtCaptcha" type="hidden">他们正在从隐藏的输入标签做的是,通过JavaScript,使用号码并把它们与验证码的背景下,跨度。
What they are doing is, via javascript, use numbers from that hidden input tag and put them on that span with "captcha" background.
所以basicaly你的流程是:
So basicaly your flow is:
-
阅读他们的HTML
read their html
得到验证码(笑,搞笑的CAPTCHA虽然)从输入字段值
get "captcha" (lol, funny captcha though) value from input field
当用户将数据在PNR场presses获取状态
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
解析响应
噢,还有一件事。你可以把隐藏的输入任何价值验证码输入,只要它们是相同的。它们不经由会话检查它或任何东西。
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(对于submiting形式code样品):为了简化形式张贴我建议从Apache的HttpClient的组成部分: hc.apache/downloads.cgi 比方说,你下载的HttpClient 4.3.1。包括客户端,核心和MIME在项目库(副本libs文件夹,右键单击项目,特性,Java构建路径,库,添加罐 - >添加这些3)
EDIT (code sample for submiting form): To simplify posting form i recommend HttpClient components from Apache: hc.apache/downloads.cgi Lets say you downloaded 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.).
code例子是:
private static final String FORM_TARGET = "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.
更多推荐
如何在Android中显示ImageView的验证码?
发布评论