상속: ICollection
        //用于获取验证码图像的方法
        public Stream GetCheckCodeImageStream(string imageAddress, CookieContainer ccEntered, out CookieCollection ccReturned)
        {
            HttpWebRequest request;
            HttpWebResponse response;

            request = (HttpWebRequest)HttpWebRequest.Create(imageAddress);

            request.Method = "GET";
            request.ProtocolVersion = HttpVersion.Version11;
            //request.Connection = "keep-alive";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
            request.Accept = "*/*";
            request.Referer = "http://buy.taobao.com/auction/buy_now.jhtml";

            //request.Headers.Add("Connection", "keep-alive");

            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
            request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");

            request.CookieContainer = ccEntered;

            request.AllowAutoRedirect = true;

            response = (HttpWebResponse)request.GetResponse();
            ccReturned = response.Cookies;
            Stream s = response.GetResponseStream();
            response.Close();
            return s;
        }
예제 #2
1
 public static List<DeepBlue.Models.Entity.DealClosingCostType> GetDealClosingCostTypesFromDeepBlue(CookieCollection cookies)
 {
     // Admin/DealClosingCostTypeList?pageIndex=1&pageSize=5000&sortName=Name&sortOrder=asc
     List<DeepBlue.Models.Entity.DealClosingCostType> dealClosingCostTypes = new List<DeepBlue.Models.Entity.DealClosingCostType>();
     // Send the request
     string url = HttpWebRequestUtil.GetUrl("Admin/DealClosingCostTypeList?pageIndex=1&pageSize=5000&sortName=Name&sortOrder=asc");
     HttpWebResponse response = HttpWebRequestUtil.SendRequest(url, null, false, cookies, false, HttpWebRequestUtil.JsonContentType);
     if (response.StatusCode == System.Net.HttpStatusCode.OK) {
         using (Stream receiveStream = response.GetResponseStream()) {
             // Pipes the stream to a higher level stream reader with the required encoding format.
             using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) {
                 string resp = readStream.ReadToEnd();
                 if (!string.IsNullOrEmpty(resp)) {
                     JavaScriptSerializer js = new JavaScriptSerializer();
                     FlexigridData flexiGrid = (FlexigridData)js.Deserialize(resp, typeof(FlexigridData));
                     foreach (Helpers.FlexigridRow row in flexiGrid.rows) {
                         DeepBlue.Models.Entity.DealClosingCostType dealClosingType = new DeepBlue.Models.Entity.DealClosingCostType();
                         dealClosingType.DealClosingCostTypeID = Convert.ToInt32(row.cell[0]);
                         dealClosingType.Name = Convert.ToString(row.cell[1]);
                         dealClosingCostTypes.Add(dealClosingType);
                     }
                 }
                 else {
                 }
                 response.Close();
                 readStream.Close();
             }
         }
     }
     return dealClosingCostTypes;
 }
예제 #3
0
 public MovieStreamSet()
 {
     Cookies      = new System.Net.CookieCollection();
     Headers      = new NameValueCollection();
     VideoStreams = new List <VideoStream>();
     Captions     = new List <Caption>();
 }
        /// <summary>
        /// Builds a unit test for a cookie collection.
        /// </summary>
        /// <param name="testType"> The test type.</param>
        /// <param name="cookies"> The cookie collection.</param>
        /// <returns> An edited cookie collection.</returns>
        public CookieCollection BuildUnitTestCookies(UnitTestType testType, CookieCollection cookies)
        {
            CookieCollection changedCookies = null;
            IHtmlFormUnitTest tester = null;

            // Call FillForm
            switch (testType)
            {
                case UnitTestType.BufferOverflow:
                    tester = new BufferOverflowTester((BufferOverflowTesterArgs)this.Arguments);
                    break;
                case UnitTestType.DataTypes:
                    tester = new DataTypesTester((DataTypesTesterArgs)this.Arguments);
                    break;
                case UnitTestType.SqlInjection:
                    tester = new SqlInjectionTester((SqlInjectionTesterArgs)this.Arguments);
                    break;
                case UnitTestType.XSS:
                    tester = new XssInjectionTester((XssInjectionTesterArgs)this.Arguments);
                    break;
            }

            if ( tester != null )
                changedCookies = tester.FillCookies(cookies);

            return changedCookies;
        }
예제 #5
0
        public static void SyncDealClosingCostTypes(CookieCollection cookies)
        {
            List<string> blueDealClosingCostTypes = GetDealClosingCostTypesFromBlue();
            List<DeepBlue.Models.Entity.DealClosingCostType> deepblueDealClosingCostTypes = GetDealClosingCostTypesFromDeepBlue(cookies);

            Util.Log("DealClosingCostTypes import. Total Records to be imported: " + blueDealClosingCostTypes.Count);
            int totalFailures = 0;
            // Create a Legal Fees closing cost type
            blueDealClosingCostTypes.Insert(0, Globals.LegalFee);
            bool first = true;
            foreach (string blueDealClosingCostType in blueDealClosingCostTypes) {
                if (first || !blueDealClosingCostType.ToLower().Contains(Globals.LegalFee.ToLower())) {
                    DeepBlue.Models.Entity.DealClosingCostType deepBlueDealClosingCostType = deepblueDealClosingCostTypes.Where(x => x.Name == blueDealClosingCostType).FirstOrDefault();
                    if (deepBlueDealClosingCostType == null) {
                        // Add the new issuer
                        string resp = string.Empty;
                        string closingCostType = blueDealClosingCostType.Length > 50 ? blueDealClosingCostType.Substring(0, 50) : blueDealClosingCostType;
                        int? issuer = CreateDealClosingCostType(cookies, closingCostType, out resp);
                        if (!issuer.HasValue) {
                            totalFailures++;
                        }
                    }
                }
                first = false;
            }
            Util.Log("Total import failures: " + totalFailures);
        }
예제 #6
0
        public bool TestConnectionToUrl(string url, string connectionType)
        {
            System.Net.CookieCollection cookies = HttpHelper.colCookies;

            bool result = true;

            try
            {
                // because the cookie collection is modified during the request, save it.

                HttpHelper httpRequest = new HttpHelper(_connectionId, this.bugzillaCharset);

                //GET HTML content for the page provided
                string htmlContent = httpRequest.GetFromUrl(url, true);

                // search in the htmlContent the string that represent the connection type
                if (htmlContent.IndexOf(connectionType) == -1)
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                MyLogger.Write(ex, "TestConnectionToUrl", LoggingCategory.Exception);

                result = false;
            }
            finally
            {
                HttpHelper.colCookies = cookies;
            }

            return(result);
        }
예제 #7
0
 public Response(String statusDescription, int statusCode, String body, CookieCollection cookies)
 {
     _statusDescription = statusDescription;
     _statusCode = statusCode;
     _cookies = cookies;
     _body = body;
 }
예제 #8
0
 public static HttpWebRequest GenerateRequest(string URL, string Method, string token, bool KeepAlive = false, string ContentType = null, byte[] data = null, int offset = 0, int length = 0, string ContentRange = null, bool PreferAsync = false, int Timeout = 20 * 1000, string host = null, string Referer = null, string Accept = null, CookieCollection cookies = null)
 {
     Uri httpUrl = new Uri(URL);
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpUrl);
     req.ProtocolVersion = new System.Version("1.0");
     req.Timeout = Timeout;
     req.ReadWriteTimeout = Timeout;
     req.Method = Method;
     if (token != null) req.Headers.Add("Authorization", "Bearer " + token);
     req.KeepAlive = KeepAlive;
     if (ContentType != null) req.ContentType = ContentType;
     if (ContentRange != null) req.Headers.Add("Content-Range", ContentRange);
     if (PreferAsync == true) req.Headers.Add("Prefer", "respond-async");
     if (Referer != null) req.Referer = Referer;
     if (Accept != null) req.Accept = Accept;
     if (cookies != null)
     {
         req.CookieContainer = new CookieContainer();
         req.CookieContainer.Add(cookies);
     }
     if (data != null)
     {
         req.ContentLength = length;
         Stream stream = req.GetRequestStream();
         stream.Write(data, offset, length);
         stream.Close();
     }
     return req;
 }
	public void Add ()
	{
		try {
			Cookie c = null;
			col.Add (c);
			Assert.Fail ("#1");
		} catch (ArgumentNullException) {
		}
		
		// in the microsoft implementation this will fail,
		// so we'll have to fail to.
		try {
			col.Add (col);
			Assert.Fail ("#2");
		} catch (Exception) {
		}
		Assert.AreEqual (col.Count, 3, "#3");
		
		col.Add (new Cookie("name1", "value1"));		
		Assert.AreEqual (col.Count, 3, "#4");
		
		CookieCollection col2 = new CookieCollection();
		Cookie c4 = new Cookie("name4", "value4");
		Cookie c5 = new Cookie("name5", "value5");
		col2.Add (c4);
		col2.Add (c5);
		col.Add (col2);
		Assert.AreEqual (col.Count, 5, "#5");
		Assert.AreEqual (col ["NAME4"], c4, "#6");
		Assert.AreEqual (col [4], c5, "#7");
	}
        public void GetReady () 
        {
		col = new CookieCollection ();	
		col.Add (new Cookie ("name1", "value1"));
		col.Add (new Cookie ("name2", "value2", "path2"));
		col.Add (new Cookie ("name3", "value3", "path3", "domain3"));		
	}
예제 #11
0
파일: Cookies.cs 프로젝트: G-IT-ED/vk
        public void AddFrom(Uri responseUrl, CookieCollection cookies)
        {
            foreach (Cookie cookie in cookies)
                Container.Add(responseUrl, cookie);

            BugFixCookieDomain();
        }
예제 #12
0
        public static void Serialize(CookieCollection cookies, Uri address, Stream stream)
        {
            var serializer = new DataContractSerializer(typeof (IEnumerable<Cookie>));
            IEnumerable<Cookie> cookieList = cookies.OfType<Cookie>();

            serializer.WriteObject(stream, cookieList);
        }
예제 #13
0
 public static HttpWebResponse CreateGetHttpResponse(string url, int timeout, string userAgent, CookieCollection cookies)
 {
     HttpWebRequest request = null;
     if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
     {
         //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true)
         ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
         request = WebRequest.Create(url) as HttpWebRequest;
         request.ProtocolVersion = HttpVersion.Version10;    //http版本,默认是1.1,这里设置为1.0
     }
     else
     {
         request = WebRequest.Create(url) as HttpWebRequest;
     }
     request.Method = "GET";
     //设置代理UserAgent和超时
     //request.UserAgent = userAgent;
     //request.Timeout = timeout;
     if (cookies != null)
     {
         request.CookieContainer = new CookieContainer();
         request.CookieContainer.Add(cookies);
     }
     return request.GetResponse() as HttpWebResponse;
 }
        /// <summary>  
        /// 创建GET方式的HTTP请求  
        /// </summary>  
        /// <param name="url">请求的URL</param>  
        /// <param name="timeout">请求的超时时间</param>  
        /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
        /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
        /// <returns></returns>  
        public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.UserAgent = DefaultUserAgent;
            if (!string.IsNullOrEmpty(userAgent))
            {
                request.UserAgent = userAgent;
            }
            if (timeout.HasValue)
            {
                request.Timeout = timeout.Value;
            }
            if (cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
            }

            return request.GetResponse() as HttpWebResponse;
        }
        protected override CookieImportResult ProtectedGetCookies(Uri targetUrl)
        {
            if (IsAvailable == false)
                return new CookieImportResult(null,CookieImportState.Unavailable);
            try
            {
                var formatVersionRec = LookupEntry(SourceInfo.CookiePath, SELECT_QUERY_VERSION);
                int formatVersion;
                if (formatVersionRec.Count == 0
                    || formatVersionRec[0].Length == 0
                    || int.TryParse((string)formatVersionRec[0][0], out formatVersion) == false)
                    return new CookieImportResult(null,CookieImportState.ConvertError);

                string query;
                query = formatVersion < 7 ? SELECT_QUERY : SELECT_QUERY_V7;
                query = string.Format("{0} {1} ORDER BY creation_utc DESC", query, MakeWhere(targetUrl));
                var cookies = new CookieCollection();
                foreach (var item in LookupCookies(SourceInfo.CookiePath, query, rec => DataToCookie(rec, formatVersion)))
                    cookies.Add(item);
                return new CookieImportResult(cookies, CookieImportState.Success);
            }
            catch (CookieImportException ex)
            {
                TraceError(this, "取得に失敗しました。", ex.ToString());
                return new CookieImportResult(null, ex.Result);
            }
        }
예제 #16
0
파일: Rest.xaml.cs 프로젝트: stiano/sl4
        public Rest()
        {
            InitializeComponent();

            // manual
            var request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri("http://localhost:51401/SilverlightApplication1TestPage.html"));
            //var request2 = (HttpWebRequest)WebRequestCreator.BrowserHttp.Create(new Uri("http://localhost:51401/SilverlightApplication1TestPage.html"));

            // automatic
            WebRequest.RegisterPrefix("http://localhost:51401/", WebRequestCreator.ClientHttp);
            //WebRequest.RegisterPrefix("http://localhost:51401/", WebRequestCreator.BrowserHttp);

            // security
            //request.UseDefaultCredentials = false;
            //request.Credentials = new NetworkCredential("stiano", "mysecretpw", "domain");

            // cookies
            var cookies =
                new CookieCollection
                {
                    new Cookie("firstName", "Pete"),
                    new Cookie("lastName", "Brown"),
                    new Cookie("lastAccess", DateTime.Now.ToString(CultureInfo.InvariantCulture))
                };

            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(new Uri("http://localhost:51401/SilverlightApplication1TestPage.html"), cookies);

            request.BeginGetResponse(OnRequestCompleted, request);
        }
예제 #17
0
 /// <summary>
 /// GET请求
 /// </summary>
 /// <param name="url">请求的URL</param>
 /// <param name="encoding">字符集编码,默认UTF-8</param>
 /// <param name="timeout">超时的时间,单是秒</param>
 /// <param name="userAgent">代理字符串</param>
 /// <param name="cookies">Cookie设置</param>
 /// <returns>返回字符串</returns>
 public static string Get(string url, Encoding encoding, int? timeout, string userAgent, CookieCollection cookies)
 {
     string res = string.Empty;
     if (encoding == null)
     {
         encoding = DefaultEncoding;
     }
     try
     {
         HttpWebResponse response = CreateGetHttpResponse(url, timeout, userAgent, cookies);
         using (var stream = response.GetResponseStream())
         {
             if (stream != null)
             {
                 var reader = new StreamReader(stream, encoding);
                 var sb = new StringBuilder();
                 while (-1 != reader.Peek())
                 {
                     sb.Append(reader.ReadLine());
                 }
                 res = sb.ToString();
             }
         }
         response.Close();
     }
     catch (Exception ex)
     {
         res = ex.Message;
     }
     return res;
 }
예제 #18
0
        public bool Login(string login, string password)
        {
            AvitoWebClient web = new AvitoWebClient(new CookieContainer());

            web.Headers.Add("Content-Type", "multipart/form-data; boundary=bound");
            web.Headers.Add("Referer", "https://www.avito.ru/profile/login?next=%2Fprofile");
            string data = "--bound\n" +
                            "Content-Disposition: form-data; name=\"next\"\n" +
                            "\n" +
                            "/profile\n" +
                            "--bound\n" +
                            "Content-Disposition: form-data; name=\"login\"\n" +
                            "\n" +
                            "[email protected]\n" +
                            "--bound\n" +
                            "Content-Disposition: form-data; name=\"password\"\n" +
                            "\n" +
                            "kirillov4ever\n" +
                            "--bound\n";

            try
            {
                web.UploadString("https://www.avito.ru/profile/login", data);
            }
            catch(Exception e)
            {
                return false;
            }

            creds = web.Container.GetCookies(new Uri("http://www.avito.ru"));

            return true;
        }
예제 #19
0
        /// <summary>
        /// 获取网页HTML代码
        /// </summary>
        /// <param name="url">网址</param>
        /// <param name="cookie">cookie</param>
        /// <returns></returns>
        public static string GetHTML(string url, CookieCollection cookie)
        {
            CookieContainer cc = new CookieContainer();
            cc.Add(cookie);

            Uri uri = new Uri(url);
            HttpWebRequest hwReq = (HttpWebRequest)WebRequest.Create(uri);
            hwReq.CookieContainer = cc;

            HttpWebResponse hwRes;

            try
            {
                hwRes = (HttpWebResponse)hwReq.GetResponse();
            }
            catch
            {
                return "NONE";
            }

            hwReq.Method = "Get";
            hwReq.KeepAlive = false;

            StreamReader reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
            return reader.ReadToEnd();
        }
예제 #20
0
 public MovieStream()
 {
     Cookies      = new System.Net.CookieCollection();
     Headers      = new Dictionary <string, string>();
     VideoStreams = new List <VideoStream>();
     Captions     = new List <Caption>();
 }
예제 #21
0
        public MyWebRequest(string url, string method, string data, CookieCollection cookies = null)
            : this(url, method)
        {
            // Create POST data and convert it to a byte array.
            string postData = data;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";

            // Set the Cookie if have.
            request.CookieContainer = new CookieContainer();
            if (cookies != null) request.CookieContainer.Add(cookies); //recover cookies First request

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            // Get the request stream.
            dataStream = request.GetRequestStream();

            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);

            // Close the Stream object.
            dataStream.Close();
        }
        /// <summary>
        /// Fills the cookie collection with tests.
        /// </summary>
        /// <param name="cookies"> The cookie collection.</param>
        /// <returns> The updated cookie collection.</returns>
        public CookieCollection FillCookies(CookieCollection cookies)
        {
            string buffer = this.SqlValue;

            CookieCollection tempCookies = new CookieCollection();

            foreach ( Cookie cky in cookies )
            {
                Cookie temp = new Cookie(cky.Name,"");
                temp.Comment = buffer;
                temp.CommentUri = cky.CommentUri;
                temp.Domain = cky.Domain;
                temp.Expired = cky.Expired;
                temp.Expires = cky.Expires;
                //temp.Name = temp.Name;
                temp.Path = cky.Path;
                temp.Port = cky.Port;
                temp.Secure = cky.Secure;
                temp.Value = buffer;
                temp.Version = cky.Version;

                tempCookies.Add(temp);
            }

            return tempCookies;
        }
예제 #23
0
파일: Session.cs 프로젝트: juhan/NModel
        public string getQuery(string url, string vars)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (vars.Length > 0 ? "?" + vars : ""));
            AddStandardHeaders(request);

            request.Method = "GET";
            request.ContentType = "text/xml; encoding='utf-8'";
            request.CookieContainer = new CookieContainer();
            if (cookies != null)
                foreach (Cookie c in cookies)
                {
                    request.CookieContainer.Add(c);
                }
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.Cookies.Count > 0)
                    cookies = response.Cookies;
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);

                string page = "";
                while (!reader.EndOfStream)
                    page += reader.ReadLine();
                reader.Close();
                response.Close();
                return page;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
예제 #24
0
 public static CapitalDistributionDetail FindCapitalDistributionDetail(CookieCollection cookies, int fundID, decimal? capitalDistributionAmount, DateTime? capitalDistributionDate, DateTime? capitalDistributionDueDate)
 {
     CapitalDistributionDetail detail = null;
     // Send the request
     string query = string.Empty;
     string resp = string.Empty;
     query = "&fundId=" + fundID + "&capitalDistributionAmount=" + capitalDistributionAmount + "&capitalDistributionDate=" + capitalDistributionDate + "&capitalDistributionDueDate=" + capitalDistributionDueDate;
     string url = HttpWebRequestUtil.GetUrl("CapitalCall/FindCapitalDistributionDetail?" + query);
     HttpWebResponse response = HttpWebRequestUtil.SendRequest(url, null, false, cookies, false, HttpWebRequestUtil.JsonContentType);
     if (response.StatusCode == System.Net.HttpStatusCode.OK) {
         using (Stream receiveStream = response.GetResponseStream()) {
             // Pipes the stream to a higher level stream reader with the required encoding format.
             using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) {
                 resp = readStream.ReadToEnd();
                 if (!string.IsNullOrEmpty(resp)) {
                     JavaScriptSerializer js = new JavaScriptSerializer();
                     detail = (CapitalDistributionDetail)js.Deserialize(resp, typeof(CapitalDistributionDetail));
                 }
                 else {
                 }
                 response.Close();
                 readStream.Close();
             }
         }
     }
     return detail;
 }
예제 #25
0
        public static string Post(string url, IDictionary<string, string> parameters, CookieCollection cookies)
        {
            var postData = ParametersToWWWFormURLEncoded(parameters);
            var postDataBytes = Encoding.ASCII.GetBytes(postData);

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

            if (cookies != null)
            {
                var container = new CookieContainer();
                container.Add(cookies);
                request.CookieContainer = container;
            }

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataBytes.Length;

            var outputStream = request.GetRequestStream();
            outputStream.Write(postDataBytes, 0, postDataBytes.Length);
            outputStream.Close();

            var response = (HttpWebResponse)request.GetResponse();
            var responseStreamReader = new StreamReader(response.GetResponseStream());
            return responseStreamReader.ReadToEnd().Trim();
        }
예제 #26
0
        public static void AddNewCookieContainer(int userId, CookieCollection cookieCollection)
        {
            if (instance.globalCookieCollection.ContainsKey(userId))
            {
                //System.Threading.Monitor.Enter(this);
                //try
                //{
                    instance.globalCookieCollection[userId].Add(cookieCollection);
                //}
                //finally
                //{
                //    System.Threading.Monitor.Exit(this);
                //}

            }
            else
                //System.Threading.Monitor.Enter(this);
                //try
                //{
                    instance.globalCookieCollection.Add(userId, cookieCollection);
                //}
                //finally
                //{
                //    System.Threading.Monitor.Exit(this);
                //}

            return;
        }
예제 #27
0
파일: Cookies.cs 프로젝트: labeuze/source
 public static IEnumerable<Cookie> GetCookies(CookieCollection cookies)
 {
     foreach (Cookie cookie in cookies)
     {
         yield return cookie;
     }
 }
예제 #28
0
파일: Util.cs 프로젝트: jujugy/bhsc_o
 public string Httppost(string url, string postData)
 {
     Encoding encoding = Encoding.GetEncoding("utf-8");
     byte[] bytesToPost = encoding.GetBytes(postData);
     CookieContainer cookieCon = new CookieContainer();
     if (cookie.Count > 0)
     {
         cookieCon.Add(new Uri(url), cookie);
     }
     HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
     httpRequest.CookieContainer = cookieCon;
     httpRequest.ContentType = "application/x-www-form-urlencoded";
     httpRequest.Method = "POST";
     httpRequest.ContentLength = bytesToPost.Length;
     Stream requestStream = httpRequest.GetRequestStream();
     requestStream.Write(bytesToPost, 0, bytesToPost.Length);
     requestStream.Close();
     Stream responseStream = httpRequest.GetResponse().GetResponseStream();
     string stringResponse = string.Empty;
     StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
     stringResponse = responseReader.ReadToEnd();
     responseReader.Close();
     cookie = httpRequest.CookieContainer.GetCookies(new Uri(url));
     return stringResponse;
 }
예제 #29
0
        public static void AddCommonHeadersToHttpRequest(
            HttpWebRequest request, CookieCollection cookies, RequestHeaders headers, Uri baseUri,
            string referer, string method, bool followRedirect = true)
        {
            // add cookies to request
            var cookieContainer = new CookieContainer();
            if (cookies != null) cookieContainer.Add(cookies);
            request.CookieContainer = cookieContainer;

            request.Method = method;
            request.AllowAutoRedirect = followRedirect;
            request.KeepAlive = true;
            request.ContentType = headers.ContentType;
            request.PreAuthenticate = headers.PreAuthenticate;
            request.Host = headers.Host;
            request.UserAgent = headers.UserAgent;
            request.Accept = headers.Accept;
            request.Headers.Add("Accept-Language", headers.AcceptLanguage);
            request.Headers.Add("Accept-Encoding", headers.AcceptEncoding);
            request.Headers.Add("Accept-Charset", headers.AcceptCharset);

            Uri uriResult;
            if (Uri.TryCreate(baseUri, referer, out uriResult))
                request.Referer = uriResult.AbsoluteUri;
        }
예제 #30
0
        private void CasAuthenticate(Site site, ref CookieCollection cookies, ref HtmlDocument document)
        {
            // read the parameters we need to know
            var form = document.GetElementbyId("fm1");
            var hidden = document.DocumentNode.Descendants("input").Where(a => a.Attributes["type"].Value == "hidden");

            var parameters = new StringBuilder();

            foreach (var p in hidden)
            {
                parameters.Append(string.Format("{0}={1}&", p.Attributes["name"].Value, p.Attributes["value"].Value));
            }

            var action = form.Attributes["action"];
            var username = ConfigurationSettings.AppSettings["username"];
            var password = ConfigurationSettings.AppSettings["password"];

            parameters.Append(string.Format("{0}={1}&", "username", username));
            parameters.Append(string.Format("{0}={1}", "password", password));

            // location to redirect back to the application
            var redirectLocation = string.Empty;
            MakeWebCallWithParameters("https://cas.ucdavis.edu:8443" + action.Value, parameters.ToString(), ref cookies, out redirectLocation);

            // get the ticket
            var ticketUrl = string.Format("https://cas.ucdavis.edu:8443/cas/login?service={0}", redirectLocation);
            var location = string.Empty;
            ErrorTypes errorType;
            MakeWebCall(ticketUrl, false, ref cookies, ref document, out location, out errorType);

            // get the aspx auth id
            var nothing = string.Empty;
            MakeWebCall(location, false, ref cookies, ref document, out nothing, out errorType);
        }
예제 #31
0
		/// <summary>
		/// urlに関連付けられたクッキーを取得します。
		/// </summary>
        public override CookieCollection GetCookieCollection(Uri url)
        {
            // 関係のあるファイルだけ調べることによってパフォーマンスを向上させる
            List<string> files = SelectFiles(url, GetAllFiles());
            List<Cookie> cookies = new List<Cookie>();

            foreach (string filepath in files)
            {
                cookies.AddRange(PickCookiesFromFile(filepath));
            }

            // Expiresが最新のもので上書きする
            cookies.Sort(CompareCookieExpiresAsc);
            CookieCollection collection = new CookieCollection();
            foreach (Cookie cookie in cookies)
            {
                try
                {
                    collection.Add(cookie);
                }
                catch (Exception ex)
                {
                    CookieGetter.Exceptions.Enqueue(ex);
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }

            return collection;
        }
예제 #32
0
        //See also overload for HttpCookieCollection
        public static string CookieCollectionAsString(System.Net.CookieCollection cookies, string sComment, LoggingOutputLevel level)
        {
            StringBuilder sb = new StringBuilder();

            //            string sRet = "";
            if (!String.IsNullOrEmpty(sComment))
            {
                sb.Append(sComment + Environment.NewLine);
            }
            sb.AppendFormat("Cookies collection. Count={0}\n", cookies.Count.ToString());
            // Print the properties of each cookie.
            foreach (Cookie cookie1 in cookies)
            {
                if (level > LoggingOutputLevel.Brief)
                {
                    sb.AppendFormat("{0} = {1}\n", cookie1.Name, cookie1.Value);
                    sb.AppendFormat("Domain: {0}\n", cookie1.Domain);
                    sb.AppendFormat("Path: {0}\n", cookie1.Path);
                    sb.AppendFormat("Port: {0}\n", cookie1.Port);
                    sb.AppendFormat("Secure: {0}\n", cookie1.Secure);
                    sb.AppendFormat("When issued: {0}\n", cookie1.TimeStamp);
                    sb.AppendFormat("Expires: {0} (expired? {1})", cookie1.Expires, cookie1.Expired);
                    sb.AppendFormat("Don't save: {0}\n", cookie1.Discard);
                    sb.AppendFormat("Comment: {0}\n", cookie1.Comment);
                    sb.AppendFormat("Uri for comments: {0}\n", cookie1.CommentUri);
                    sb.AppendFormat("Version: RFC {0}\n", cookie1.Version == 1 ? "2109" : "2965");
                }
                // Show the string representation of the cookie.
                sb.AppendFormat("String: {0}", cookie1.ToString());
            }
            return(sb.ToString());
        }
        /// <summary>  
        /// 创建POST方式的HTTP请求  
        /// </summary>  
        /// <param name="url">请求的URL</param>  
        /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>  
        /// <param name="timeout">请求的超时时间</param>  
        /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
        /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>  
        /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
        /// <returns></returns>  
        public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            if (requestEncoding == null)
            {
                throw new ArgumentNullException("requestEncoding");
            }
            HttpWebRequest request = null;

            request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            if (!string.IsNullOrEmpty(userAgent))
            {
                request.UserAgent = userAgent;
            }
            else
            {
                request.UserAgent = DefaultUserAgent;
            }

            if (timeout.HasValue)
            {
                request.Timeout = timeout.Value;
            }
            if (cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
            }
            //如果需要POST数据  
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder();
                int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, parameters[key]);
                    }
                    i++;
                }
                byte[] data = requestEncoding.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return request.GetResponse() as HttpWebResponse;
        }
        public string GetHttpPage(string strUrl, string strEncode, string strType,
            string strCookie, string strData, string strReferer, string strAcceptType,
            string strExtendHead, CookieCollection cookies)
        {
            try
            {
                Stream stream = GetHttpPageSteam(strUrl, strEncode, strType, strCookie, strData, strReferer, strAcceptType, strExtendHead, cookies);
                while (m_bRedirect)
                {
                    stream = GetHttpPageSteam(m_strRedirectUrl, strEncode, "get", strCookie, "", "", strAcceptType, strExtendHead, null);
                }
                if (stream == null)
                {
                    return "";
                }

                StreamReader sr = new StreamReader(stream, strEncode == "gb2312" ? Encoding.GetEncoding("gb2312") : Encoding.UTF8);
                string retStr = sr.ReadToEnd();
                sr.Close();
                return retStr; // UnZip(retStr);
            }
            catch (System.Exception e)
            {
                return "";
            }
        }
예제 #35
0
 public static void PrintCookies(Uri uri, CookieContainer container)
 {
     if (container == null)
     {
         DebugOutputHelper.TracedLine("CookieContainer is null");
         return;
     }
     System.Net.CookieCollection cookies = container.GetCookies(uri);
     PrintCookies(cookies, "PrintCookies for Uri " + uri.ToString());
 }
예제 #36
0
 public HttpResponse(Uri uri, System.Net.HttpStatusCode httpStatusCode, string sourceCode, System.Net.CookieCollection cookieCollection, TimeSpan responseTime, System.Net.HttpWebRequest httpWebRequest, System.Net.HttpWebResponse httpWebResponse)
 {
     // TODO: Complete member initialization
     this.uri              = uri;
     this.httpStatusCode   = httpStatusCode;
     this.SourceCode       = sourceCode;
     this.cookieCollection = cookieCollection;
     this.ResponseTime     = responseTime;
     this.httpWebRequest   = httpWebRequest;
     this.httpWebResponse  = httpWebResponse;
 }
예제 #37
0
        public static void HttpDownload(string savefilename, string url, string referer = "", InputFormRef.AutoPleaseWait pleaseWait = null, System.Net.CookieContainer cookie = null)
        {
            HttpWebRequest request = HttpMakeRequest(url, referer, cookie);

            WebResponse rsp = request.GetResponse();

            using (Stream output = File.OpenWrite(savefilename))
                using (Stream input = rsp.GetResponseStream())
                {
                    byte[] buffer        = new byte[1024 * 8];
                    int    totalSize     = (int)rsp.ContentLength;
                    int    readTotalSize = 0;
                    int    bytesRead;
                    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        output.Write(buffer, 0, bytesRead);

                        if (pleaseWait != null)
                        {
                            readTotalSize += bytesRead;
                            if (totalSize == -1)
                            {
                                pleaseWait.DoEvents("Download: " + readTotalSize + "/" + "???");
                            }
                            else
                            {
                                pleaseWait.DoEvents("Download: " + readTotalSize + "/" + totalSize);
                            }
                        }
                    }
                }

            rsp.Close();

            if (cookie != null)
            {
                System.Net.CookieCollection cookies = request.CookieContainer.GetCookies(request.RequestUri);
                cookie.Add(cookies);
            }
        }
예제 #38
0
        //httpでそこそこ怪しまれずに通信する
        public static string HttpGet(string url, string referer = "", System.Net.CookieContainer cookie = null)
        {
            HttpWebRequest request = HttpMakeRequest(url, referer, cookie);
            string         r       = "";

            WebResponse rsp = request.GetResponse();
            Stream      stm = rsp.GetResponseStream();

            if (stm != null)
            {
                StreamReader reader = new StreamReader(stm, Encoding.UTF8);
                r = reader.ReadToEnd();
                stm.Close();
            }
            rsp.Close();

            if (cookie != null)
            {
                System.Net.CookieCollection cookies = request.CookieContainer.GetCookies(request.RequestUri);
                cookie.Add(cookies);
            }

            return(r);
        }
예제 #39
0
        internal CookieCollection InternalGetCookies(Uri uri)
        {
            bool             isSecure    = uri.Scheme == Uri.UriSchemeHttps;
            int              port        = uri.Port;
            CookieCollection destination = new CookieCollection();
            ArrayList        arrayList   = new ArrayList();
            string           host        = uri.Host;
            int              startIndex  = host.IndexOf('.');
            int              num1;

            if (startIndex == -1)
            {
                arrayList.Add((object)host);
                arrayList.Add((object)("." + host));
                if (this.m_fqdnMyDomain != null && this.m_fqdnMyDomain.Length != 0)
                {
                    arrayList.Add((object)(host + this.m_fqdnMyDomain));
                    arrayList.Add((object)this.m_fqdnMyDomain);
                    num1 = 3;
                }
                else
                {
                    num1 = 1;
                }
            }
            else
            {
                arrayList.Add((object)host);
                arrayList.Add((object)("." + host));
                arrayList.Add((object)host.Substring(startIndex));
                num1 = 2;
                if (host.Length > 2)
                {
                    int num2 = host.LastIndexOf('.', host.Length - 2);
                    if (num2 > 0)
                    {
                        num2 = host.LastIndexOf('.', num2 - 1);
                    }
                    if (num2 != -1)
                    {
                        while (startIndex < num2 && (startIndex = host.IndexOf('.', startIndex + 1)) != -1)
                        {
                            arrayList.Add((object)host.Substring(startIndex));
                        }
                    }
                }
            }
            foreach (string key in arrayList)
            {
                bool     flag1    = false;
                bool     flag2    = false;
                PathList pathList = (PathList)this.m_domainTable[(object)key];
                --num1;
                if (pathList != null)
                {
                    foreach (DictionaryEntry dictionaryEntry in pathList)
                    {
                        string str = (string)dictionaryEntry.Key;
                        if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(str)))
                        {
                            flag1 = true;
                            CookieCollection source = (CookieCollection)dictionaryEntry.Value;
                            source.TimeStamp(CookieCollection.Stamp.Set);
                            this.MergeUpdateCollections(destination, source, port, isSecure, num1 < 0);
                            if (str == "/")
                            {
                                flag2 = true;
                            }
                        }
                        else if (flag1)
                        {
                            break;
                        }
                    }
                    if (!flag2)
                    {
                        CookieCollection source = (CookieCollection)pathList["/"];
                        if (source != null)
                        {
                            source.TimeStamp(CookieCollection.Stamp.Set);
                            this.MergeUpdateCollections(destination, source, port, isSecure, num1 < 0);
                        }
                    }
                    if (pathList.Count == 0)
                    {
                        this.AddRemoveDomain(key, (PathList)null);
                    }
                }
            }
            return(destination);
        }
예제 #40
0
        public async void Vote(string voteUri, int selected_choice)
        {
            try
            {
                handler = new HttpClientHandler();
                handler.CookieContainer        = CookieContainer;
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                clientLogin = new HttpClient(handler);


                clientLogin.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
                clientLogin.DefaultRequestHeaders.Add("authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA");
                clientLogin.DefaultRequestHeaders.Add("x-twitter-auth-type", "OAuth2Session");
                clientLogin.DefaultRequestHeaders.Add("x-twitter-active-user", "yes");
                clientLogin.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
                clientLogin.DefaultRequestHeaders.Add("X-MicrosoftAjax", "Delta=true");

                int index = voteUri.IndexOf("?");
                if (index > 0)
                {
                    voteUri = voteUri.Substring(0, index);
                }
                string tweet_id = voteUri.Split('/')[voteUri.Split('/').Length - 1];

                Response res = getRequest("https://twitter.com/");
                //var matches = Regex.Matches(res.Page, "ct0=(.*?);");
                string ct0 = ""; // matches[0].Groups[1].Value;

                var card = getRequest($"https://twitter.com/i/cards/tfw/v1/{tweet_id}", voteUri);

                string card_uri = getJsonValue(card.Page, "card_uri");
                //string tweet_id = getJsonValue(card.Page, "tweet_id");
                string card_name = getJsonValue(card.Page, "card_name");
                string token     = getJsonValue(card.Page, "token");
                string capi      = getJsonValue(card.Page, "capi");
                //

                Uri uri = new Uri("https://www.twitter.com");
                System.Net.CookieCollection cookies = CookieContainer.GetCookies(new Uri("https://twitter.com"));
                foreach (System.Net.Cookie cookie in cookies)
                {
                    if (cookie.Name == "ct0")
                    {
                        clientLogin.DefaultRequestHeaders.Add("x-csrf-token", cookie.Value);

                        break;
                    }
                }



                var values = new Dictionary <string, string>
                {
                    { "twitter:string:card_uri", card_uri },
                    { "twitter:long:original_tweet_id", tweet_id },
                    { "twitter:string:response_card_name", card_name },
                    { "twitter:string:cards_platform", "Web-12" },
                    { "twitter:string:selected_choice", selected_choice.ToString() }
                };

                var content = new FormUrlEncodedContent(values);

                var response = await clientLogin.PostAsync("https://caps.twitter.com/v2/capi/passthrough/1", content);

                var responseString = response.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error Voting Code #6662 " + Environment.NewLine + ex.Message);
            }
        }
 public void AddCookies(System.Net.CookieCollection cookies)
 {
     throw new NotImplementedException();
 }
예제 #42
0
        // This method is called *only* when cookie verification is done, so unlike with public
        // Add(Cookie cookie) the cookie is in a reasonable condition.
        internal void Add(Cookie cookie, bool throwOnError)
        {
            PathList pathList;

            if (cookie.Value.Length > m_maxCookieSize)
            {
                if (throwOnError)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_size, cookie.ToString(), m_maxCookieSize));
                }
                return;
            }

            try
            {
                lock (m_domainTable.SyncRoot)
                {
                    pathList = (PathList)m_domainTable[cookie.DomainKey];
                    if (pathList == null)
                    {
                        m_domainTable[cookie.DomainKey] = (pathList = new PathList());
                    }
                }
                int domain_count = pathList.GetCookiesCount();

                CookieCollection cookies;
                lock (pathList.SyncRoot)
                {
                    cookies = (CookieCollection)pathList[cookie.Path];

                    if (cookies == null)
                    {
                        cookies = new CookieCollection();
                        pathList[cookie.Path] = cookies;
                    }
                }

                if (cookie.Expired)
                {
                    // Explicit removal command (Max-Age == 0)
                    lock (cookies)
                    {
                        int idx = cookies.IndexOf(cookie);
                        if (idx != -1)
                        {
                            cookies.RemoveAt(idx);
                            --m_count;
                        }
                    }
                }
                else
                {
                    // This is about real cookie adding, check Capacity first
                    if (domain_count >= m_maxCookiesPerDomain && !AgeCookies(cookie.DomainKey))
                    {
                        return; // Cannot age: reject new cookie
                    }
                    else if (m_count >= m_maxCookies && !AgeCookies(null))
                    {
                        return; // Cannot age: reject new cookie
                    }

                    // About to change the collection
                    lock (cookies)
                    {
                        m_count += cookies.InternalAdd(cookie, true);
                    }
                }
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception e)
            {
                if (throwOnError)
                {
                    throw new CookieException(SR.net_container_add_cookie, e);
                }
            }
        }
예제 #43
0
        public bool CreateThread(string title, string name, string mailAddress, string contents)
        {
            Match  m                = GetRegexMatchURL();
            string url              = null;
            string postData         = null;
            string referer          = ThreadURL;
            int    jbbsBaseIndex    = 1;
            bool   isThreadCreation = title != null;

            if (!m.Success)
            {
                if (isThreadCreation)
                {
                    m = GetRegexMatchBaseURL();

                    if (!m.Success)
                    {
                        return(false);
                    }
                    jbbsBaseIndex = 0;
                    referer       = BaseURL;
                }
                else
                {
                    return(false);
                }
            }

            CookieContainer cookieContainer = new CookieContainer();


            DateTime writeTime = DateTime.Now;
            DateTime orgTime   = DateTime.Parse("1970/1/1 00:00:00");

            int unixTime = (int)((writeTime.ToFileTimeUtc() - orgTime.ToFileTimeUtc()) / 10000000);

            for (int i = 0; i < 2; i++)
            {
                switch (Response.Style)
                {
                case Response.BBSStyle.jbbs:
                {
                    url = string.Format("{0}/bbs/write.cgi", m.Groups[1].Value);

                    string submitText      = "書き込む";
                    string additionalParam = "";

                    if (isThreadCreation)
                    {
                        submitText       = "新規スレッド作成";
                        additionalParam += "&SUBJECT=" + UrlEncode(title);
                    }
                    else
                    {
                        additionalParam += "&KEY==" + m.Groups[5].Value;
                    }

                    postData = string.Format("DIR={0}&BBS={1}&TIME={2}&NAME={3}&MAIL={4}&MESSAGE={5}&submit={6}" + additionalParam
                                             , m.Groups[2 + jbbsBaseIndex].Value
                                             , m.Groups[3 + jbbsBaseIndex].Value
                                             , unixTime
                                             , UrlEncode(name)
                                             , UrlEncode(mailAddress)
                                             , UrlEncode(contents)
                                             , UrlEncode(submitText)
                                             );

                    break;
                }

                case Response.BBSStyle.yykakiko:
                case Response.BBSStyle.nichan:
                {
                    url = string.Format("{0}/test/bbs.cgi", m.Groups[1].Value);

                    string submitText      = "書き込む";
                    string additionalParam = "";
                    string nameText        = name;
                    string subject         = "";

                    if (isThreadCreation)
                    {
                        submitText       = "新規スレッド作成";
                        subject          = title;
                        additionalParam += "&subject=" + UrlEncode(subject);
                    }
                    else
                    {
                        additionalParam += "&key=" + m.Groups[3].Value;
                    }



                    if (i == 1)
                    {
                        submitText = "上記全てを承諾して書き込む";
                    }

                    if (Response.Style == Response.BBSStyle.nichan)
                    {
                        additionalParam += "&tepo=don";
                    }
                    else
                    {
                        additionalParam += "&MIRV=kakkoii";
                    }



                    postData = string.Format("bbs={0}&time={1}&FROM={2}&mail={3}&MESSAGE={4}&submit={5}" + additionalParam
                                             , m.Groups[2].Value
                                             , unixTime
                                             , UrlEncode(nameText)
                                             , UrlEncode(mailAddress)
                                             , UrlEncode(contents)
                                             , UrlEncode(submitText)
                                             );

                    if (i == 1)
                    {
                        url += "?guid=ON";
                    }

                    if (Response.Style == Response.BBSStyle.nichan && isThreadCreation)
                    {
                        referer = url;
                    }
                    break;
                }
                }

                byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);

                System.Net.HttpWebRequest webReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                FormMain.UserConfig.SetProxy(webReq);

                webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)";

                //Cookieの設定
                webReq.CookieContainer = new CookieContainer();
                webReq.CookieContainer.Add(cookieContainer.GetCookies(webReq.RequestUri));
                //webReq.UserAgent = "Monazilla / 1.00(monaweb / 1.00)";

                //メソッドにPOSTを指定
                webReq.Method = "POST";

                //ContentTypeを"application/x-www-form-urlencoded"にする
                webReq.ContentType = "application/x-www-form-urlencoded";
                //POST送信するデータの長さを指定
                webReq.ContentLength = postDataBytes.Length;
                //
                webReq.Referer = referer;


                System.Net.HttpWebResponse webRes = null;

                try
                {
                    //データをPOST送信するためのStreamを取得
                    using (System.IO.Stream reqStream = webReq.GetRequestStream())
                    {
                        //送信するデータを書き込む
                        reqStream.Write(postDataBytes, 0, postDataBytes.Length);
                    }

                    webRes = (System.Net.HttpWebResponse)webReq.GetResponse();

                    //受信したCookieのコレクションを取得する
                    System.Net.CookieCollection cookies =
                        webReq.CookieContainer.GetCookies(webReq.RequestUri);
                    //Cookie名と値を列挙する
                    //foreach (System.Net.Cookie cook in cookies)
                    //{
                    //    Console.WriteLine("{0}={1}", cook.Name, cook.Value);
                    //}
                    //取得したCookieを保存しておく
                    cookieContainer.Add(cookies);


                    //応答データを受信するためのStreamを取得
                    System.IO.Stream resStream = webRes.GetResponseStream();
                    //受信して表示
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(resStream, GetEncoding()))
                    {
                        ReturnText = sr.ReadToEnd();
                    }

                    if (ReturnText.IndexOf("書き込み確認") >= 0)
                    {
                        //referer = url;
                        continue;
                    }

                    string temp = ReturnText.Replace("\n", "");
                    m = htmlBodyRegex.Match(temp);

                    if (m.Success)
                    {
                        ReturnText = Response.ConvertToText(m.Groups[1].Value);
                    }



                    if (ReturnText.IndexOf("ERROR") >= 0 || ReturnText.IndexOf("ERROR") >= 0)
                    {
                        return(false);
                    }


                    return(true);
                }
                catch (Exception e)
                {
                    ReturnText = e.Message;
                    return(false);
                }
            }

            return(false);
        }
예제 #44
0
 internal CookieCollectionEnumerator(CookieCollection cookies)
 {
     m_cookies = cookies;
     m_count   = cookies.Count;
     m_version = cookies.m_version;
 }
예제 #45
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CookieCollection"/> class.
 /// </summary>
 /// <param name="cookieCollection">The cookie collection.</param>
 public CookieCollection(System.Net.CookieCollection cookieCollection)
 {
     _cookieCollection = cookieCollection;
 }
예제 #46
0
        private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, ref CookieCollection cookies, System.Collections.Generic.List <string> domainAttribute, bool matchOnlyPlainCookie)
        {
            for (int i = 0; i < domainAttribute.Count; i++)
            {
                PathList pathList;
                lock (m_domainTable.SyncRoot)
                {
                    pathList = (PathList)m_domainTable[domainAttribute[i]];
                    if (pathList == null)
                    {
                        continue;
                    }
                }

                lock (pathList.SyncRoot)
                {
                    // Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations.
                    IDictionaryEnumerator e = pathList.GetEnumerator();
                    while (e.MoveNext())
                    {
                        string path = (string)e.Key;
                        if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(path)))
                        {
                            CookieCollection cc = (CookieCollection)e.Value;
                            cc.TimeStamp(CookieCollection.Stamp.Set);
                            MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);
                        }
                    }
                }

                // Remove unused domain
                // (This is the only place that does domain removal)
                if (pathList.Count == 0)
                {
                    lock (m_domainTable.SyncRoot)
                    {
                        m_domainTable.Remove(domainAttribute[i]);
                    }
                }
            }
        }
예제 #47
0
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
        {
            if (NetEventSource.IsEnabled)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Info(this, $"uri:{uri} headerName:{headerName} setCookieHeader:{setCookieHeader} isThrow:{isThrow}");
                }
            }

            CookieCollection cookies = new CookieCollection();
            CookieVariant    variant = CookieVariant.Unknown;

            if (headerName == null)
            {
                variant = CookieVariant.Default;
            }
            else
            {
                for (int i = 0; i < s_headerInfo.Length; ++i)
                {
                    if ((String.Compare(headerName, s_headerInfo[i].Name, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        variant = s_headerInfo[i].Variant;
                    }
                }
            }

            bool isLocalDomain = IsLocalDomain(uri.Host);

            try
            {
                CookieParser parser = new CookieParser(setCookieHeader);
                do
                {
                    Cookie cookie = parser.Get();
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Info(this, $"CookieParser returned cookie:{cookie}");
                    }

                    if (cookie == null)
                    {
                        break;
                    }

                    // Parser marks invalid cookies this way
                    if (String.IsNullOrEmpty(cookie.Name))
                    {
                        if (isThrow)
                        {
                            throw new CookieException(SR.net_cookie_format);
                        }
                        // Otherwise, ignore (reject) cookie
                        continue;
                    }

                    // This will set the default values from the response URI
                    // AND will check for cookie validity
                    if (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, true, isThrow))
                    {
                        continue;
                    }
                    // If many same cookies arrive we collapse them into just one, hence setting
                    // parameter isStrict = true below
                    cookies.InternalAdd(cookie, true);
                } while (true);
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception e)
            {
                if (isThrow)
                {
                    throw new CookieException(SR.Format(SR.net_cookie_parse_header, uri.AbsoluteUri), e);
                }
            }

            foreach (Cookie c in cookies)
            {
                Add(c, isThrow);
            }

            return(cookies);
        }
예제 #48
0
        private void MergeUpdateCollections(ref CookieCollection destination, CookieCollection source, int port, bool isSecure, bool isPlainOnly)
        {
            lock (source)
            {
                // Cannot use foreach as we are going to update 'source'
                for (int idx = 0; idx < source.Count; ++idx)
                {
                    bool to_add = false;

                    Cookie cookie = source[idx];

                    if (cookie.Expired)
                    {
                        // If expired, remove from container and don't add to the destination
                        source.RemoveAt(idx);
                        --m_count;
                        --idx;
                    }
                    else
                    {
                        // Add only if port does match to this request URI
                        // or was not present in the original response.
                        if (isPlainOnly && cookie.Variant != CookieVariant.Plain)
                        {
                            ; // Don't add
                        }
                        else if (cookie.PortList != null)
                        {
                            foreach (int p in cookie.PortList)
                            {
                                if (p == port)
                                {
                                    to_add = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            // It was implicit Port, always OK to add.
                            to_add = true;
                        }

                        // Refuse to add a secure cookie into an 'unsecure' destination
                        if (cookie.Secure && !isSecure)
                        {
                            to_add = false;
                        }

                        if (to_add)
                        {
                            // In 'source' are already ordered.
                            // If two same cookies come from different 'source' then they
                            // will follow (not replace) each other.
                            if (destination == null)
                            {
                                destination = new CookieCollection();
                            }
                            destination.InternalAdd(cookie, false);
                        }
                    }
                }
            }
        }
예제 #49
0
    public static string OpenLongTimeUrl(string TargetURL, string RefURL, string Body, string Method, System.Net.CookieCollection BrowCookie, Encoding ContactEncoding, bool AllowRedirect = false, bool KeepAlive = true, string ContentType = "application/json;charset=UTF-8", string authorization = "")
    {
        //System.Net.ServicePointManager.MaxServicePoints=20;

        System.Net.ServicePointManager.DefaultConnectionLimit = 500;

        System.Net.ServicePointManager.SetTcpKeepAlive(true, 15000, 15000);
        //HttpWebRequest LoginPage = null;
        //    GetHttpWebResponseNoRedirect(TargetURL,"","",out LoginPage);

        WebRequest LoginPage = HttpWebRequest.Create(TargetURL);

        ((HttpWebRequest)LoginPage).AllowAutoRedirect = AllowRedirect;
        ((HttpWebRequest)LoginPage).KeepAlive         = KeepAlive;
        //SetHeaderValue(((HttpWebRequest)LoginPage).Headers, "Connection", "Keep-Alive");
        ((HttpWebRequest)LoginPage).Timeout     = 30000;
        ((HttpWebRequest)LoginPage).Credentials = CredentialCache.DefaultCredentials;
        if (authorization != "")
        {
            LoginPage.Headers.Add("Authorization", authorization);
        }

        LoginPage.Method = Method;
        if (TargetURL.ToLower().StartsWith("https"))
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

            //System.Net.ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
            ((HttpWebRequest)LoginPage).ProtocolVersion = System.Net.HttpVersion.Version11;
        }

        switch (Method)
        {
        case "GET":
            // ((HttpWebRequest)LoginPage).Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel,application/json, text/plain, */*";
            ((HttpWebRequest)LoginPage).Accept    = "*/*";
            ((HttpWebRequest)LoginPage).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.40";
            LoginPage.Headers.Add("Accept-Encoding", "gzip, deflate,br");
            LoginPage.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            ((HttpWebRequest)LoginPage).CookieContainer = new CookieContainer();
            ((HttpWebRequest)LoginPage).CookieContainer.Add(BrowCookie);

            //((HttpWebRequest)LoginPage).Connection = "KeepAlive,Close";
            ((HttpWebRequest)LoginPage).Referer = RefURL;

            break;

        case "POST":
            ((HttpWebRequest)LoginPage).Accept    = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel,application/json, text/plain, */*";
            ((HttpWebRequest)LoginPage).Referer   = RefURL;
            ((HttpWebRequest)LoginPage).UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
            LoginPage.Headers.Add("Accept-Encoding", "gzip, deflate,br");
            LoginPage.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            ((HttpWebRequest)LoginPage).CookieContainer = new CookieContainer();
            ((HttpWebRequest)LoginPage).CookieContainer.Add(BrowCookie);
            ((HttpWebRequest)LoginPage).ContentType = ContentType;
            //((HttpWebRequest)LoginPage).ServicePoint.Expect100Continue = true;
            //((HttpWebRequest)LoginPage).Connection = "KeepAlive";
            if (((HttpWebRequest)LoginPage).Referer != null)
            {
                LoginPage.Headers.Add("Origin", ((HttpWebRequest)LoginPage).Referer.Substring(0, ((HttpWebRequest)LoginPage).Referer.Length - 1));
            }

            if (Body != "")
            {
                Stream bodys = LoginPage.GetRequestStream();

                byte[] text = ContactEncoding.GetBytes(Body);

                bodys.Write(text, 0, text.Length);

                bodys.Flush();
                bodys.Close();
            }
            break;

        case "OPTIONS":
            // ((HttpWebRequest)LoginPage).Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel,application/json, text/plain, */*";
            ((HttpWebRequest)LoginPage).Accept    = "*/*";
            ((HttpWebRequest)LoginPage).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.40";
            LoginPage.Headers.Add("Accept-Encoding", "gzip, deflate,br");
            LoginPage.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
            ((HttpWebRequest)LoginPage).CookieContainer = new CookieContainer();
            ((HttpWebRequest)LoginPage).CookieContainer.Add(BrowCookie);

            //((HttpWebRequest)LoginPage).Connection = "KeepAlive";
            ((HttpWebRequest)LoginPage).Referer = RefURL;
            LoginPage.Headers.Add("Origin", RefURL);

            break;

        default:
            break;
        }
        ((HttpWebRequest)LoginPage).KeepAlive = true;
        SetHeaderValue(((HttpWebRequest)LoginPage).Headers, "Connection", "Keep-Alive");
        LoginPage.Timeout = 30000;
        if (RefURL.ToLower().StartsWith("https"))
        {
            //System.Net.ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
            ((HttpWebRequest)LoginPage).ProtocolVersion = System.Net.HttpVersion.Version11;
        }
        //System.GC.Collect();
        System.Threading.Thread.Sleep(100);
        HttpWebResponse LoginPage_Return = null;

        try
        {
            //CurrentUrl = "正在下载" + TargetURL;
            //System.GC.Collect();

            // NetFramework.Console.WriteLine("下载URL" + LoginPage.RequestUri.AbsoluteUri + Environment.NewLine);
            LoginPage_Return = (HttpWebResponse)LoginPage.GetResponse();

            //CurrentUrl = "已下载" + TargetURL;

            if (((HttpWebResponse)LoginPage_Return).Headers["Set-Cookie"] != null)
            {
                string Start = LoginPage.RequestUri.Host.Substring(0, LoginPage.RequestUri.Host.LastIndexOf("."));
                string Host  = LoginPage.RequestUri.Host.Substring(LoginPage.RequestUri.Host.LastIndexOf("."));

                foreach (Cookie cookieitem in ((HttpWebResponse)LoginPage_Return).Cookies)
                {
                    string[] SplitDomain = cookieitem.Domain.Split((".").ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    Int32    Length      = SplitDomain.Length;
                    cookieitem.Domain  = "." + SplitDomain[Length - 2] + "." + SplitDomain[Length - 1];
                    cookieitem.Expires = cookieitem.Expires == null?DateTime.Now.AddHours(168) : cookieitem.Expires.AddHours(168);

                    BrowCookie.Add(cookieitem);
                }


                //CookieContainer NC = new CookieContainer();
                //NC.SetCookies(((HttpWebResponse)LoginPage_Return).ResponseUri, ((HttpWebResponse)LoginPage_Return).Headers["Set-Cookie"]);
                //BrowCookie.Add(NC.GetCookies(((HttpWebResponse)LoginPage_Return).ResponseUri));

                // Host = Start.Substring(Start.LastIndexOf(".")) + Host;
                // AddCookieWithCookieHead(tmpcookie, ((HttpWebResponse)LoginPage_Return).Headers["Set-Cookie"].Replace("Secure,", ""), Host);
            }
        }
        catch (Exception AnyError)
        {
            LoginPage = null;
            System.GC.Collect();

            //NetFramework.Console.WriteLine("网址打开失败" + TargetURL, false);
            //NetFramework.Console.WriteLine("网址打开失败" + AnyError.Message, false);
            //NetFramework.Console.WriteLine("网址打开失败" + AnyError.StackTrace, false);
            return(AnyError.Message);
        }

        string responseBody = string.Empty;

        try
        {
            if (LoginPage_Return.ContentEncoding.ToLower().Contains("gzip"))
            {
                using (GZipStream stream = new GZipStream(LoginPage_Return.GetResponseStream(), CompressionMode.Decompress))
                {
                    using (StreamReader reader = new StreamReader(stream, ContactEncoding))
                    {
                        responseBody = reader.ReadToEnd();
                        stream.Close();
                    }
                }
            }
            else if (LoginPage_Return.ContentEncoding.ToLower().Contains("deflate"))
            {
                using (DeflateStream stream = new DeflateStream(LoginPage_Return.GetResponseStream(), CompressionMode.Decompress))
                {
                    using (StreamReader reader = new StreamReader(stream, ContactEncoding))
                    {
                        responseBody = reader.ReadToEnd();
                        stream.Close();
                    }
                }
            }
            else if (LoginPage_Return.ContentEncoding.ToLower().Contains("br"))
            {
                using (Brotli.BrotliStream stream = new Brotli.BrotliStream(LoginPage_Return.GetResponseStream(), CompressionMode.Decompress))
                {
                    using (StreamReader reader = new StreamReader(stream, ContactEncoding))
                    {
                        responseBody = reader.ReadToEnd();
                        stream.Close();
                    }
                }
            }
            else
            {
                using (Stream stream = LoginPage_Return.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, ContactEncoding))
                    {
                        responseBody = reader.ReadToEnd();
                        stream.Close();
                    }
                }
            }
        }
        catch (Exception AnyError)
        {
            LoginPage.Abort();
            LoginPage = null;
            //System.GC.Collect();

            //NetFramework.Console.WriteLine("网址打开失败" + TargetURL, true);
            //NetFramework.Console.WriteLine("网址打开失败" + AnyError.Message, true);
            //NetFramework.Console.WriteLine("网址打开失败" + AnyError.StackTrace, true);
            return(AnyError.Message);
        }
        LoginPage.Abort();


        //  NetFramework.Console.WriteLine("下载完成" + LoginPage_Return.ResponseUri.AbsoluteUri + Environment.NewLine);

        LoginPage_Return.Close();
        LoginPage_Return = null;
        LoginPage        = null;
        System.GC.Collect();


        return(responseBody);
    }
예제 #50
0
        internal CookieCollection InternalGetCookies(Uri uri)
        {
            if (m_count == 0)
            {
                return(null);
            }

            bool             isSecure = (uri.Scheme == UriScheme.Https || uri.Scheme == UriScheme.Wss);
            int              port     = uri.Port;
            CookieCollection cookies  = null;

            var domainAttributeMatchAnyCookieVariant = new System.Collections.Generic.List <string>();

            System.Collections.Generic.List <string> domainAttributeMatchOnlyCookieVariantPlain = null;

            string fqdnRemote = uri.Host;

            // Add initial candidates to match Domain attribute of possible cookies.
            // For these Domains, cookie can have any CookieVariant enum value.
            domainAttributeMatchAnyCookieVariant.Add(fqdnRemote);
            domainAttributeMatchAnyCookieVariant.Add("." + fqdnRemote);

            int dot = fqdnRemote.IndexOf('.');

            if (dot == -1)
            {
                // DNS.resolve may return short names even for other inet domains ;-(
                // We _don't_ know what the exact domain is, so try also grab short hostname cookies.
                // Grab long name from the local domain
                if (m_fqdnMyDomain != null && m_fqdnMyDomain.Length != 0)
                {
                    domainAttributeMatchAnyCookieVariant.Add(fqdnRemote + m_fqdnMyDomain);
                    // Grab the local domain itself
                    domainAttributeMatchAnyCookieVariant.Add(m_fqdnMyDomain);
                }
            }
            else
            {
                // Grab the host domain
                domainAttributeMatchAnyCookieVariant.Add(fqdnRemote.Substring(dot));

                // The following block is only for compatibility with Version0 spec.
                // Still, we'll add only Plain-Variant cookies if found under below keys
                if (fqdnRemote.Length > 2)
                {
                    // We ignore the '.' at the end on the name
                    int last = fqdnRemote.LastIndexOf('.', fqdnRemote.Length - 2);
                    // AND keys with <2 dots inside.
                    if (last > 0)
                    {
                        last = fqdnRemote.LastIndexOf('.', last - 1);
                    }
                    if (last != -1)
                    {
                        while ((dot < last) && (dot = fqdnRemote.IndexOf('.', dot + 1)) != -1)
                        {
                            if (domainAttributeMatchOnlyCookieVariantPlain == null)
                            {
                                domainAttributeMatchOnlyCookieVariantPlain = new System.Collections.Generic.List <string>();
                            }

                            // These candidates can only match CookieVariant.Plain cookies.
                            domainAttributeMatchOnlyCookieVariantPlain.Add(fqdnRemote.Substring(dot));
                        }
                    }
                }
            }

            BuildCookieCollectionFromDomainMatches(uri, isSecure, port, ref cookies, domainAttributeMatchAnyCookieVariant, false);
            if (domainAttributeMatchOnlyCookieVariantPlain != null)
            {
                BuildCookieCollectionFromDomainMatches(uri, isSecure, port, ref cookies, domainAttributeMatchOnlyCookieVariantPlain, true);
            }

            return(cookies);
        }
예제 #51
0
 public HttpWebResponse()
 {
     _requestUri = null !;
     _cookies    = null !;
 }
예제 #52
0
        // This function, when called, must delete at least one cookie.
        // If there are expired cookies in given scope they are cleaned up.
        // If nothing is found the least used Collection will be found and removed
        // from the container.
        //
        // Also note that expired cookies are also removed during request preparation
        // (this.GetCookies method).
        //
        // Param. 'domain' == null means to age in the whole container.
        private bool AgeCookies(string domain)
        {
            Debug.Assert(m_maxCookies != 0);
            Debug.Assert(m_maxCookiesPerDomain != 0);

            int      removed = 0;
            DateTime oldUsed = DateTime.MaxValue;
            DateTime tempUsed;

            CookieCollection lruCc      = null;
            string           lruDomain  = null;
            string           tempDomain = null;

            PathList pathList;
            int      domain_count      = 0;
            int      itemp             = 0;
            float    remainingFraction = 1.0F;

            // The container was shrunk, might need additional cleanup for each domain
            if (m_count > m_maxCookies)
            {
                // Means the fraction of the container to be left.
                // Each domain will be cut accordingly.
                remainingFraction = (float)m_maxCookies / (float)m_count;
            }
            lock (m_domainTable.SyncRoot)
            {
                foreach (DictionaryEntry entry in m_domainTable)
                {
                    if (domain == null)
                    {
                        tempDomain = (string)entry.Key;
                        pathList   = (PathList)entry.Value; // Aliasing to trick foreach
                    }
                    else
                    {
                        tempDomain = domain;
                        pathList   = (PathList)m_domainTable[domain];
                    }

                    domain_count = 0; // Cookies in the domain
                    lock (pathList.SyncRoot)
                    {
                        foreach (CookieCollection cc in pathList.Values)
                        {
                            itemp         = ExpireCollection(cc);
                            removed      += itemp;
                            m_count      -= itemp; // Update this container's count
                            domain_count += cc.Count;

                            // We also find the least used cookie collection in ENTIRE container.
                            // We count the collection as LRU only if it holds 1+ elements.
                            if (cc.Count > 0 && (tempUsed = cc.TimeStamp(CookieCollection.Stamp.Check)) < oldUsed)
                            {
                                lruDomain = tempDomain;
                                lruCc     = cc;
                                oldUsed   = tempUsed;
                            }
                        }
                    }

                    // Check if we have reduced to the limit of the domain by expiration only.
                    int min_count = Math.Min((int)(domain_count * remainingFraction), Math.Min(m_maxCookiesPerDomain, m_maxCookies) - 1);
                    if (domain_count > min_count)
                    {
                        // This case requires sorting all domain collections by timestamp.
                        Array cookies;
                        Array stamps;
                        lock (pathList.SyncRoot)
                        {
                            cookies = Array.CreateInstance(typeof(CookieCollection), pathList.Count);
                            stamps  = Array.CreateInstance(typeof(DateTime), pathList.Count);
                            foreach (CookieCollection cc in pathList.Values)
                            {
                                stamps.SetValue(cc.TimeStamp(CookieCollection.Stamp.Check), itemp);
                                cookies.SetValue(cc, itemp);
                                ++itemp;
                            }
                        }
                        Array.Sort(stamps, cookies);

                        itemp = 0;
                        for (int i = 0; i < cookies.Length; ++i)
                        {
                            CookieCollection cc = (CookieCollection)cookies.GetValue(i);

                            lock (cc)
                            {
                                while (domain_count > min_count && cc.Count > 0)
                                {
                                    cc.RemoveAt(0);
                                    --domain_count;
                                    --m_count;
                                    ++removed;
                                }
                            }
                            if (domain_count <= min_count)
                            {
                                break;
                            }
                        }

                        if (domain_count > min_count && domain != null)
                        {
                            // Cannot complete aging of explicit domain (no cookie adding allowed).
                            return(false);
                        }
                    }
                }
            }

            // We have completed aging of the specified domain.
            if (domain != null)
            {
                return(true);
            }

            // The rest is for entire container aging.
            // We must get at least one free slot.

            // Don't need to apply LRU if we already cleaned something.
            if (removed != 0)
            {
                return(true);
            }

            if (oldUsed == DateTime.MaxValue)
            {
                // Something strange. Either capacity is 0 or all collections are locked with cc.Used.
                return(false);
            }

            // Remove oldest cookies from the least used collection.
            lock (lruCc)
            {
                while (m_count >= m_maxCookies && lruCc.Count > 0)
                {
                    lruCc.RemoveAt(0);
                    --m_count;
                }
            }
            return(true);
        }
예제 #53
0
        public string GetHtml(string url, string postData, bool isPost, string ip = null, bool isCountTime = false)
        {
            httpWebRequest                   = (HttpWebRequest)HttpWebRequest.Create(url);
            httpWebRequest.Headers           = this.Headers;
            httpWebRequest.CookieContainer   = CookieContainer;
            httpWebRequest.Referer           = Referer;
            httpWebRequest.Accept            = Accept;
            httpWebRequest.AllowAutoRedirect = true;
            if (this.IsGzip)
            {
                httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            }
            httpWebRequest.UserAgent = UserAgent;

            httpWebRequest.KeepAlive = this.IsKeepAlive;

            if (!string.IsNullOrEmpty(PostData))
            {
                httpWebRequest.Method      = "Post";
                httpWebRequest.ContentType = ContentType;
                byte[] byteRequest = this.Encoding.GetBytes(PostData);
                httpWebRequest.ContentLength = byteRequest.Length;
                using (Stream SendStream = httpWebRequest.GetRequestStream())
                {
                    SendStream.Write(byteRequest, 0, byteRequest.Length);
                }
            }
            else
            {
                httpWebRequest.Method = "GET";
            }
            try
            {
                IAsyncResult result = httpWebRequest.BeginGetResponse(null, null);
                bool         bl     = result.AsyncWaitHandle.WaitOne(this.Timeout, true);
                if (!bl)
                {
                    throw new WebException("主机连接超时", WebExceptionStatus.Timeout);
                }
                httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(result);

                this.ResponseContentLength = httpWebResponse.ContentLength;
                this.RequestHeader         = "GET " + httpWebRequest.RequestUri.PathAndQuery + " HTTP/" + httpWebRequest.ProtocolVersion + "\r\n" + httpWebRequest.Headers.ToString();
                this.HttpHeader            = "HTTP/" + httpWebResponse.ProtocolVersion + " " + Convert.ToInt32(httpWebResponse.StatusCode) + " " + httpWebResponse.StatusCode + "\r\n" + httpWebResponse.Headers.ToString();
                this.RequestHeader         = this.RequestHeader.Trim('\r', '\n');
                this.HttpHeader            = this.HttpHeader.Trim('\r', '\n');
                this.ResponseHeaders       = httpWebResponse.Headers;
                this.Cookies = httpWebResponse.Cookies;

                CompressType = httpWebResponse.ContentEncoding.ToLower();

                Stream stream = null;
                if (CompressType == "gzip")
                {
                    this.IsGzip = true;
                    stream      = new System.IO.Compression.GZipStream(httpWebResponse.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else if (CompressType == "deflate")
                {
                    this.IsGzip = true;
                    stream      = new System.IO.Compression.DeflateStream(httpWebResponse.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else
                {
                    this.IsGzip = false;
                    stream      = httpWebResponse.GetResponseStream();
                }
                if (Encoding != null)
                {
                    using (System.IO.StreamReader sr = new StreamReader(stream, Encoding))
                    {
                        return(sr.ReadToEnd());
                    }
                }
                else
                {
                    string html = DecodeData(httpWebResponse.Headers["content-type"], stream);
                    httpWebResponse.Close();
                    return(html);
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                httpWebRequest.Abort();
            }
        }
예제 #54
0
        public CookieCollection GetCookies(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            CheckExpiration();
            CookieCollection coll = new CookieCollection();

            if (cookies == null)
            {
                return(coll);
            }

            foreach (Cookie cookie in cookies)
            {
                string domain = cookie.Domain;
                if (!CheckDomain(domain, uri.Host, cookie.ExactDomain))
                {
                    continue;
                }

                if (cookie.Port.Length > 0 && cookie.Ports != null && uri.Port != -1)
                {
                    if (Array.IndexOf(cookie.Ports, uri.Port) == -1)
                    {
                        continue;
                    }
                }

                string path    = cookie.Path;
                string uripath = uri.AbsolutePath;
                if (path != "" && path != "/")
                {
                    if (uripath != path)
                    {
                        if (!uripath.StartsWith(path))
                        {
                            continue;
                        }

                        if (path [path.Length - 1] != '/' && uripath.Length > path.Length &&
                            uripath [path.Length] != '/')
                        {
                            continue;
                        }
                    }
                }

                if (cookie.Secure && uri.Scheme != "https")
                {
                    continue;
                }

                coll.Add(cookie);
            }

            coll.Sort();
            return(coll);
        }
예제 #55
0
        // This function, when called, must delete at least one cookie.
        // If there are expired cookies in given scope they are cleaned up.
        // If nothing is found the least used Collection will be found and removed
        // from the container.
        //
        // Also note that expired cookies are also removed during request preparation
        // (this.GetCookies method).
        //
        // Param. 'domain' == null means to age in the whole container.
        private bool AgeCookies(string domain)
        {
            Debug.Assert(_maxCookies != 0);
            Debug.Assert(_maxCookiesPerDomain != 0);

            int      removed = 0;
            DateTime oldUsed = DateTime.MaxValue;
            DateTime tempUsed;

            CookieCollection lruCc      = null;
            string           lruDomain  = null;
            string           tempDomain = null;

            PathList pathList;
            int      domain_count      = 0;
            int      itemp             = 0;
            float    remainingFraction = 1.0F;

            // The container was shrunk, might need additional cleanup for each domain
            if (_count > _maxCookies)
            {
                // Means the fraction of the container to be left.
                // Each domain will be cut accordingly.
                remainingFraction = (float)_maxCookies / (float)_count;
            }
            lock (_domainTable)
            {
                foreach (KeyValuePair <string, PathList> entry in _domainTable)
                {
                    if (domain == null)
                    {
                        tempDomain = entry.Key;
                        pathList   = entry.Value; // Aliasing to trick foreach
                    }
                    else
                    {
                        tempDomain = domain;
                        _domainTable.TryGetValue(domain, out pathList);
                    }

                    domain_count = 0; // Cookies in the domain
                    lock (pathList.SyncRoot)
                    {
                        foreach (KeyValuePair <string, CookieCollection> pair in pathList)
                        {
                            CookieCollection cc = pair.Value;
                            itemp         = ExpireCollection(cc);
                            removed      += itemp;
                            _count       -= itemp; // Update this container's count
                            domain_count += cc.Count;

                            // We also find the least used cookie collection in ENTIRE container.
                            // We count the collection as LRU only if it holds 1+ elements.
                            if (cc.Count > 0 && (tempUsed = cc.TimeStamp(CookieCollection.Stamp.Check)) < oldUsed)
                            {
                                lruDomain = tempDomain;
                                lruCc     = cc;
                                oldUsed   = tempUsed;
                            }
                        }
                    }

                    // Check if we have reduced to the limit of the domain by expiration only.
                    int min_count = Math.Min((int)(domain_count * remainingFraction), Math.Min(_maxCookiesPerDomain, _maxCookies) - 1);
                    if (domain_count > min_count)
                    {
                        // This case requires sorting all domain collections by timestamp.
                        KeyValuePair <DateTime, CookieCollection>[] cookies;
                        lock (pathList.SyncRoot)
                        {
                            cookies = new KeyValuePair <DateTime, CookieCollection> [pathList.Count];
                            foreach (KeyValuePair <string, CookieCollection> pair in pathList)
                            {
                                CookieCollection cc = pair.Value;
                                cookies[itemp] = new KeyValuePair <DateTime, CookieCollection>(cc.TimeStamp(CookieCollection.Stamp.Check), cc);
                                ++itemp;
                            }
                        }
                        Array.Sort(cookies, (a, b) => a.Key.CompareTo(b.Key));

                        itemp = 0;
                        for (int i = 0; i < cookies.Length; ++i)
                        {
                            CookieCollection cc = cookies[i].Value;

                            lock (cc)
                            {
                                while (domain_count > min_count && cc.Count > 0)
                                {
                                    cc.RemoveAt(0);
                                    --domain_count;
                                    --_count;
                                    ++removed;
                                }
                            }
                            if (domain_count <= min_count)
                            {
                                break;
                            }
                        }

                        if (domain_count > min_count && domain != null)
                        {
                            // Cannot complete aging of explicit domain (no cookie adding allowed).
                            return(false);
                        }
                    }
                }
            }

            // We have completed aging of the specified domain.
            if (domain != null)
            {
                return(true);
            }

            // The rest is for entire container aging.
            // We must get at least one free slot.

            // Don't need to apply LRU if we already cleaned something.
            if (removed != 0)
            {
                return(true);
            }

            if (oldUsed == DateTime.MaxValue)
            {
                // Something strange. Either capacity is 0 or all collections are locked with cc.Used.
                return(false);
            }

            // Remove oldest cookies from the least used collection.
            lock (lruCc)
            {
                while (_count >= _maxCookies && lruCc.Count > 0)
                {
                    lruCc.RemoveAt(0);
                    --_count;
                }
            }
            return(true);
        }
예제 #56
0
        internal void AddHeader(string header)
        {
            int colon = header.IndexOf(':');

            if (colon == -1 || colon == 0)
            {
                context.ErrorMessage = "Bad Request";
                context.ErrorStatus  = 400;
                return;
            }

            string name  = header.Substring(0, colon).Trim();
            string val   = header.Substring(colon + 1).Trim();
            string lower = name.ToLower(CultureInfo.InvariantCulture);

            headers.SetInternal(name, val);
            switch (lower)
            {
            case "accept-language":
                user_languages = val.Split(',');                          // yes, only split with a ','
                break;

            case "accept":
                accept_types = val.Split(',');                          // yes, only split with a ','
                break;

            case "content-length":
                try {
                    //TODO: max. content_length?
                    content_length = Int64.Parse(val.Trim());
                    if (content_length < 0)
                    {
                        context.ErrorMessage = "Invalid Content-Length.";
                    }
                    cl_set = true;
                } catch {
                    context.ErrorMessage = "Invalid Content-Length.";
                }

                break;

            case "referer":
                try {
                    referrer = new Uri(val);
                } catch {
                    referrer = new Uri("http://someone.is.screwing.with.the.headers.com/");
                }
                break;

            case "cookie":
                if (cookies == null)
                {
                    cookies = new CookieCollection();
                }

                string[] cookieStrings = val.Split(new char[] { ',', ';' });
                Cookie   current       = null;
                int      version       = 0;
                foreach (string cookieString in cookieStrings)
                {
                    string str = cookieString.Trim();
                    if (str.Length == 0)
                    {
                        continue;
                    }
                    if (str.StartsWith("$Version"))
                    {
                        version = Int32.Parse(Unquote(str.Substring(str.IndexOf('=') + 1)));
                    }
                    else if (str.StartsWith("$Path"))
                    {
                        if (current != null)
                        {
                            current.Path = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else if (str.StartsWith("$Domain"))
                    {
                        if (current != null)
                        {
                            current.Domain = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else if (str.StartsWith("$Port"))
                    {
                        if (current != null)
                        {
                            current.Port = str.Substring(str.IndexOf('=') + 1).Trim();
                        }
                    }
                    else
                    {
                        if (current != null)
                        {
                            cookies.Add(current);
                        }
                        current = new Cookie();
                        int idx = str.IndexOf('=');
                        if (idx > 0)
                        {
                            current.Name  = str.Substring(0, idx).Trim();
                            current.Value = str.Substring(idx + 1).Trim();
                        }
                        else
                        {
                            current.Name  = str.Trim();
                            current.Value = String.Empty;
                        }
                        current.Version = version;
                    }
                }
                if (current != null)
                {
                    cookies.Add(current);
                }
                break;
            }
        }
 public static int InternalAdd(this CookieCollection cookieCollection, Cookie cookie, bool isStrict)
 {
     s_internalAddFunc ??= (Func <CookieCollection, Cookie, bool, int>) typeof(CookieCollection).GetMethod("InternalAdd", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(Func <CookieCollection, Cookie, bool, int>));
     Debug.Assert(s_internalAddFunc != null, "Reflection failed for CookieCollection.InternalAdd().");
     return(s_internalAddFunc(cookieCollection, cookie, isStrict));
 }
예제 #58
0
        private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, ref CookieCollection cookies, List <string> domainAttribute, bool matchOnlyPlainCookie)
        {
            for (int i = 0; i < domainAttribute.Count; i++)
            {
                bool     found        = false;
                bool     defaultAdded = false;
                PathList pathList;
                lock (_domainTable)
                {
                    if (!_domainTable.TryGetValue(domainAttribute[i], out pathList))
                    {
                        continue;
                    }
                }

                lock (pathList.SyncRoot)
                {
                    foreach (KeyValuePair <string, CookieCollection> pair in pathList)
                    {
                        string path = pair.Key;
                        if (uri.AbsolutePath.StartsWith(CookieParser.CheckQuoted(path)))
                        {
                            found = true;

                            CookieCollection cc = pair.Value;
                            cc.TimeStamp(CookieCollection.Stamp.Set);
                            MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);

                            if (path == "/")
                            {
                                defaultAdded = true;
                            }
                        }
                        else if (found)
                        {
                            break;
                        }
                    }
                }

                if (!defaultAdded)
                {
                    CookieCollection cc = pathList["/"];

                    if (cc != null)
                    {
                        cc.TimeStamp(CookieCollection.Stamp.Set);
                        MergeUpdateCollections(ref cookies, cc, port, isSecure, matchOnlyPlainCookie);
                    }
                }

                // Remove unused domain
                // (This is the only place that does domain removal)
                if (pathList.Count == 0)
                {
                    lock (_domainTable)
                    {
                        _domainTable.Remove(domainAttribute[i]);
                    }
                }
            }
        }
예제 #59
0
        internal CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
        {
            CookieCollection cookieCollection = new CookieCollection();
            CookieVariant    variant          = CookieVariant.Unknown;

            if (headerName == null)
            {
                variant = CookieVariant.Rfc2109;
            }
            else
            {
                for (int index = 0; index < CookieContainer.HeaderInfo.Length; ++index)
                {
                    if (string.Compare(headerName, CookieContainer.HeaderInfo[index].Name, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        variant = CookieContainer.HeaderInfo[index].Variant;
                    }
                }
            }
            bool isLocalDomain = this.IsLocalDomain(uri.Host);

            try
            {
                CookieParser cookieParser = new CookieParser(setCookieHeader);
                while (true)
                {
                    Cookie cookie;
                    do
                    {
                        cookie = cookieParser.Get();
                        if (cookie != null)
                        {
                            if (ValidationHelper.IsBlankString(cookie.Name))
                            {
                                if (isThrow)
                                {
                                    throw new CookieException(SR.GetString("net_cookie_format"));
                                }
                            }
                        }
                        else
                        {
                            goto label_19;
                        }
                    }while (!cookie.VerifySetDefaults(variant, uri, isLocalDomain, this.m_fqdnMyDomain, true, isThrow));
                    cookieCollection.InternalAdd(cookie, true);
                }
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }
                else if (isThrow)
                {
                    throw new CookieException(SR.GetString("net_cookie_parse_header", new object[1]
                    {
                        (object)uri.AbsoluteUri
                    }), ex);
                }
            }
label_19:
            foreach (Cookie cookie in cookieCollection)
            {
                this.Add(cookie, isThrow);
            }
            return(cookieCollection);
        }
예제 #60
0
 public void Add(Uri uri, CookieCollection cookies)
 {
     throw new NotImplementedException();
 }