java模擬post請(qǐng)求登錄貓撲示例分享
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("請(qǐng)輸入用戶名:");
String user_name = scanner.next();
System.out.println("請(qǐng)輸入密碼:");
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 服務(wù)器表明自己是從哪個(gè) 網(wǎng)頁/URL 獲得/點(diǎn)擊 當(dāng)前請(qǐng)求中的網(wǎng)址/URL
// conn.setRequestProperty("Cookie", cookies); //發(fā)送設(shè)置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("登錄失敗");
}
}
}
- java登錄驗(yàn)證碼實(shí)現(xiàn)代碼
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- 第三方網(wǎng)站微信登錄java代碼實(shí)現(xiàn)
- JSP+Servlet制作Java Web登錄功能的全流程解析
- java中servlet實(shí)現(xiàn)登錄驗(yàn)證的方法
- Java web過濾器驗(yàn)證登錄防止未登錄進(jìn)入界面
- java實(shí)現(xiàn) 微博登錄、微信登錄、qq登錄實(shí)現(xiàn)代碼
- java中使用Filter控制用戶登錄權(quán)限具體實(shí)例
- java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼
- java實(shí)現(xiàn)登錄案例
相關(guān)文章
Java輕量級(jí)權(quán)限認(rèn)證框架Sa-Token的使用
Sa-Token是一個(gè)輕量級(jí)Java權(quán)限認(rèn)證框架,本文就詳細(xì)的來介紹一下Java輕量級(jí)權(quán)限認(rèn)證框架Sa-Token的使用,主要解決:登錄認(rèn)證、權(quán)限認(rèn)證、Session會(huì)話、單點(diǎn)登錄、OAuth2.0、微服務(wù)網(wǎng)關(guān)鑒權(quán)等,感興趣的可以了解一下2022-03-03springboot多文件上傳實(shí)現(xiàn)使用postman測(cè)試多文件上傳接口
這篇文章主要介紹了springboot多文件上傳實(shí)現(xiàn)使用postman測(cè)試多文件上傳接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車、迅雷)
這篇文章主要介紹了Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法,涉及Java文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11使用Spring Data JDBC實(shí)現(xiàn)DDD聚合的示例代碼
這篇文章主要介紹了使用Spring Data JDBC實(shí)現(xiàn)DDD聚合的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09Java8 將List轉(zhuǎn)換為用逗號(hào)隔開的字符串的多種方法
這篇文章主要介紹了Java8 將List轉(zhuǎn)換為用逗號(hào)隔開的字符串的幾種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Java多線程編程之訪問共享對(duì)象和數(shù)據(jù)的方法
這篇文章主要介紹了Java多線程編程之訪問共享對(duì)象和數(shù)據(jù)的方法,多個(gè)線程訪問共享對(duì)象和數(shù)據(jù)的方式有兩種情況,本文分別給出代碼實(shí)例,需要的朋友可以參考下2015-05-05SpringCloud微服務(wù)之Hystrix組件實(shí)現(xiàn)服務(wù)熔斷的方法
微服務(wù)架構(gòu)特點(diǎn)就是多服務(wù),多數(shù)據(jù)源,支撐系統(tǒng)應(yīng)用。這樣導(dǎo)致微服務(wù)之間存在依賴關(guān)系。這篇文章主要介紹了SpringCloud微服務(wù)之Hystrix組件實(shí)現(xiàn)服務(wù)熔斷的方法,需要的朋友可以參考下2019-08-08spring-spring容器中bean知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于spring-spring容器中bean知識(shí)點(diǎn)總結(jié),有需要的朋友們可以學(xué)習(xí)下。2019-08-08SpringMVC配置攔截器實(shí)現(xiàn)登錄控制的方法
這篇文章主要介紹了SpringMVC配置攔截器實(shí)現(xiàn)登錄控制的方法,SpringMVC讀取Cookie判斷用戶是否登錄,對(duì)每一個(gè)action都要進(jìn)行判斷,有興趣的可以了解一下。2017-03-03