Java Cookie/会话 - 问题 - phpmyadmin

发布于 2024-11-17 12:15:27 字数 1898 浏览 1 评论 0原文

大家好,我想编写一个与 phpmyadmin 交互的工具。我想创建一个新表。为此,我需要 cookie 和安全令牌。这两件事我都做过。我的问题是我将获取 cookie 并使用这些 Cookie 打开一个新的 URLConnection。并获取令牌来验证我的请求。但每次我这样做时,我都会得到我的 SQLQuery 为空的响应,如果你收到此错误,则你的令牌无效。无效的令牌意味着您的 cookie 没有很好地放置在新连接中,因此您没有与以前相同的会话。我做错了什么,有办法解决这个问题吗?

这是我的代码:(它非常丑陋,但仅用于测试目的)

import java.io.*;
import java.net.*;
import java.util.List;

public class connect {

 public void connectto() throws IOException{
     URLConnection connection = new URL("http://localhost/phpmyadmin/index.php").openConnection();
     List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
     connection.connect();
     InputStream response = connection.getInputStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(response));
     String line;
     String token = "";
     while((line = br.readLine())!= null){
         System.out.println(line);
     if (line.contains("var token = ")){
         System.out.println("hit");
          token = "&token=" + line.substring(line.indexOf("var token = '") + "var token = '".length()).substring(0, line.substring(line.indexOf("var token = '") + "var token = '".length()).indexOf("';"));
     }

     }
     System.out.println(token);

     String url = URLEncoder.encode("db=mysql&sql_query=CREATE TABLE testtable(testtable TEXT);" + token, "UTF-8");


     connection = new URL("http://localhost/phpmyadmin/sql.php?" + url).openConnection();
     connection.setDoOutput(true);
     for (String cookie : cookies) {
         System.out.println(cookie.split(";", 2)[0]);
         connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
     }
     connection.connect();
     response = connection.getInputStream();
     br = new BufferedReader(new InputStreamReader(response));
     line = "";
     while((line = br.readLine())!= null){
     System.out.println(line);
     }

     }
 }

hi guys i want to code a tool, which interacts with phpmyadmin. I want to create a new Table. For this i need the cookie and the security token. Both things I've done. My Problem is i'll take the cookies and open an new URLConnection with these Cookies. And take the token to validate my request. But everytime i do this i got the response that my SQLQuery is empty and if u get this Error ur token is invalid. And an invalid token means that ur cookies haven't been placed very well in the new connection so u don't have the same session as before. What i've done wrong, any idea to fix this problem?

Here is my code:(its very ugly but its only for testing purposes)

import java.io.*;
import java.net.*;
import java.util.List;

public class connect {

 public void connectto() throws IOException{
     URLConnection connection = new URL("http://localhost/phpmyadmin/index.php").openConnection();
     List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
     connection.connect();
     InputStream response = connection.getInputStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(response));
     String line;
     String token = "";
     while((line = br.readLine())!= null){
         System.out.println(line);
     if (line.contains("var token = ")){
         System.out.println("hit");
          token = "&token=" + line.substring(line.indexOf("var token = '") + "var token = '".length()).substring(0, line.substring(line.indexOf("var token = '") + "var token = '".length()).indexOf("';"));
     }

     }
     System.out.println(token);

     String url = URLEncoder.encode("db=mysql&sql_query=CREATE TABLE testtable(testtable TEXT);" + token, "UTF-8");


     connection = new URL("http://localhost/phpmyadmin/sql.php?" + url).openConnection();
     connection.setDoOutput(true);
     for (String cookie : cookies) {
         System.out.println(cookie.split(";", 2)[0]);
         connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
     }
     connection.connect();
     response = connection.getInputStream();
     br = new BufferedReader(new InputStreamReader(response));
     line = "";
     while((line = br.readLine())!= null){
     System.out.println(line);
     }

     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-11-24 12:15:28

如果我误解了您的问题,我很抱歉,但是您为什么要通过 phpMyAdmin 强制请求? phpMyAdmin 的目的是为人们提供一个 UI 来使用他们的 MySQL 数据库。如果您想与 MySQL 数据库交互,您应该直接打开与数据库的连接并在 java 代码中执行语句。

再次,如果这对您来说不可行,请原谅我的无知,但如果您必须通过 phpMyAdmin 而不是 Java 和数据库之间的直接连接,请提供更多信息。

My apologies if I misunderstood your issue but why are you forcing the request through phpMyAdmin? The point of phpMyAdmin is to provide a UI to people to work with their MySQL database. If you want to interact with the MySQL database you should be opening a connection directly to the database and executing statements in the java code.

Again, please excuse my ignorance if this is unfeasable for you but if you must work through phpMyAdmin instead of a direct connection between Java and your DB please provide more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文