java模拟登录获取cookies
时间: 2023-10-18 17:18:24 浏览: 38
要模拟登录获取Cookies,需要使用Java中的HttpURLConnection和CookieManager类。
首先,创建一个CookieManager对象来存储Cookies:
```
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
```
然后,创建一个HttpURLConnection对象并设置请求方法和请求头:
```
URL url = new URL("登录页面URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
conn.setDoOutput(true);
```
接下来,将登录表单中的用户名和密码拼接成字符串,并写入输出流中:
```
String username = "your_username";
String password = "your_password";
String postData = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");
OutputStream os = conn.getOutputStream();
os.write(postData.getBytes());
os.flush();
os.close();
```
最后,获取响应码和响应内容,并从CookieManager中获取Cookies:
```
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
for (HttpCookie cookie : cookies) {
System.out.println(cookie.getName() + "=" + cookie.getValue());
}
}
```
完整代码示例:
```
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.net.HttpCookie;
public class LoginDemo {
public static void main(String[] args) throws Exception {
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
URL url = new URL("登录页面URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
conn.setDoOutput(true);
String username = "your_username";
String password = "your_password";
String postData = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");
OutputStream os = conn.getOutputStream();
os.write(postData.getBytes());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
for (HttpCookie cookie : cookies) {
System.out.println(cookie.getName() + "=" + cookie.getValue());
}
}
}
}
```