[英]Logging onto a website through java
I try to scape some data from a website named alphadraft.com. 我尝试从名为alphadraft.com的网站获取一些数据。 Unfortunately, I need to authenticate myself to access that data. 不幸的是,我需要对自己进行身份验证才能访问该数据。 After searching around for a while I learned that I needed to send a POST request to the website that logs me in.Trough the Chrome DevTools I learned that this should be the should be the post request that logs me in. 经过一段时间的搜索后,我得知我需要向登录我的网站发送POST请求。通过Chrome DevTools,我了解到这应该是登录我的发布请求。
After searching around how to do that, I stumbled on this answer and decided to replicate for my purposes. 在研究了如何做之后,我偶然发现了这个答案,并决定出于我的目的进行复制。 How to send Request payload to REST API in java? 如何在Java中将请求有效负载发送到REST API?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.io.OutputStreamWriter;
public class testasdf {
public static void main(String[] args) throws IOException, NumberFormatException {
String line;
StringBuffer jsonString = new StringBuffer();
try {
URL url = new URL("https://alphadraft.com/api/ui/auth/loginuser/");
//escape the double quotes in json string
String payload="{email_address : \"(EMAILHERE)\", password : \"(PASSWORDHERE)\"}";
System.out.println(payload);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("content-type", "text/plain;charset=UTF-8");
connection.setRequestProperty("user-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
System.out.println(jsonString.toString());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
URL url1 = new URL("https://alphadraft.com/api/ui/contest/getloadinfo/");
URLConnection con1 = url1.openConnection();
con1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36");
con1.connect();
InputStream is1 =con1.getInputStream();
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
System.out.println(br1.readLine());
}
}
Yet, the error message 但是,错误消息
{"error": "Failed to login"} {"error": "Not Authenticated"} {“错误”:“无法登录”} {“错误”:“未验证”}
still pops up for both my outputs. 仍然弹出我的两个输出。 I would be really grateful if anyone could explain me why this doesn't work. 如果有人能向我解释为什么这行不通,我将不胜感激。
JSON格式的引号引号既标记了属性名称,也标记了该值,因此在您的情况下应为: String payload="{\\"email_address\\" : \\"(EMAILHERE)\\", \\"password\\" : \\"(PASSWORDHERE)\\"}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.