Article From:https://segmentfault.com/q/1010000011128062
Question:

java Using httpclient to simulate landing knowledge, get the difference between cookie and browser landing.

    public static String post(String url, NameValuePair[] loginInfo) throws Exception {
        HttpClient httpClient = new HttpClient();
        // Simulate landing, select the Post request according to the actual server-side request.PostMethod postMethod = new PostMethod (URL);SetHead (postMeThod);SetBody (postMethod, loginInfo);HttpClient.getParams ().SetCookiePolicy (CookiePolicy.BROWSER_COMPATIBILITY);HttpClient.executeMethod (postMethod);/ / Cooki after landingECookie[] cookies = httpClient.getState ().GetCookies ();StringBuffer cookie = new STringBuffer ();For (Cookie C: cookies) {Cookie.append (c.toString () + ";");}Return cookie.toString ();}Private static void setHead (HttpMethod httpM)Ethod, Map< String, String> map) {/ / / / determine whether a custom message header is passed inIf (null! = map & & MA)P.size () > 0) {Map.keySet ().ForEach (key -> httpMethod.setRequestHeader (key, map.g)ET (key))))}/ / public header (inconsistent content of different web header)HttpMethod.setRequestHeader ("User-Agent"), "Mozilla/5.0 (Windows NT 10; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113"Safari/537.36 ");HttpMethod.setRequestHeader ("Content-Type", "application/x-www-form-urlenco")Ded ");HttpMethod.setRequestHeader ("Accept", "text/html, application/xhtml+xml, application/xm"L; q=0.9, * / *; q=0.8 ");}Private static void setBody (PostMethod postMethod, NameValuePair)[] loginInfo) {PostMethod.setRequestBody (loginInfo);}

httpclientObtained Cookie:
aliyungf_tc=AQAAAD9s3S/D6A0AQEfsc/I8r3DFSet9;l_n_c=1;q_c1=6691d50c82ae42789d444a4131ef4e24|1505186310000|1505186310000;r_cap_id=NjY5MzFmZjIyY2FkNDFlMTg4NmYxZDI1NzlmYjFjNTY=|1505186310|6d176ad90ba8868dbb8f62aaff1dd3d95ab71df9;cap_id=MjU1ZmVjZjBhYzVkNGRjZDk2ZDFhOTY4Nzc1MGQ3YmI=|1505186310|5800cbe49135a68d11e2416f182c7540586d60c2;l_cap_id=NWY2OWJiNzgxZGE0NDg0OTg3NDk2MjE4YjNiMDI2MDU=|1505186310|6cb0b38aee885d53bc28bbf5434be8fb93044535;n_c=1;

Browser landing Cookie:
_zap=8bee571a-7927-46b8-9a1e-561d437c80a5; d_c0=”AAACmF3qRQyPTobzdgXafIBJ9zFDhI22laY=|1503630379″; q_c1=a0eebeb7f0634e7792282bf1bceac306|1503887510000|1494232687000; q_c1=a0eebeb7f0634e7792282bf1bceac306|1504688356000|1494232687000; aliyungf_tc=AQAAALNUi2SIIQ8AQEfsc5RHw3SM7uUs; infinity_uid=”2|1:0|10:1505184034|12:infinity_uid|24:ODg2OTY0MzkwMDM1ODY1NjAw|dd6fd2c07dd0fb3fb1ad07aca4ed378470a6420a60fb0d8bbaa257c2d53a84f0″; _xsrf=28666d5b-5c8c-4d5f-875e-ccbc5d88252c; r_cap_id=”MjcxYWI4ODk2OTE0NDRlZTg4NzY4NDgzMzA3OWJiMzk=|1505184653|5876f662ff54aa1cbc3b7b61e51a353b96e75c29″; __utma=51854390.1046329935.1505184717.1505184717.1505184717.1; __utmb=51854390.0.10.1505184717; __utmc=51854390; __utmz=51854390.1505184717.1.1.utmcsr=zhihu.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmv=51854390.000–|3=entry_date=20170508=1; l_cap_id=”OWQyOGZmYTk2ODk2NGYzMmFkNmU3NDg5ZDM4YzdhZjU=|1505185164|92ea8fe79847c654b84acd95dd1caf84c3106f26″; z_c0=Mi4xUUpYUkJRQUFBQUFBQUFLWVhlcEZEQmNBQUFCaEFsVk5ydHJlV1FBVFhIYnVHdm5OLVFjcTNxOU5yYUtfamVEUGxB|1505185198|5c5904bdadce56814173771ba598a851181ea19e; unlock_ticket=”QUZCQ2tyZ2dTd3dYQUFBQVlRSlZUYlpVdDFuVVUyZWlza1N2ajhwVnJPbGtxcm1MdWliVWZnPT0=|1505185198|af0cb75a0b7e76fb83f13ce25f4066eca68227d5″; cap_id=”NjI2ZmYxZmIwMmU3NDliNzllZTc0MDI3YThkOWRhOTU=|1505185198|98d1814bdebc6d2277c50185475d8aea2c03ca25″

Why is this? Ask the gods to answer

Answer 0:

Your httpClient request is different from the browser request. Some data such as Cookie are created by client JavaScript, such as __utmc, which is cookie of Google Analytics. HttpClient andDo not execute the JavaScript on the page.

Leave a Reply

Your email address will not be published. Required fields are marked *