Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying Captcha in a Swing panel
    primarykey
    data
    text
    <p>I've been trying to display a captcha for user input in a swing panel and then post the captcha text alongside with another information in order to access some public information about a company (in Brazil). BTW, I don't want to make a robot and neither to break/crack the captcha, I just wan't to avoid browsing and then fetch and register some information from the internet on my db. </p> <p>So far I've managed to display the captcha, but I can't understand why I'm not succeeding when posting. </p> <p>here is the page's link: <a href="http://www.receita.fazenda.gov.br/pessoaJuridica/CNPJ/CNPJREVA/CNPJREVA_solicitacao2.asp" rel="nofollow">http://www.receita.fazenda.gov.br/pessoaJuridica/CNPJ/CNPJREVA/CNPJREVA_solicitacao2.asp</a></p> <p>The form is basically the following:</p> <pre><code>&lt;form id="theForm" action="" onSubmit="javascript:return Submeter();" method="post" name="frmConsulta"&gt; &lt;input type="hidden" name="origem" value="comprovante"&gt; &lt;input tabIndex="1" name="cnpj" maxlength="14" size="16" onKeyup="SaltaCampo(document.frmConsulta.cnpj, document.frmConsulta.chave, 14, event)"value=""&gt; &lt;input type="text" tabIndex="2" id="idLetra" name="idLetra" maxlength="4" size="6"&gt; &lt;input type="text" id="idSom" name="idSom" size="7" maxlength="6" tabindex="4"&gt; &lt;input type="submit" value="Consultar" id=submit1 name=submit1&gt; &lt;input type="hidden" name="search_type" value="cnpj"&gt; &lt;input type="reset" name="opcao" value="Limpar"&gt; &lt;/form&gt; </code></pre> <p>The onSubmit javascript function is:</p> <pre><code>function Submeter() { document.cookie = 'flag=1'; if (validaCaracteresCaptcha('theForm', 'idLetra', 'idSom', 'valida.asp') == false) { return false; } } </code></pre> <p>the validaCaracteresCaptcha function is:</p> <pre><code>function validaCaracteresCaptcha(nomeForm, idLetra, idSom, paginaDestino) { var form = document.getElementById(nomeForm); if (document.getElementById(idLetra).value == "" &amp;&amp; document.getElementById(idSom).value == "") { AlertaCaptcha("Favor preencher algum dos campos de validação"); form.action= ""; return false; } if (document.getElementById(idLetra).value != "" &amp;&amp; document.getElementById(idSom).value != "") { AlertaCaptcha("Favor preencher apenas um dos campos de validação"); form.action=""; return false; } form.action=paginaDestino; return true; } </code></pre> <p>and the two functions I'm using to get the captcha and then post the information (java):</p> <pre><code>protected void getCaptcha() { try { URL url = new URL ("http://www.receita.fazenda.gov.br/pessoaJuridica/CNPJ/CNPJREVA/valida.asp"); // URL connection channel. urlConn = (HttpURLConnection) url.openConnection(); URL cUrl = new URL("http://www.receita.fazenda.gov.br/scripts/srf/intercepta/captcha.aspx?opt=image"); lblCaptcha.setIcon(new ImageIcon(cUrl)); // Let the run-time system (RTS) know that we want input. urlConn.setDoInput (true); // Let the RTS know that we want to do output. urlConn.setDoOutput (true); // No caching, we want the real thing. urlConn.setUseCaches (false); // Specify the content type. urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConn.setRequestMethod("POST"); urlConn.setConnectTimeout(0); urlConn.connect(); // } catch(Exception e) { e.printStackTrace(); } } private void postIt() { try { // Send POST output. DataOutputStream printout = new DataOutputStream (urlConn.getOutputStream ()); String content = "origem=comprovante&amp;cnpj="+txtCnpj.getText()+"&amp;idSom="+txtCaptcha.getText()+"&amp;search_type=cnpj"; printout.writeBytes (content); printout.flush (); printout.close (); // Get response data. if(urlConn.getResponseCode()==HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); String str = ""; while((str = reader.readLine()) != null) { System.out.println(str); } reader.close (); } else System.out.println("Answer not received"); } catch(Exception e) { e.printStackTrace(); } } </code></pre> <p>and that's it. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload