在Jframe

时间:2015-06-14 10:24:53

标签: java swing

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

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

1 个答案:

答案 0 :(得分:0)

要发送回复,您可能需要来自服务器的会话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);
            }
        }
    }