Httpclient access to webpages with cookie restrictions
(13:01:39)
Reprinted 1. Cookie with no request header set String url = "http://www.drugstore.com/products/prod.asp? Httpclient client = new httpclient (); Getmethod = new getmethod (URL );
Int status = client.exe cutemethod (getmethod ); In this way, staatus returns 301 or the PAGE result after the jump is returned, and the URL content cannot be accessed.Therefore, you need to set the request header 2. Set the cookie of the request header to use the iehttpheaders tool to enter the URL in the browser to view the request header information, as follows: browser request information:GET/products/prod. asp? PID = 188801 & catid = 12943 HTTP/1.1Accept: image/JPEG, application/X-MS-application, image/GIF, application/XAML + XML, image/pjpeg, application/X-MS-xbap, application/X-Shockwave-flash, application/MSWord, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, ** accept-language: ZH-cnuser-AGENT: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qqpinyin 689; slcc2 ;. net CLR 2.0.50727 ;. net CLR 3.5.30729 ;. net CLR 3.0.30729) Accept-encoding: gzip, deflatehost: www. drugstore. comconnection: Keep-alivecookie: _ br_uid_1 = uid % 3d6876780775167% 3A; sticky = seaweb004p: 044086bc26eb4fae88bfbc531c091edd: 5mwwt52kjx2imf55cou5ue55; Drugstore % 2 efish = userid = 728708e24faa4229a58d61aa436133e3; s_vi = [Cs] V1 | 25d1666085011d4f-60000109600d3fba [ce]; Foresee. repeatdays = 90Set the request header in the Code: simply according to the request header sent by the browser, in the Code for the corresponding settings can be string url = "http://www.drugstore.com/products/prod.asp? PID = 266446 & catid = 119689 & cmbprodbrandfilter = 15867 & MP = true & TRX = GFI-0-MBS & trxp1 = 119689 & trxp2 = 266446 & trxp3 = 2 & trxp4 = 2 "; httpclient client = new httpclient (); // client. getparams (). setcookiepolicy (cookiepolicy. browser_compatibility );Getmethod = new getmethod (URL ); Getmethod. setRequestHeader ("accept", "image/JPEG, application/X-MS-application, image/GIF, application/XAML + XML, image/pjpeg, application/X-MS-xbap, application/X-Shockwave-flash, application/MSWord, application/vnd. MS-Excel, application/vnd. MS-PowerPoint ,*/*"); //Getmethod. setRequestHeader ("Referer", "http://kyxk.net/wForum/disparticle.php? Boardname = lifescience & id = 2114 "); Getmethod. setRequestHeader ("Accept-language", "En-us "); //Getmethod. setRequestHeader ("Accept-encoding", "gzip, deflate "); Getmethod. setRequestHeader ("If-modified-since", "Thu, 29 Jul 2004 02:24:49 GMT "); Getmethod. setRequestHeader ("If-None-match", "'3014d-1d31-000085ff1 '"); Getmethod. setRequestHeader ("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; qqpinyin 689; slcc2 ;. net CLR 2.0.50727 ;. net CLR 3.5.30729 ;. net CLR 3.0.30729 )"); Getmethod. setRequestHeader ("host", "www.drugstore.com "); Getmethod. setRequestHeader ("connection", "keep-alive "); Getmethod. setRequestHeader ("cookie", "_ br_uid_1 = uid % 3d6876780775167% 3A; bigipserverdscm_farm = 1746184384.0.0000; sticky = seaweb004p: bytes1c091edd: bdm5j355thye4445yvus2kis; Drugstore % 2 efish = userid = 728708e24faa4229a58d61aa436133e3; s_vi = [Cs] V1 | 25d1666085011d4f-60000109600d3fba [ce]; Foresee. repeatdays = 90 "); // Getmethod. getparams (). setparameter (httpmethodparams. retry_handler, //New defaulthttpmethodretryhaNdler ()); Int status = client.exe cutemethod (getmethod );The request headers set above are all the request headers seen in iehttpheaders, but setmethod must be set. setRequestHeader ("Accept-encoding", "gzip, deflate") is removed. Otherwise, garbled characters are required. The value is the information requested by the browser, the obtained webpage content is the actual requested webpage content.