Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

java - Display captcha in Jframe

I want to create application for registration in Java, then I want to submit the information to a website. This is only experiment so the information for the registration(e.g username, password) will be submit with GET request. However I want to integrate captcha with the registration and I want to display it on the Jframe and submit the answer along side with the other data. I have no idea how to get the captcha image, and then submit the data. Also I think to use the new reCaptcha(where it ask you to select foods). Any ideas how to do this?

Edit: I know how to display the image with JLabel, I also was able to find a way to extract it Get image for captcha session .Now i'm wondering how to send the response.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To send a response you will probably need a Session ID from the server and the clients answer then just send a GET request to the server with both of the values

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);
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...