使用cookie模拟登陆豆瓣影评

1、先登录自己的豆瓣号,按F12会出现下面的开发者工具在这里插入图片描述2、然后点击Network,再Ctrl+R,,可以看到有很多数据,比如下面这张图,包含有状态码,请求内型等,太多了,我们只需要找到含有我们自己用户的cookie值就好,豆瓣这个还是很好找,我的cooike就放在www.douban.com这个文件里,双击它,翻到Request eaders,就可以看到自己的cooike值,将它复制下来(每个网站不一样,需要耐心仔细的找,包含的参数啊等等)
在这里插入图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用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。如果此请求成功,它将打印受保护页面的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值