Esempio n. 1
0
        /// <summary>
        /// 简单HTTP POST方法,用于post验证码,Content-Type: application/x-www-form-urlencoded
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string Post(string url, WebHeaderCollection headers, string postData, bool isUseCookie = false)
        {
            string responseText = "";

            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);


                foreach (string key in headers.Keys)
                {
                    switch (key.ToLower())
                    {
                    case "user-agent":
                        request.UserAgent = headers[key];
                        break;

                    case "referer":
                        request.Referer = headers[key];
                        break;

                    case "host":
                        request.Host = headers[key];
                        break;

                    case "contenttype":
                        request.ContentType = headers[key];
                        break;

                    default:
                        break;
                    }
                }

                if (string.IsNullOrEmpty(request.Referer))
                {
                    request.Referer = "http://weixin.sogou.com/";
                }
                ;
                if (string.IsNullOrEmpty(request.Host))
                {
                    request.Host = "weixin.sogou.com";
                }
                ;

                if (isUseCookie)
                {
                    request.CookieContainer = new CookieContainer();
                    CookieCollection cc = Tools.LoadCookieFromCache();
                    request.CookieContainer.Add(cc);
                }



                request.Method = "POST";

                request.ContentType = "application/x-www-form-urlencoded";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;

                // Get the request stream.
                Stream inDataStream = request.GetRequestStream();
                // Write the data to the request stream.
                inDataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                inDataStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();


                if (isUseCookie && response.Cookies.Count > 0)
                {
                    var         cookieCollection = response.Cookies;
                    WechatCache cache            = new WechatCache(Config.CacheDir, 3000);
                    if (!cache.Add("cookieCollection", cookieCollection, 3000))
                    {
                        cache.Update("cookieCollection", cookieCollection, 3000);
                    }
                    ;
                }

                // Get the stream containing content returned by the server.
                Stream outDataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(outDataStream);
                // Read the content.
                responseText = reader.ReadToEnd();

                // Cleanup the streams and the response.
                reader.Close();
                outDataStream.Close();
                response.Close();
            }
            catch (Exception e)
            {
                logger.Error(e);
            }

            return(responseText);
        }
Esempio n. 2
0
        string _vcode_url = ""; //需要填验证码的url



        /// <summary>
        /// 指定header参数的HTTP Get方法
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="url"></param>
        /// <returns>respondse</returns>
        public string Get(WebHeaderCollection headers, string url, string responseEncoding = "UTF-8", bool isUseCookie = false)
        {
            string responseText = "";

            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);


                request.Method = "GET";
                //request.Headers = headers;
                foreach (string key in headers.Keys)
                {
                    switch (key.ToLower())
                    {
                    case "user-agent":
                        request.UserAgent = headers[key];
                        break;

                    case "referer":
                        request.Referer = headers[key];
                        break;

                    case "host":
                        request.Host = headers[key];
                        break;

                    case "contenttype":
                        request.ContentType = headers[key];
                        break;

                    case "accept":
                        request.Accept = headers[key];
                        break;

                    default:
                        break;
                    }
                }

                if (string.IsNullOrEmpty(request.Referer))
                {
                    request.Referer = "http://weixin.sogou.com/";
                }
                ;
                if (string.IsNullOrEmpty(request.Host))
                {
                    request.Host = "weixin.sogou.com";
                }
                ;
                if (string.IsNullOrEmpty(request.UserAgent))
                {
                    Random r     = new Random();
                    int    index = r.Next(WechatSogouBasic._agent.Count - 1);
                    request.UserAgent = WechatSogouBasic._agent[index];
                }
                if (isUseCookie)
                {
                    CookieCollection cc = Tools.LoadCookieFromCache();
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cc);
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (isUseCookie && response.Cookies.Count > 0)
                {
                    var         cookieCollection = response.Cookies;
                    WechatCache cache            = new WechatCache(Config.CacheDir, 3000);
                    if (!cache.Add("cookieCollection", cookieCollection, 3000))
                    {
                        cache.Update("cookieCollection", cookieCollection, 3000);
                    }
                    ;
                }
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();



                //如果response是图片,则返回以base64方式返回图片内容,否则返回html内容
                if (response.Headers.Get("Content-Type") == "image/jpeg" || response.Headers.Get("Content-Type") == "image/jpg")
                {
                    //totest
                    Image img = Image.FromStream(dataStream, true);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        byte[] imageBytes   = ms.ToArray();
                        string base64String = Convert.ToBase64String(imageBytes);
                        responseText = base64String;
                    }
                }
                else //read response string
                {
                    // Open the stream using a StreamReader for easy access.
                    Encoding encoding;
                    switch (responseEncoding.ToLower())
                    {
                    case "utf-8":
                        encoding = Encoding.UTF8;
                        break;

                    case "unicode":
                        encoding = Encoding.Unicode;
                        break;

                    case "ascii":
                        encoding = Encoding.ASCII;
                        break;

                    default:
                        encoding = Encoding.Default;
                        break;
                    }
                    StreamReader reader = new StreamReader(dataStream, encoding);//System.Text.Encoding.Default
                    // Read the content.

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        responseText = reader.ReadToEnd();
                        if (responseText.Contains("用户您好,您的访问过于频繁,为确认本次访问为正常用户行为,需要您协助验证"))
                        {
                            _vcode_url = url;
                            throw new Exception("weixin.sogou.com verification code");
                        }
                    }
                    else
                    {
                        logger.Error("requests status_code error" + response.StatusCode);
                        throw new Exception("requests status_code error");
                    }
                    reader.Close();
                }


                // Cleanup the streams and the response.
                dataStream.Close();
                response.Close();
            }
            catch (Exception e)
            {
                logger.Error(e);
            }

            return(responseText);
        }