示例#1
0
 /// <summary>
 /// Get verify code (appears if getting page too fast)
 /// </summary>
 /// <param name="PageDocument">Page as document</param>
 /// <returns>Struct which stores all four necessary parameters for verify code, plus local verify image code address</returns>
 public static VerifyCode GetVerifyCode(HtmlDocument PageDocument)
 {
     string localVerifyCode = System.IO.Path.GetTempFileName() + ".jpg";
     HtmlNode codeNode = PageDocument.DocumentNode.SelectSingleNode("//img[@id=\"J_CheckCode\"]");
     Utility.DownloadFile(codeNode.GetAttributeValue("src", ""), localVerifyCode);
     VerifyCode verifyCode = new VerifyCode();
     verifyCode.code = "";
     verifyCode.sessionID = PageDocument.DocumentNode.SelectSingleNode("//input[@name=\"sessionID\"]").GetAttributeValue("value", "");
     verifyCode.apply = PageDocument.DocumentNode.SelectSingleNode("//input[@name=\"apply\"]").GetAttributeValue("value", "");
     verifyCode.referer = PageDocument.DocumentNode.SelectSingleNode("//input[@name=\"referer\"]").GetAttributeValue("value", "");
     verifyCode.localVerifyCode = localVerifyCode;
     return (verifyCode);
 }
示例#2
0
        /// <summary>
        /// Post verify code
        /// </summary>
        /// <param name="VerifyData">The struct which contains all four necessary parameters</param>
        /// <returns>The page after posting as Document</returns>
        public static HtmlDocument PostVerifyCode(VerifyCode VerifyData)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.xiami.com/alisec/captcha/tmdgetv3.php");

            StringBuilder postData = new StringBuilder();
            postData.Append("apply=" + HttpUtility.UrlEncode(VerifyData.apply) + "&");
            postData.Append("code=" + HttpUtility.UrlEncode(VerifyData.code) + "&");
            postData.Append("referer=" + HttpUtility.UrlEncode(VerifyData.referer) + "&");
            postData.Append("sessionID=" + HttpUtility.UrlEncode(VerifyData.sessionID));

            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] postBytes = ascii.GetBytes(postData.ToString());

            request.Method = "POST";
            request.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postBytes.Length;
            request.Headers.Set("Accept-Encoding", "deflate");
            request.Headers.Set("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            request.Host = "www.xiami.com";
            request.Referer = VerifyData.referer;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
            request.CookieContainer = Utility.cookies;

            Stream postStream = request.GetRequestStream();
            postStream.Write(postBytes, 0, postBytes.Length);

            Stream responseStream;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            responseStream = response.GetResponseStream();
            StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8, true);

            Utility.cookies.Add(response.Cookies);

            string responseText = responseReader.ReadToEnd();

            HtmlDocument htmlPageContent = new HtmlAgilityPack.HtmlDocument();
            htmlPageContent.LoadHtml(responseText);
            return (htmlPageContent);
        }