程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java模擬post請求登錄貓撲示例分享

java模擬post請求登錄貓撲示例分享

編輯:JAVA編程入門知識

代碼如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class TestPost {
 public static void main(String args[]) throws IOException{ 
  Scanner scanner = new Scanner(System.in);
  System.out.println("請輸入用戶名:");
  String user_name = scanner.next();
  System.out.println("請輸入密碼:");
  String password = scanner.next();
  testPost(user_name , password,"d:/fileDown2.txt");
  testJsoup();
 }

 public static void testPost(String user_name , String password,String outPath) throws IOException{
  String login =""; 
  URL url = new URL("http://passport.mop.com");
  HttpURLConnection connection = null;
  connection = (HttpURLConnection) url.openConnection();//建立鏈接

  connection.setInstanceFollowRedirects(false);
  connection.setRequestProperty("Connection","keep-alive");
  connection.setRequestProperty("User-Agent",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
  connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  connection.setDoInput(true);
  connection.setDoOutput(true);
//  connection.disconnect();
  String str = connection.getHeaderField("Location");//獲得重定向的url地址
  URL newURL = new URL(str); 
//  String cookies = getCookies(connection);
  HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
  conn.setRequestProperty("Referer", str);//浏覽器向 WEB 服務器表明自己是從哪個 網頁/URL 獲得/點擊 當前請求中的網址/URL
//  conn.setRequestProperty("Cookie", cookies); //發送設置cookie:
  conn.setRequestProperty("Connection","keep-alive");
  conn.setRequestProperty("User-Agent",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
  conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  conn.setDoInput(true);
  conn.setDoOutput(true);
  OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"utf-8");
  login =login+"user_name="+user_name+"&"+"password="+password;
  out.write(login);
  out.flush();
  out.close();
  InputStream inputStream = conn.getInputStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
  reader.close();
  //鏈接到personal頁面
  String headerName = null;
  StringBuilder myCookies = new StringBuilder();
//  myCookies.append(cookies + ";");
  for(int i =1;(headerName= conn.getHeaderFieldKey(i))!=null;i++){
    if(headerName.equals("Set-Cookie")){
     String cookie = conn.getHeaderField(i);
     cookie = cookie.substring(0, cookie.indexOf(";"));
     String cookieName = cookie.substring(0, cookie.indexOf("="));
     String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
     myCookies.append(cookieName + "=");
     myCookies.append(cookieValue + ";");
    }
  }
  URL purl = new URL("http://passport.mop.com/personal");
  HttpURLConnection pconn = (HttpURLConnection) purl.openConnection();
  pconn.setRequestProperty("Referer", str);
  pconn.setRequestProperty("Cookie", myCookies.toString());

  pconn.connect();
  InputStream inputStream1 = pconn.getInputStream();
//  BufferedReader reader1 = new BufferedReader(new InputStreamReader(inputStream1,"utf-8"));
//  String line1 = reader1.readLine();
//  while(line1 != null){
//   System.out.println(line1);
//   line1 = reader1.readLine();
//  }
//  reader1.close();
  int chByte = 0;
  FileOutputStream fileOut = new FileOutputStream(new File(outPath));
  chByte = inputStream1.read();
  while(chByte != -1){
   fileOut.write(chByte);
   chByte = inputStream1.read();
  }
 }
 private static String getCookies(HttpURLConnection conn) {
  // TODO Auto-generated method stub
//     StringBuffer cookies = new StringBuffer();  
     StringBuilder cookies = new StringBuilder();
           String headName;  

           for (int i = 1; (headName = conn.getHeaderField(i)) != null; i++) {  

               StringTokenizer st = new StringTokenizer(headName, "; ");  

               while (st.hasMoreTokens()) {  

                   cookies.append(st.nextToken() + "; ");  
               }  
           }             
           return cookies.toString();      
 }

 private static void testJsoup() throws IOException{
  //解析html文檔 
     File input = new File("D:/fileDown2.txt"); 
     Document doc = Jsoup.parse(input, "UTF-8");
//     for(Element ele : doc.getElementsByClass("zhnc").select("ul")){
//      if(!ele.select("li").toString().equals("")){
//       String text = ele.select("li").text();
//       System.out.println("user_name is:"+text);
//      }
//     } 
     Elements ele = doc.getElementsByClass("zhnc").select("ul");
     if(!ele.select("li").toString().equals("")){
      String text = ele.select("li").text();
      System.out.println("user_name is:"+text);
     }else{
      System.out.println("登錄失敗");
     }
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved