我尝试将POST数据发送到网站。但是"登录"不管用。 慢慢地我不知道为什么。没有browser-ui,有很多方法可以登录网站。什么是最好的"在这里登录的方法? 我使用Jsoup和"普通" HttpURLConnection类。使用Selenium它工作正常,但非常慢=(
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.List;
import java.util.Map;
public class postget {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "https://de.metin2.gameforge.com/";
private static final String POST_URL = "https://de.metin2.gameforge.com:443/user/login?__token=";//+token
private static final String POST_PARAMS = "username=USERNAME"+"password=PASSWORD";
static final String COOKIES_HEADER = "Set-Cookie";
public static void main(String[] args) throws IOException {
String var = sendGET();
String var1 =var.substring(0,var.indexOf(";"));
String var2 =var.substring(var.indexOf(";")+1,var.indexOf(":"));
String token =var.substring(var.indexOf(":")+1);
//sendPOST(token,cookie);
jsoup(cookie(),token);
}
private static String sendGET() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String veri1=response.substring(response.indexOf("verify-v1")+20,response.indexOf("verify-v1")+63);
String veri2=response.substring(response.indexOf("verify-v1")+104,response.indexOf("verify-v1")+147);
String token=response.substring(response.indexOf("token")+6,response.indexOf("token")+38);
return (veri1+";"+veri2+":"+token);
} else {
System.out.println("GET request not worked");
return " ";
}
}
private static String cookie() throws IOException{
String realCookie="";
URL obj = new URL(GET_URL);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
java.net.CookieManager msCookieManager = new java.net.CookieManager();
Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
}
}
List<HttpCookie> cookiess = msCookieManager.getCookieStore().getCookies();
if (cookiess != null) {
if (cookiess.size() > 0) {
for (HttpCookie cookie : cookiess) {
realCookie = cookie.toString().substring(cookie.toString().indexOf("=")+1);
return(realCookie);
}
}
}
return(realCookie);
}
private static void sendPOST(String token,CookieManager msCookieManager) throws IOException {
URL obj = new URL(POST_URL+token);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
if (msCookieManager != null) {
List<HttpCookie> cookies = msCookieManager.getCookieStore().getCookies();
if (cookies != null) {
if (cookies.size() > 0) {
for (HttpCookie cookie : cookies) {
String realCookie = cookie.toString().substring(cookie.toString().indexOf("=")+1);
}
con.setRequestProperty("Cookie", StringUtils.join(cookies, ";"));
}
}
}
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
if(response.indexOf("form-login")>0){
System.out.println("NOPE");
}
if(response.indexOf("logout")>0){
System.out.println("YES");
}
} else {
System.out.println("POST request not worked");
}
}
private static void jsoup(String cookie,String token) throws IOException{
Document response = Jsoup.connect("https://de.metin2.gameforge.com/")
.referrer("https://de.metin2.gameforge.com/main/index?__token=5"+token)
.cookie("Cookie","gf-locale=de_DE; _ga=GA1.2.404625572.1501231885; __auc=a02de56115d8864ad2bd7ad8120; pc_idt=ANu-cNegKMzU8CAsBQAHIu1cRlXEEUD1ka6NvJXWqO4sVVaLIsqSFPyZFt8dXHKcrhrB_u2FFdQnD-2vsA377NnVgjkrKn-3qzi5Q3LXbzgnbbmIEir4zYNCddPbjCUg9cVpSU4GP-CvU53XhrQ6_MWP9tOYNdjCqRVIPw; SID="+cookie+"; __utma=96667401.404625572.1501231885.1503225538.1503232069.15; __utmc=96667401; __utmz=96667401.1501247623.3.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)")
//.data("X-DevTools-Emulate-Network-Conditions-Client-Id","c9bbb769-df80-47ff-8e25-e582de026ecc")
.userAgent("Mozilla")
.data("username", "USERNAME")
.data("password", "PASSWORD")
.post();
System.out.println(response);
}
}
}
var1和var1是不必要的。 他是我第一个用POST发送的想法。