我试图登录到一个网站与一个程序。我谷歌了很多,但其他人的方式不知怎么做对我不起作用。
登录到网站的步骤如下:
通过请求auth.php获得一个PHPSESSID 发送带有PHPSESSID作为cookie +用户名和密码的内容的post请求
在整个过程中,我使用了打嗝套件来查看发送和接收的数据包。
获得PHPSESSID非常直接,设置Cookie并发送post请求也不是那么困难,但响应并不相同。
String site = "The Site(Https://....)";
URL myUrl = new URL(site);
HttpURLConnection con = (HttpURLConnection) myUrl.openConnection();
con.setRequestMethod("GET");
Map<String, List<String>> headers = con.getHeaderFields();
//Get the PHPSESSID
for(Entry<String, List<String>> e : headers.entrySet()){
System.out.println(e.getKey() + ":" + e.getValue());
}
List<String> ff = headers.get("Set-Cookie");
String asd = ff.get(0);
String[] temp = asd.split(";");
asd = temp[0];
System.out.println(asd);
con.disconnect();
myUrl = new URL(site);
con = null;
con = (HttpURLConnection) myUrl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Host", "The host");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml");
con.setRequestProperty("Accept-Language", "de,en-US;q=0.7,en;q=0.3");
con.setRequestProperty("Referer", "https://ericsson.mareksokol.info/auth.php");
con.setRequestProperty("Connection", "close");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Cookie", asd);
con.setRequestProperty("Content-Length", "" +login.length());
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream (
con.getOutputStream());
wr.writeBytes(login); //login is just copy&pasted from burp-suite
wr.close();
System.out.println("\n");
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); //
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response);这是我第一次与一个网站工作,并登录到它,我真的很感谢你能给我任何帮助。
发布于 2016-08-14 22:28:36
我建议您为Java使用一个Httpclient库: Authentication、OKHttp等。如果您想使用普通的Connection对象(请注意此示例用于平原身份验证方案),您可以这样做:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpBasicAuth {
public static void main(String[] args) {
try {
URL url = new URL ("http://ip:port/login");
String encoding = Base64Encoder.encode ("test1:test1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}https://stackoverflow.com/questions/38947146
复制相似问题