关于httpclient访问url获取cookie的问题

天若有情灬丶 2019-02-21 03:10:18
url举例:http://wap.hn.10086.cn/wap/static/login/Login.html
该网址是一个湖南移动的登录页面,直接通过浏览器访问是可以正常访问的,但是通过httpclient访问,报202错误,如下图显示:
,经分析,该url需要传递cookie方可访问,
cookie格式如下:
FSSBBIl1UgzbN7N80S=u6PS.8ZYfsJE6mqkQDYQNo3UKR_i_jobqR5faviqjZO163HGWMJVxCxcK.vlkTkQQILhGiDaUvbI1krd7EMHlA; FSSBBIl1UgzbN7N80T=3eO6O0YuCfFn.WJrmte4cUDlpP7iVG6QvppyOtXq1Gkv1d3WQTfaYa9T2tIrRmv6K6xMAEE1I97jOyATy9FDzQL2pqbJ051iTYDbwE1XAi6QtBNG3eNcu_ce1HjIj1t796WiSYDZNOCDI70Zmnby_FlG3C9GMw2uw6_SR4fkuy0Np17Uk79Sv8.OP0CfboTKobm.PKtBj4QZXGL2CU7wkZ1SWGtzxcLKR3HPORWxQAEvwiG; WT_FPC=id=275ccc497bc0eee9a211550731084440:lv=1550731974228:ss=1550731084440; WT_USER_ID=344-226547929cbdae6; EcSite.session.id=961b388e-00ae-4149-bdd7-6527f8f8487d



现在的问题是怎么获取到request的cookie?求大神们帮助!!!
(通过response的cookie值只有FSSBBIl1UgzbN7N80S这一个值,通过httpclient还是访问不了的)


...全文
842 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶遮沉阳 2019-02-22
  • 打赏
  • 举报
回复
你想表达的是,使用HTTPURLConnection设置cookie,在connect()之后再使用HTTPURLConnection.getHeaderFields()遍历获取不到你设置的cookie吗?
天若有情灬丶 2019-02-21
  • 打赏
  • 举报
回复
httpclient是4.5版本的,这个是调用方法,现在是差请求头的cookie这个参数的值,如下图: ,这个值该怎么获取到呢?
maradona1984 2019-02-21
  • 打赏
  • 举报
回复
你确定是要问 "现在的问题是怎么获取到request的cookie"?

我看你是要如何用httpclient发送cookie吧?如果是的话,你直接复制我这段话,丢到百度里就能得到答案,如果没有,就加上httpClient的版本
httpClient各版本的差异还是挺大
天若有情灬丶 2019-02-21
  • 打赏
  • 举报
回复
图二上传错了,这个图才对
Jsoup+httpclient 模拟登陆和抓取页面 package com.app.html; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.cookie.CookieSpec; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import com.app.comom.FileUtil; public class HttpClientHtml { private static final String SITE = "login.goodjobs.cn"; private static final int PORT = 80; private static final String loginAction = "/index.php/action/UserLogin"; private static final String forwardURL = "http://user.goodjobs.cn/dispatcher.php/module/Personal/?skip_fill=1"; private static final String toUrl = "d:\\test\\"; private static final String css = "http://user.goodjobs.cn/personal.css"; private static final String Img = "http://user.goodjobs.cn/images"; private static final String _JS = "http://user.goodjobs.cn/scripts/fValidate/fValidate.one.js"; /** * 模拟等录 * @param LOGON_SITE * @param LOGON_PORT * @param login_Action * @param params * @throws Exception */ private static HttpClient loginHtml(String LOGON_SITE, int LOGON_PORT,String login_Action,String ...params) throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT); // 模拟登录页面 PostMethod post = new PostMethod(login_Action); NameValuePair userName = new NameValuePair("memberName",params[0] ); NameValuePair password = new NameValuePair("password",params[1] ); post.setRequestBody(new NameValuePair[] { userName, password }); client.executeMethod(post); post.releaseConnection(); // 查看cookie信息 CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); if (cookies != null) if (cookies.length == 0) { System.out.println("Cookies is not Exists "); } else { for (int i = 0; i < cookies.length; i++) { System.out.println(cookies[i].toString()); } } return client; } /** * 模拟等录 后获取所需要的页面 * @param client * @param newUrl * @throws Exception */ private static String createHtml(HttpClient client, String newUrl) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String filePath = toUrl + format.format(new Date() )+ "_" + 1 + ".html"; PostMethod post = new PostMethod(newUrl); client.executeMethod(post); //设置编码 post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK"); String content= post.getResponseBodyAsString(); FileUtil.write(content, filePath); System.out.println("\n写入文件成功!"); post.releaseConnection(); return filePath; } /** * 解析html代码 * @param filePath * @param random * @return */ private static String JsoupFile(String filePath, int random) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); File infile = new File(filePath); String url = toUrl + format.format(new Date()) + "_new_" + random+ ".html"; try { File outFile = new File(url); Document doc = Jsoup.parse(infile, "GBK"); String html=""; StringBuffer sb = new StringBuffer(); sb.append(html).append("\n"); sb.append("").append("\n"); sb.append("").append("\n"); sb.append("欢迎使用新安人才网个人专区").append("\n"); Elements meta = doc.getElementsByTag("meta"); sb.append(meta.toString()).append("\n"); ////////////////////////////body////////////////////////// Elements body = doc.getElementsByTag("body"); ////////////////////////////link////////////////////////// Elements links = doc.select("link");//对link标签有href的路径都作处理 for (Element link : links) { String hrefAttr = link.attr("href"); if (hrefAttr.contains("/personal.css")) { hrefAttr = hrefAttr.replace("/personal.css",css); Element hrefVal=link.attr("href", hrefAttr);//修改href的属性值 sb.append(hrefVal.toString()).append("\n"); } } ////////////////////////////script////////////////////////// Elements scripts = doc.select("script");//对script标签 for (Element js : scripts) { String jsrc = js.attr("src"); if (jsrc.contains("/fValidate.one.js")) { String oldJS="/scripts/fValidate/fValidate.one.js";//之前的css jsrc = jsrc.replace(oldJS,_JS); Element val=js.attr("src", jsrc);//修改href的属性值 sb.append(val.toString()).append("\n").append(""); } } ////////////////////////////script////////////////////////// Elements tags = body.select("*");//对所有标签有src的路径都作处理 for (Element tag : tags) { String src = tag.attr("src"); if (src.contains("/images")) { src = src.replace("/images",Img); tag.attr("src", src);//修改src的属性值 } } sb.append(body.toString()); sb.append(""); BufferedReader in = new BufferedReader(new FileReader(infile)); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outFile), "gbk")); String content = sb.toString(); out.write(content); in.close(); System.out.println("页面已经爬完"); out.close(); } catch (IOException e) { e.printStackTrace(); } return url; } public static void main(String[] args) throws Exception { String [] params={"admin","admin123"}; HttpClient client = loginHtml(SITE, PORT, loginAction,params); // 访问所需的页面 String path=createHtml(client, forwardURL); System.out.println( JsoupFile(path,1)); } }

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧