我试图登录" Udemy.com"使用HttpUrlConnection库以及我已经完成的工作。
我得到了#34; 200"发送POST后的响应代码,但是当我打开从响应中获得的HTML时,它并没有显示我登录页面。几天来我一直在弄乱标题属性,但是没有意识到什么是错的。
public final String urlGet = "https://www.udemy.com/join/login-popup/?displayType=ajax&display_"
+ "type=popup&showSkipButton=1&returnUrlAfterLogin=https%3A%2F%2Fwww.udemy.com%2F&next"
+ "=https%3A%2F%2Fwww.udemy.com%2F&locale=pt_BR";
public final String urlPost = "https://www.udemy.com/join/login-popup/?displayType=ajax&display_type"
+ "=popup&showSkipButton=1&returnUrlAfterLogin=https%3A%2F%2Fwww.udemy.com%"
+ "2F&next=https%3A%2F%2Fwww.udemy.com%2F&locale=pt_BR";
public final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36";
public static void main(String[] args) throws IOException {
UdemyCoupon uc = new UdemyCoupon();
// uc.getCouponEachPage();
// uc.readArrayList();
uc.sendGetLogin();
uc.sendPostLogin();
// uc.sendGetApplyCoupon();
}
Date date = new Date();
private ArrayList<String> urls = new ArrayList<>();
private List<String> cookies;
private String cookiesArray[] = new String[100];
private String cookieTotal = "";
public String[] sendGetLogin() throws MalformedURLException, IOException {
HttpsURLConnection urlConnection
= (HttpsURLConnection) new URL(urlGet)
.openConnection();
cookies = urlConnection.getHeaderFields().get("Set-Cookie");
for (int i = 0; i < cookies.size(); i++) {
if (cookies.get(i).contains(";")) {
cookiesArray[i] = cookies.get(i).split(";")[0];
System.out.println(i + " - " + cookiesArray[i]);
}
}
return cookiesArray;
}
public int sendPostLogin() throws IOException {
URL obj = new URL(urlPost);
System.out.println("\nConnecting ...");
HttpsURLConnection con;
con = (HttpsURLConnection) obj.openConnection();
int posTokenArray = 0;
for (int i = 0; i < cookies.size(); i++) {
cookieTotal += cookiesArray[i];
if (i < cookies.size() - 1) {
cookieTotal += "; ";
}
if (cookiesArray[i].contains("csrftoken")) {
posTokenArray = i;
}
}
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
con.setRequestProperty("Accept-Encoding", "gzip, deflate");
con.setRequestProperty("Accept-Language", "pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4");
con.setRequestProperty("Cache-Control", "max-age=0");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Content-Length", "128");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Cookie", cookieTotal);
con.setRequestProperty("Host", "www.udemy.com");
con.setRequestProperty("Origin", "https://www.udemy.com");
con.setRequestProperty("Referer", "https://www.udemy.com/join/login-popup/?displayType=ajax&display_type=popup&showSkipButton=1&returnUrlAfterLogin=https%3A%2F%2Fwww.udemy.com%2F&next=https%3A%2F%2Fwww.udemy.com%2F&locale=pt_BR");
con.setRequestProperty("Upgrade-Insecure-Requests", "1");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setDoOutput(true);
con.setDoInput(true);
String postParams = "csrfmiddlewaretoken="+ cookiesArray[posTokenArray].split("=")[1] +"&locale=pt_BR&email="+URLEncoder.encode("testudemy@hmamail.com", "UTF-8")+
"&password=" + URLEncoder.encode("testpass", "UTF-8")+"&submit=Acessar";
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
System.out.println("\nSending 'POST' request to URL : " + urlPost);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + con.getResponseCode());
System.out.println("Cookies : " + cookieTotal);
System.out.println("csrfmiddlewaretoken " + cookiesArray[posTokenArray].split("=")[1]);
BufferedReader in
= new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println(response);
in.close();
con.disconnect();
if (response.toString().contains("Por favor, verifique seu e-mail e sua senha.")) {
System.out.println("Incorrect email or password!");
return 0;
} else if (response.toString().contains("nforme um endereço de email válido")) {
System.out.println("Email syntax invalid!");
return 0;
} else {
System.out.println("You're in!");
return 1;
}
}
感谢任何帮助,谢谢。