java模拟登陆

  1.  // 连接地址(通过阅读html源代码获得,即为登陆表单提交的URL)   
  2. String surl = "http://login.goodjobs.cn/index.php/action/UserLogin";  
  3.   
  4. /** 
  5.  * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 
  6.  * java.net.URL and //java.net.URLConnection 
  7.  */  
  8. URL url = new URL(surl);  
  9. HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  10.   
  11. /** 
  12.  * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 
  13.  * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 
  14.  */  
  15. connection.setDoOutput(true);  
  16. /** 
  17.  * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 
  18.  */  
  19. OutputStreamWriter out = new OutputStreamWriter(connection  
  20.         .getOutputStream(), "GBK");  
  21.               //其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称   
  22. out.write("memberName=myMemberName&password=myPassword"); // post的关键所在!   
  23. // remember to clean up   
  24. out.flush();  
  25. out.close();  
  26.   
  27. // 取得cookie,相当于记录了身份,供下次访问时使用   
  28. String cookieVal = connection.getHeaderField("Set-Cookie");  

      登陆成功后,即可访问其他URL了。

  1.               String s = "http://user.goodjobs.cn/dispatcher.php/module/Resume/action/Preview";  
  2. //重新打开一个连接   
  3.               url = new URL(s);  
  4. HttpURLConnection resumeConnection = (HttpURLConnection) url  
  5.         .openConnection();  
  6. if (cookieVal != null) {  
  7.                       //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限   
  8.     resumeConnection.setRequestProperty("Cookie", cookieVal);  
  9. }  
  10. resumeConnection.connect();  
  11. InputStream urlStream = resumeConnection.getInputStream();  
  12. BufferedReader bufferedReader = new BufferedReader(  
  13.         new InputStreamReader(urlStream));  
  14. String ss = null;  
  15. String total = "";  
  16. while ((ss = bufferedReader.readLine()) != null) {  
  17.     total += ss;  
  18. }  
  19. IOUtils.write(total, new FileOutputStream("d:/index.html"));  
  20. bufferedReader.close();  

       通过上述方式,就能访问带有权限控制的URL了。思路即为:模拟登陆,取得cookie以记录身份,下次请求时发送cookie以表明身份。

 

Java模拟登录微博,你可以使用网络请求库(如HttpClient或OkHttp)来发送HTTP请求,并模拟用户登录微博的过程。以下是一个简单的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class WeiboLogin { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://passport.weibo.cn/sso/login"); // 设置请求参数 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("username", "your_username")); params.add(new BasicNameValuePair("password", "your_password")); params.add(new BasicNameValuePair("savestate", "1")); params.add(new BasicNameValuePair("r", "https://m.weibo.cn/")); params.add(new BasicNameValuePair("ec", "0")); params.add(new BasicNameValuePair("pagerefer", "https://m.weibo.cn/")); params.add(new BasicNameValuePair("entry", "mweibo")); params.add(new BasicNameValuePair("wentry", "")); params.add(new BasicNameValuePair("loginfrom", "")); params.add(new BasicNameValuePair("client_id", "")); params.add(new BasicNameValuePair("code", "")); params.add(new BasicNameValuePair("qq", "")); params.add(new BasicNameValuePair("mainpageflag", "1")); params.add(new BasicNameValuePair("hff", "")); params.add(new BasicNameValuePair("hfp", "")); try { httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); // 发送登录请求 HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); // 处理登录结果 if (entity != null) { String result = EntityUtils.toString(entity); System.out.println(result); // 在这里可以解析返回的结果,判断登录是否成功 } } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个简单的示例代码,实际上模拟登录微博可能要更多的参数和步骤。另外,模拟登录涉及到网络请求和用户隐私,要遵守相关法律法规和网站的使用规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值