Exemple #1
0
        public static PostRequestModel CreatePostModel(string url, string postData, CookieContainer cookie,
                                                       WebProxy proxy = null, bool allowRedirect = true, string refUrl = "",
                                                       bool isReadAll = true, string fakeIP      = "")
        {
            PostRequestModel submitOrder = new PostRequestModel()
            {
                TheURL          = url,
                RefURL          = refUrl,
                AllowRedirect   = true,
                CookieContainer = cookie,
                IsReadAll       = true,
                TheCode         = System.Text.UTF8Encoding.UTF8,
                WebProxy        = null,
                FakeIP          = string.Empty,
                PostData        = postData
            };

            return(submitOrder);
        }
Exemple #2
0
        public static string SendPostHttpRequest(PostRequestModel model)
        {
            string responseText;

            try
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
                {
                    return(true);
                };

                string temp = string.Empty;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(model.TheURL);
                request.Method            = "POST";
                request.Proxy             = model.WebProxy;
                request.Referer           = model.RefURL;
                request.KeepAlive         = true;
                request.AllowAutoRedirect = model.AllowRedirect;
                request.UserAgent         = MyUserAgent;
                request.CookieContainer   = model.CookieContainer;
                request.ContentType       = "application/x-www-form-urlencoded";
                request.Headers.Add("Accept-Encoding", "gzip, deflate");

                if (!string.IsNullOrEmpty(model.FakeIP))
                {
                    request.Headers.Add("X-Forwarded-For", model.FakeIP);
                }

                //写form表单数据,拼成string,参数之间用&
                request.ServicePoint.Expect100Continue = false;
                byte[] bytes = model.TheCode.GetBytes(model.PostData);
                request.ContentLength = bytes.Length;
                request.GetRequestStream().Write(bytes, 0, bytes.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.Cookies != null && response.Cookies.Count > 0)
                {
                    model.CookieContainer.Add(response.Cookies);
                }

                if (model.IsReadAll)
                {
                    Stream       responseStream;
                    StreamReader reader;
                    string       contentType     = response.ContentType;
                    string       contentEncoding = response.ContentEncoding;
                    if (contentEncoding == "gzip")
                    {
                        responseStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
                    }
                    else
                    {
                        responseStream = response.GetResponseStream();
                    }
                    string characterSet = response.CharacterSet;
                    if (characterSet == "")
                    {
                        reader = new StreamReader(responseStream, Encoding.Default);
                    }
                    else
                    {
                        characterSet = characterSet.Trim().ToLower();
                        string str7 = characterSet;
                        if (str7 == "gb2312")
                        {
                            reader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
                        }
                        else if ((str7 == "utf-8") || (str7 == "iso-8859-1"))
                        {
                            reader = new StreamReader(responseStream, Encoding.UTF8);
                        }
                        else
                        {
                            reader = new StreamReader(responseStream, Encoding.Default);
                        }
                    }

                    temp = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                }

                response.Close();
                request  = null;
                response = null;
                bytes    = null;

                responseText = temp;
            }
            catch (Exception)
            {
                responseText = string.Empty;
            }
            return(responseText);
        }