【问题标题】:Display captcha in Jframe在 Jframe 中显示验证码
【发布时间】:2015-06-14 10:24:53
【问题描述】:

我想用 Java 创建注册申请,然后我想将信息提交到网站。这只是实验,因此注册信息(例如用户名、密码)将通过 GET 请求提交。但是,我想将验证码与注册集成,我想将其显示在 Jframe 上并将答案与其他数据一起提交。我不知道如何获取验证码图像,然后提交数据。我还想使用新的 reCaptcha(它要求你选择食物)。任何想法如何做到这一点?

编辑: 我知道如何使用 JLabel 显示图像,我还能够找到一种方法来提取它Get image for captcha session。现在我想知道如何发送响应。

【问题讨论】:

  • 您是否开始使用任何类型的代码,研究过如何在 JFrame 等中显示图像?
  • 我知道如何用 JLabel 显示图像,我也能找到一种方法来提取它stackoverflow.com/questions/17665037/…。现在我想知道如何发送响应。

标签: java swing


【解决方案1】:

要发送响应,您可能需要来自服务器的 Session ID 并且客户端会回答,然后只需向服务器发送一个带有这两个值的 GET 请求

public void getMethod() throws IOException
    {
        String userAgent = "Java/" + Runtime.class.getPackage().getImplementationVersion();
        //The server will need to know what "question" we are answering so it sent us the captha and a sesion ID
        //example is just a random one you will need to figure out how to get a sesion id
        String captchaSesionParam = "captchaSesionID=";
        String captchaSesionID = UUID.randomUUID().toString();
        //user has completed captha client side here is their answer
        String queryParam = "answer=";
        String answer = "blah blah answer";
        String urlString = "https://127.0.0.1/?" + queryParam + URLEncoder.encode(answer, "UTF-8") + "&" + captchaSesionParam + URLEncoder.encode(captchaSesionID, "UTF-8");

        URL url = new URL(urlString);
        //Open a HTTPS connection to the URL
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        //set request method to GET
        con.setRequestMethod("GET");
        //set user agent to our agent (by default I believe that this is 'Java/Version')
        con.setRequestProperty("User-Agent", userAgent);

        //print out debug info about request method and url
        System.out.println(con.getRequestMethod() + " URL : " + url);
        int responseCode = con.getResponseCode();
        System.out.println("Server response code: " + responseCode);

        try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));)
        {
            String line;
            List<String> lines = new ArrayList<String>();

            while ((line = in.readLine()) != null)
            {
                lines.add(line);
            }
            //parse lines received from server to see if the captcha was (in)correct

            //print lines for debug
            for(String l : lines)
            {
                System.out.println(l);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多