Java 在Jframe中显示验证码
我想用Java创建注册应用程序,然后我想将信息提交到一个网站。这只是一个实验,因此注册信息(例如用户名、密码)将随GET请求一起提交。然而,我想将captcha与注册集成在一起,我想将其显示在Jframe上,并与其他数据一起提交答案。我不知道如何获得验证码图像,然后提交数据。我还想使用新的reCaptcha(它要求你选择食物)。有什么办法吗 编辑:Java 在Jframe中显示验证码,java,swing,Java,Swing,我想用Java创建注册应用程序,然后我想将信息提交到一个网站。这只是一个实验,因此注册信息(例如用户名、密码)将随GET请求一起提交。然而,我想将captcha与注册集成在一起,我想将其显示在Jframe上,并与其他数据一起提交答案。我不知道如何获得验证码图像,然后提交数据。我还想使用新的reCaptcha(它要求你选择食物)。有什么办法吗 编辑: 我知道如何使用JLabel显示图像,我也找到了提取图像的方法。现在我想知道如何发送响应。要发送响应,您可能需要服务器提供一个会话ID,客户端回答,然
我知道如何使用JLabel显示图像,我也找到了提取图像的方法。现在我想知道如何发送响应。要发送响应,您可能需要服务器提供一个会话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);
}
}
}
public void getMethod()引发IOException
{
字符串userAgent=“Java/”+Runtime.class.getPackage().getImplementationVersion();
//服务器需要知道我们在回答什么“问题”,所以它会向我们发送captha和sesion ID
//这个示例只是一个随机示例,您需要弄清楚如何获取sesion id
字符串captchaSesionParam=“captchaSesionID=”;
字符串captchaSesionID=UUID.randomUUID().toString();
//用户已完成captha客户端,以下是他们的答案
字符串queryParam=“answer=”;
String-answer=“诸如此类的回答”;
字符串URL字符串=”https://127.0.0.1/?“+queryParam+URLEncoder.encode(回答,“UTF-8”)+”&“+captchaSesionParam+URLEncoder.encode(captchaSesionID,“UTF-8”);
URL=新URL(URL字符串);
//打开到URL的HTTPS连接
HttpURLConnection con=(HttpURLConnection)url.openConnection();
//设置要获取的请求方法
con.setRequestMethod(“GET”);
//将用户代理设置为我们的代理(默认情况下,我认为这是“Java/Version”)
con.setRequestProperty(“用户代理”,userAgent);
//打印出关于请求方法和url的调试信息
System.out.println(con.getRequestMethod()+“URL:”+URL);
int responseCode=con.getResponseCode();
System.out.println(“服务器响应代码:“+responseCode”);
try(BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream(),“UTF-8”);)
{
弦线;
列表行=新的ArrayList();
而((line=in.readLine())!=null)
{
行。添加(行);
}
//分析从服务器接收到的行,以查看验证码是否正确
//用于调试的打印行
用于(字符串l:行)
{
系统输出打印LN(l);
}
}
}
您是否已经开始使用任何类型的代码,研究如何在JFrame ect中显示图像?我知道如何使用JLabel显示图像,我还能够找到提取图像的方法。现在我想知道如何发送响应。