java cookie模拟登陆
时间: 2023-10-14 16:28:48 浏览: 70
以下是一个使用Java模拟登陆的示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
public class LoginDemo {
public static void main(String[] args) throws Exception {
String loginUrl = "http://example.com/login";
String username = "your_username";
String password = "your_password";
// Create a new URL object
URL url = new URL(loginUrl);
// Create a new HttpURLConnection object
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set the request method to POST
connection.setRequestMethod("POST");
// Set the request headers
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Set the request parameters
String params = "username=" + URLEncoder.encode(username, "UTF-8")
+ "&password=" + URLEncoder.encode(password, "UTF-8");
connection.setDoOutput(true);
connection.getOutputStream().write(params.getBytes("UTF-8"));
// Send the request
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// Read the response
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Print the response
System.out.println(response.toString());
// Get the cookies from the response headers
Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> cookiesHeader = headerFields.get("Set-Cookie");
if (cookiesHeader != null) {
// Parse the cookies and store them in a CookieManager object
java.net.CookieManager cookieManager = new java.net.CookieManager();
for (String cookie : cookiesHeader) {
cookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
}
// Use the CookieManager to send subsequent requests with the cookies
URL newUrl = new URL("http://example.com/protected_page");
HttpURLConnection newConnection = (HttpURLConnection) newUrl.openConnection();
cookieManager.getCookieStore().getCookies().forEach(c -> newConnection.addRequestProperty("Cookie", c.toString()));
int newResponseCode = newConnection.getResponseCode();
if (newResponseCode == HttpURLConnection.HTTP_OK) {
// Read the response
BufferedReader newIn = new BufferedReader(new InputStreamReader(newConnection.getInputStream()));
String newInputLine;
StringBuffer newResponse = new StringBuffer();
while ((newInputLine = newIn.readLine()) != null) {
newResponse.append(newInputLine);
}
newIn.close();
// Print the response
System.out.println(newResponse.toString());
} else {
System.out.println("Failed to fetch protected page");
}
}
} else {
System.out.println("Failed to login");
}
}
}
```
此代码将使用POST请求发送用户名和密码到登录页面。如果登录成功,它将获取响应标头中的cookie,并将其存储在CookieManager对象中。然后,它将使用CookieManager对象向受保护的页面发送新请求,并在请求中添加所有cookie。如果此请求成功,它将打印受保护页面的内容。
阅读全文