java HttpURLConnection 登录网站 完整代码

 java httpurlconnection 登录网站 完整代码

  1. import java.io.*;   
  2. import java.util.*;   
  3. import java.net.*;   
  4.   
  5. public class WebTest {   
  6.   
  7.     public static void main(String[] args) {   
  8.   
  9.         System.out.println("beging...");   
  10.         DownLoadPages("http://login.xiaonei.com/Login.do""d:/fileDown.txt");   
  11.         // visit("http://www.xiaonei.com");   
  12.         System.out.println("end.");   
  13.     }   
  14.   
  15.     public static void DownLoadPages(String urlStr, String outPath) {   
  16.         int chByte = 0;   
  17.   
  18.         URL url = null;   
  19.   
  20.         HttpURLConnection httpConn = null;   
  21.   
  22.         InputStream in = null;   
  23.   
  24.         FileOutputStream out = null;   
  25.   
  26.         try {   
  27.             String post = "email=" + URLEncoder.encode("e-mail""UTF-8")   
  28.                     + "&password=" + "password";   
  29.             url = new URL(urlStr);   
  30.   
  31.             httpConn = (HttpURLConnection) url.openConnection();   
  32.   
  33.             //setInstanceFollowRedirects can then be used to set if    
  34.             //redirects should be followed or not and this should be used before the   
  35.             //connection is established (via getInputStream, getResponseCode, and other   
  36.             //methods that result in the connection being established).   
  37.   
  38.             httpConn.setFollowRedirects(false);   
  39.   
  40.             //inorder to disable the redirects   
  41.             httpConn.setInstanceFollowRedirects(false);   
  42.   
  43.             httpConn.setDoOutput(true);   
  44.             httpConn.setDoInput(true);   
  45.             httpConn.setRequestProperty("User-Agent",   
  46.                     "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");   
  47.             httpConn.setRequestProperty("Content-Type",   
  48.                     "application/x-www-form-urlencoded");   
  49.   
  50.             //ok now, we can post it   
  51.             PrintStream send = new PrintStream(httpConn.getOutputStream());   
  52.             send.print(post);   
  53.             send.close();   
  54.             URL newURL = new URL(httpConn.getHeaderField("Location"));   
  55.             System.out.println("the URL has move to "  
  56.                     + httpConn.getHeaderField("Location"));   
  57.             httpConn.disconnect();   
  58.   
  59.             //           OK, now we are ready to get the cookies out of the URLConnection      
  60.             String cookies = getCookies(httpConn);   
  61.             System.out.println(cookies);   
  62.             httpConn = (HttpURLConnection) newURL.openConnection();   
  63.             httpConn.setRequestProperty("User-Agent",   
  64.                     "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");   
  65.             httpConn.setRequestProperty("Content-Type",   
  66.                     "application/x-www-form-urlencoded");   
  67.             httpConn.setRequestProperty("Cookie", cookies);   
  68.   
  69.             httpConn.setDoInput(true);   
  70.             in = httpConn.getInputStream();   
  71.             out = new FileOutputStream(new File(outPath));   
  72.   
  73.             chByte = in.read();   
  74.             while (chByte != -1) {   
  75.                 out.write(chByte);   
  76.                 //System.out.println(chByte);   
  77.                 chByte = in.read();   
  78.             }   
  79.         } catch (Exception e) {   
  80.             e.printStackTrace();   
  81.         }   
  82.     }   
  83.   
  84.     public static String getCookies(HttpURLConnection conn) {   
  85.         StringBuffer cookies = new StringBuffer();   
  86.         String headName;   
  87.         for (int i = 7; (headName = conn.getHeaderField(i)) != null; i++) {   
  88.             StringTokenizer st = new StringTokenizer(headName, "; ");   
  89.             while (st.hasMoreTokens()) {   
  90.                 cookies.append(st.nextToken() + "; ");   
  91.             }   
  92.         }   
  93.         return cookies.toString();   
  94.     }   
  95. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 HttpURLConnection 来模拟登录网站。下面是一个示例代码: ``` import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class LoginExample { private static final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { String url = "http://example.com/login"; String username = "your_username"; String password = "your_password"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为 POST con.setRequestMethod("POST"); // 设置请求头 con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 构造请求参数 String urlParameters = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); // 发送 POST 请求 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 打印响应结果 System.out.println(response.toString()); } } ``` 在上面的示例代码中,我们使用 `HttpURLConnection` 来发送 POST 请求,并且在请求中添加了用户名和密码。如果登录成功,服务器会返回响应结果。你可以根据实际情况修改代码中的 url、username 和 password 参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值