HttpClient---cookie(保持登入)
public class APIMthod {
// 建立CookieStore例項
private static CookieStore cookieStore = null;
private static HttpClientContext context = null;
private static String response = "";
public static String sendGetByHttpCient(String url){
HttpResponse httpResponse = null;
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
try {
HttpGet httpGet = new HttpGet(url);
httpResponse = client.execute(httpGet);
response = printResponse(httpResponse);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
// 關閉流並釋放資源
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
public static String sendPostByHttpCient(String url, Map<String, String> parameterMap){
HttpResponse httpResponse = null ;
UrlEncodedFormEntity postEntity = null;
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
try {
HttpPost httpPost = new HttpPost(url);
postEntity = new UrlEncodedFormEntity(getParam(parameterMap), "UTF-8");
httpPost.setEntity(postEntity);
httpResponse = client.execute(httpPost);
response = printResponse(httpResponse);
if (cookieStore == null || "".equals(cookieStore)) {
String host = httpPost.getURI().getHost();
setCookieStore(httpResponse, host);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
// 關閉流並釋放資源
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
public static String printResponse(HttpResponse httpResponse) throws ParseException, IOException {
String responseString = "";
// 獲取響應訊息實體
HttpEntity entity = httpResponse.getEntity();
// 判斷響應實體是否為空
if (entity != null) {
responseString = EntityUtils.toString(entity);
}
return responseString;
}
public static void setContext() {
context = HttpClientContext.create();
Registry<CookieSpecProvider> registry = RegistryBuilder
.<CookieSpecProvider> create()
.register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
.register(CookieSpecs.BROWSER_COMPATIBILITY,
new BrowserCompatSpecFactory()).build();
context.setCookieSpecRegistry(registry);
context.setCookieStore(cookieStore);
}
public static void setCookieStore(HttpResponse httpResponse,String host) {
cookieStore = new BasicCookieStore();
// JSESSIONID
if (null == httpResponse.getFirstHeader("Set-Cookie")) {
cookieStore = null;
}else {
String setCookie = httpResponse.getFirstHeader("Set-Cookie").getValue();
String JSESSIONID = setCookie.substring("JSESSIONID=".length(),setCookie.indexOf(";"));
// 新建一個Cookie
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID",JSESSIONID);
cookie.setVersion(0);
cookie.setDomain(host);
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
}
public static List<Cookie> getCookies(){
if (null == cookieStore) {
return null;
}
return cookieStore.getCookies();
}
public static void deleteCookies(){
if (cookieStore != null) {
cookieStore.clear();
cookieStore = null;
}
}
public static List<NameValuePair> getParam(Map<String, String> parameterMap) {
List<NameValuePair> param = new ArrayList<NameValuePair>();
Iterator it = parameterMap.entrySet().iterator();
while (it.hasNext()) {
Entry parmEntry = (Entry) it.next();
param.add(new BasicNameValuePair((String) parmEntry.getKey(),(String) parmEntry.getValue()));
}
return param;
}
相關推薦
HttpClient---cookie(保持登入)
public class APIMthod { // 建立CookieStore例項 private static CookieStore cookieStore = null; private static HttpClientCont
HttpClient的使用(模擬登入)
HttpClient是專門設計用來簡化HTTP客戶端與伺服器間各種通訊,程式設計的,可以使複雜的事情變得簡單。 注意: 在Android SDK編譯版本為23的時,且使用HTTPClient相關類庫專案的時候,如使用HttpClient會出現無法解析的錯誤, 這是由於an
ASP.NET頁面之間傳值的方式之Cookie(個人整理)
無意中 etime linq 首選項 ner www. ati 訪問 net Cookie Cookie 提供了一種在 Web 應用程序中存儲用戶特定信息的方法。例如,當用戶訪問您的站點時,您可以使用 Cookie 存儲用戶首選項或其他信息。當該用戶再次訪問您的網站時,
python相關小技巧(保持更新)
HA top 就是 method python 函數定義 blog 解決 div 1、查看導入庫的類屬性、方法 python有一點感覺特別不方便的就是,不像C++指定了類型後,該類型的實例打個“.”會智能提示它含有的方法或屬性 之前都是靠查看庫對應的官方文檔解決。這次才發現
Django 專案總結(3)- 第三方登入(QQ登入)
第三方登入(QQ 登入) 根據 qq 開發文件 oauth2.0 QQ 登入流程: 前端頁面點選 QQ 登入,需要跳轉到 QQ 的登入頁面,但是前端不知道 QQ 的登入連結,所以先想後端傳送請求,由伺服器生成 QQ 登入頁地址,返回給前端; 使用者在
【WEB開發】微信網頁授權第三方登入介面(WEB登入)
本文連結至:http://blog.csdn.net/hxker/article/details/50260669 第一步:獲取AppID AppSecret(不做解釋,自己去微信公眾平臺申請) 第二步:生成掃描二維碼,獲取code https://open
二、使用者模組(退出登入)
退出登入 大概步驟: Servlet層: /** * 退出登入 * 1、銷燬session * 2、刪除cookie * 3、跳轉到登入頁面 * @param request * @param
struts攔截器(實現登入)
攔截器的使用: package com.beiruan.xitongguanli.interceptor; import java.util.Map; import com.beiruan.xitongguanli.entity.User; import c
JavaEE 要懂的小事:二、圖解 Cookie(小甜餅)
作者:李強強 上一篇 圖解Http協議 ,這次繼續Http家族中的Cookie。泥瓦匠最近看到部落格園中一篇好文《超大cookie拒絕服務攻擊》,這就是因為瀏覽器Cookie太大,導致請求時,請求頭域過大造成傳送失敗。下面咱們就瞭解一下Cookie。按著以前的思路圖文並茂哈,沒圖說個XX。
python庫os的小總結(保持更新...)
os庫裡面提供了豐富的方法來處理檔案和目錄,自己在進行一些專案時經常用到,故記錄在下: 1. 返回當前的工作目錄 使用os.getcwd()函式,返回的是絕對路徑 2. 返回指定的資料夾包含的檔案或資料夾的名字的列表,這個列
SSH(遠端登入)
在linux中SSH服務對應兩個配置檔案: ssh特點:在傳輸資料的時候,對檔案加密後傳輸。 ssh作用:為遠端登入會話和其他網路服務提供安全性協議。 ssh小結: 1、SSH是安全的加密協議,用於遠端連線伺服器。 2、SSH預設埠是22,
微信小程式之八(使用者登入)
在商城中,訪問個人中心或者購物車前先判斷是否登入,從快取中讀取使用者名稱,密碼等,若無登入,或者清楚快取,則需登入。 下面以本人做的登入為例,login.js頁面 // pages/login/login.js Page({ onLoad:function(option
CTF中常見的php函式繞過(保持更新)
is_numeric()用於判斷是否是數字,通常配合數值判斷 is_numeric(@$a["param1"])?exit:NULL; if(@$a["param1"]){ ($a["param1"]>2017)?$v1=1:NULL; } //param
微信小程式 request請求封裝(包括登入)
這段時間都在開發小程式。封裝是少不了的部分。經過三輪的修改修改再修改之後,得到了下面現在一直在用的這版。如果小夥伴你只需要封裝,不考慮需不需要重新登入的話可以把if(res.data.code ==5000)這段去掉。下面wxLogin也可以去掉了(強迫症,用不到的都喜歡去
第三方登入(QQ登入)開發流程詳解
近排由於工作的繁忙,已經一個星期沒寫博文做分享了,接下來我對網站接入第三方登入----QQ登入的實現邏輯做一個詳細的講解。 對於整個流程的詳細文件可以到QQ互聯官網(http://wiki.connect.qq.com)檢視,我這裡就簡單地進行描述,主要是分析程式碼的實現過程。 我用的是C
CSS/HTML 一個漂亮的使用者註冊(使用者登入)頁面 動態效果
先上效果圖吧平面泡沫立體泡沫之前想做動態泡泡的效果,在網上沒有搜到類似的樣式。所以自己做了一個發上來,給有需要的人蔘考參考。程式碼部分有疑問或者有錯請在評論指出,儘量快點回復。程式碼部分:(無奈,發程式碼格式一直亂碼)<!DOCTYPE html><html
小程式開發7-資料渲染(for迴圈,block(解決title+img類似情況),wx:key(保持狀態),ws:if判斷)要麼迴圈要麼判斷
<view>{{username}}</view>單一項<view>{{renyuan[0]}}---{{renyuan[1]}}</view><view wx:for = "{{renyuan}}" > {{index}}--{{item}}<
springmvc+spring+mybatis(簡單登入)+aop日誌管理
1.logger日誌類 package com.springmvc.manage.model; import org.springframework.stereotype.Component; @Component public class Logger { pri
WEB_Rember Me(自動登入) 的幾種實現思路
本文討論幾種記住我功能的實現方式。 原理:使用者登入後,服務端為使用者生成一個Token,並放入客戶端Cookie中。下次使用者登入,服務端驗證Cookie中的Token並自動登入。 簡單的Token生成方法 Token=MD
網站註冊登入大概步驟(網站是怎樣通過cookie記住使用者的)
註冊: 將使用者填寫的賬號和密碼儲存在資料庫中(其中包括去重操作和密碼加密操作) 登入: 將使用者填寫的賬號和密碼與資料庫進行比對,如果資料庫中存在同樣的賬號和密碼,那麼在響應頭中設定Set-Cookie值,標誌該使用者是通過登入步驟進入首頁的 這樣每次使用者訪問同源網址時請求頭中都