HTTPClient实现免登陆请求(带cookie请求)
程序员文章站
2022-05-30 21:58:42
背景: 使用httpClient请求某登录型网站,模拟一个操作,一般步骤一个httpclient模式登录-》httpClient模拟操作; 此时发现,每次操作都需要进行一次登录,极其浪费时间,是否可以通过某一方式进行一次登录多次操作,这里提供一种方式,带cookie登录。 登录获取cookie: 创 ......
背景:
使用httpclient请求某登录型网站,模拟一个操作,一般步骤一个httpclient模式登录-》httpclient模拟操作;
此时发现,每次操作都需要进行一次登录,极其浪费时间,是否可以通过某一方式进行一次登录多次操作,这里提供一种方式,带cookie登录。
登录获取cookie:
public string loginmodel(string username, string password) { string jsessionid = null; httppost httppost = new httppost(url);//httppost try { list<namevaluepair> para = new arraylist<namevaluepair>(); para.add(new basicnamevaluepair("password", password)); para.add(new basicnamevaluepair("username", username));//构造表单 httppost.setheader( "user-agent", "mozilla/5.0 (windows nt 6.3; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/41.0.2272.118 safari/537.36"); httppost.setentity(new urlencodedformentity(para, "utf-8"));//设置请求体 basiccookiestore cookiestore = new basiccookiestore();//建立一个cookiestore closeablehttpclient httpclient = httpclients.custom().setdefaultcookiestore(cookiestore).build();//建立带cookie的httpclient int statuts_codes = httpclient.execute(httppost).getstatusline().getstatuscode();//发送请求,发送成功后cookie将存在于cookiestore中 if (statuts_codes == httpstatus.sc_ok) {//请求成功 list<cookie> cookies = cookiestore.getcookies();//遍历获取需要的值 for (int i = 0; i < cookies.size(); i++) {//获取jsessionid if (cookies.get(i).getname().equals("id")) { jsessionid = cookies.get(i).getvalue(); } } cookiemap.put("jsessionid", jsessionid); } else {//请求失败 } } catch (unsupportedencodingexception ex) { } catch (ioexception ex) { } finally { httppost.releaseconnection();//释放资源 } return cookiemap.get("jsessionid"); }
创建带有cookie的httpclient
public closeablehttpclient gethttpclients(string username, string password) { basiccookiestore cookiestore = new basiccookiestore(); httpclientbuilder httpclientbuilder = httpclientbuilder.create(); string jsessionid = loginmodel(username, password); basicclientcookie cookie = new basicclientcookie("jsessionid", jsessionid); cookie.setversion(0); string domain = constant.hook_url.substring(0, constant.hook_url.indexof(":")); if (constant.hook_url.startswith("http://")) { domain = constant.hook_url.substring(constant.hook_url.lastindexof("/") + 1, constant.hook_url.lastindexof(":")); } cookie.setdomain(domain); cookie.setpath(constant.hook_fw); cookiestore.addcookie(cookie); //带有cookie的httpclient return httpclientbuilder.setdefaultcookiestore(cookiestore).build(); }
使用:
public void usecookiehttpclient() { closeablehttpclient httpclient = gethttpclients(user, pass); httppost httppost = new httppost(url2); list<namevaluepair> para = new arraylist<>();//表单 para.add("键", "值"); httppost.setentity(new urlencodedformentity(para, "utf-8")); httppost.setentity(new urlencodedformentity(para, "utf-8")); closeablehttpresponse res = httpclient.execute(httppost); int statuts_codes = res.getstatusline().getstatuscode(); if (statuts_codes == httpstatus.sc_ok) {//请求成功 string result = entityutils.tostring(res.getentity(), "utf-8");//返回值 } }
上一篇: 为什么会明朝多“昏君”,清代多“明君”?
下一篇: 介绍Python中几个常用的类方法