Java Cookie/Session - Problem - phpmyadmin
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){
S开发者_如何学Pythonystem.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);
}
}
}
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.
精彩评论