模擬https請求獲取cookie

2021-10-02 01:48:06 字數 4678 閱讀 6301

目錄前言

正文httppost傳送https請求方式

1.引入依賴包

2.重寫httpclient

3.傳送請求預設帶參,不傳參為null就行

4.附上cookie獲取**

httpsurlconnection傳送https請求方式

1.重寫x509trustmanager

2.傳送https請求

3.附上cookie獲取方法

最近專案要求訪問其他裝置頁面獲取資料,頁面多數使用https請求,用過一段時間,用過兩種不同的方式,看過其他大佬們的帖子,自己總結後,在此記錄一下。http請求變為https請求最主要的就是重寫自己的協議認證方式。

本文分為 httppost 和 httpsurlconnection 兩種方法,httpsurlconnection是jdk自帶jar包,httppost 是外部引入。

首先引入專案依賴的包,httpsurlconnection是jdk自帶的不需要引入,下面是引入httppost 依賴包。

// 將引數進行封裝,提交到伺服器端

//根據自己需求,可以換成其他方式

//根據需求獲取自己需要的cookie,我專案中需要兩個

} //我專案中有多個cookie,所以獲取多個

string setcookiea = list.get(0);

string setcookieb = list.get(1);

if(setcookiea.length()>setcookieb.length())else

}httpsurlconnection不需引入其他依賴,直接使用jdk自帶包即可(jdk1.7以上就可以,我使用的是jdk1.8)

public class myx509trustmanager implements x509trustmanager 

public void checkservertrusted(x509certificate chain, string authtype)

throws certificateexception

public x509certificate getacceptedissuers()

}

public static string testget(string url,string cookie) throws exception  ;

sslcontext.init(null, tm, new securerandom());

hostnameverifier ignorehostnameverifier = new hostnameverifier()

};url posturl = new url(url);

// 根據拼湊的url,開啟連線,url.openconnection函式會根據url的型別,

// 返回不同的urlconnection子類的物件,這裡url是乙個http,因此實際返回的是httpurlconnection

//不填寫預設是get請求

connection.setrequestmethod("post");

//post請求必須設定output,需要傳輸引數,不設定預設為false

connection.setdooutput(true);

connection.setdoinput(true);

connection.setrequestproperty("connection", "keep-alive");

//需要傳輸cookie則加上,不需要可以刪除

if(null != cookie)

// 進行連線,但是實際上get request要在下一句的connection.getinputstream()函式中才會真正發到

connection.connect();

//所有引數可以使用key=value的格式用 & 符號拼接起來,滿足通常的post和get請求

//例:

string param = "username=admin&password=admin123!";

out = new dataoutputstream(connection.getoutputstream());

out.writebytes(param);

out.flush();

// 取得輸入流,並使用reader讀取

bufferedreader reader = new bufferedreader(new inputstreamreader(

connection.getinputstream(),"utf-8"));

system.out.println("contents of get request:");

string result="";

string lines;

while ((lines = reader.readline()) != null)

system.out.println(connection.getheaderfields());

//獲取連線頭中的cookie

string cookiestr = stringhandleutil.getcookie2(connection.getheaderfields().get("set-cookie"));

reader.close();

//請求處理完之後關閉

connection.disconnect();

return result;

}

httpsurlconnection和httppost請求獲取的response不同,所以獲取的方式也有些不一樣,下面附上**

public static  string getcookie2(listlist)

return setcookiea+"; "+setcookieb;

}

curl 獲取 https 請求方法

使用curl如果想發起的https請求正常的話有2種做法 方法一 設定為不驗證證書和host 示例 url curl curl init curl setopt curl,curlopt url,url curl setopt curl,curlopt header,1 curl setopt cu...

CURL模擬手機端的HTTPS請求

curl模擬手機端需要設定使用者 為手機端的,https請求需要ssl證書或禁止驗證證書 curl curl init curl 初始化 curl setopt curl curlopt url url curl setopt curl curlopt ssl verifypeer false 禁止...

lampp下配置https,並設定cookie跨域

最近公司的專案登入要轉成https的,但是根據同源協議,https登入後的cookie與http的網頁不能共享。兩個問題,乙個是centos下https的配置,另外乙個是登入後cookie的跨域問題 問題 apache配置https 第一次配置apache,之前一直都是配的nginx,不到之處請諒解...