当前位置: 首页>>代码示例>>C#>>正文


C# CookieCollection.Parse方法代码示例

本文整理汇总了C#中System.Net.CookieCollection.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# CookieCollection.Parse方法的具体用法?C# CookieCollection.Parse怎么用?C# CookieCollection.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.CookieCollection的用法示例。


在下文中一共展示了CookieCollection.Parse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendRequest

        /// <include file='IWebClient.xml' path='/IWebClient/SendRequest_WebRequest/*'/>
        public WebResponse SendRequest( WebRequest webRequest )
        {
            var httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create( webRequest.Destination );

            httpWebRequest.Method = webRequest.Type.ToString().ToUpperInvariant();

            // We shall handle redirects by hand so that we may capture cookies and properly
            // handle login forms.
            //
            // Automating Web Login With HttpWebRequest
            // https://www.stevefenton.co.uk/Content/Blog/Date/201210/Blog/Automating-Web-Login-With-HttpWebRequest/
            #if !NETSTANDARD1_5
            httpWebRequest.AllowAutoRedirect = false;
            #endif

            // Default headers.
            httpWebRequest.Accept = "*/*";
            httpWebRequest.Headers[HttpRequestHeader.UserAgent] = UserAgent;

            // Set and/or override any provided headers.
            foreach ( var headerName in webRequest.Headers.AllKeys ) {
                ConfigureHeader( httpWebRequest, headerName, webRequest.Headers[headerName] );
            }

            httpWebRequest.CookieContainer = new CookieContainer();
            httpWebRequest.CookieContainer.Add( webRequest.Destination, cookieJar.GetCookies( webRequest.Destination ) );

            if ( webRequest.Type == WebRequestType.Post ) {
                var postRequest = (PostWebRequest)webRequest;

                var requestDataBytes = Encoding.UTF8.GetBytes( postRequest.RequestData );
                Stream requestStream = null;

                httpWebRequest.Headers[HttpRequestHeader.ContentLength] = requestDataBytes.Length.ToString();
                httpWebRequest.ContentType = postRequest.ContentType;
            #if !NETSTANDARD1_5
                httpWebRequest.ServicePoint.Expect100Continue = false;
            #endif

                try {
                    requestStream = httpWebRequest.GetRequestStreamAsync().Result;
                    requestStream.Write( requestDataBytes, 0, requestDataBytes.Length );
                }
                finally {
                    if ( requestStream != null ) {
                        requestStream.Dispose();
                    }
                }
            }

            OnSendingRequest( new SendingRequestEventArgs( webRequest ) );

            WebResponse response;
            HttpWebResponse webResponse = null;

            try {
                webResponse = ( HttpWebResponse )httpWebRequest.GetResponseAsync().Result;

                OnProcessingResponse( new ProcessingResponseEventArgs( webResponse ) );

                if ( httpWebRequest.HaveResponse ) {
                    // Process cookies that the .NET client 'forgot' to include,
                    // see http://stackoverflow.com/questions/15103513/httpwebresponse-cookies-empty-despite-set-cookie-header-no-redirect
                    // for more details;
                    // an example cookie which is not parsed is this one:
                    //
                    // Set-Cookie:ADCDownloadAuth=[long token];Version=1;Comment=;Domain=apple.com;Path=/;Max-Age=108000;HttpOnly;Expires=Tue, 03 May 2016 13:30:57 GMT

                    // Handle cookies that are offered
                    CookieCollection cookies = new CookieCollection();
                    cookies.Add(webResponse.Cookies);

                    if (webResponse.Headers.AllKeys.Contains("Set-Cookie"))
                    {
                        cookies.Parse(webResponse.Headers["Set-Cookie"], httpWebRequest.Headers[HttpRequestHeader.Host]);
                    }

                    foreach ( Cookie responseCookie in cookies ) {
                        var cookieFound = false;

                        foreach ( Cookie existingCookie in cookieJar.GetCookies( webRequest.Destination ) ) {
                            if ( responseCookie.Name.Equals( existingCookie.Name ) ) {
                                existingCookie.Value = responseCookie.Value;
                                cookieFound = true;
                            }
                        }

                        if ( !cookieFound ) {
                            var args = new AddingCookieEventArgs( responseCookie );

                            OnAddingCookie( args );

                            if ( !args.Cancel ) {
                                cookieJar.Add( new Uri(responseCookie.Domain), responseCookie );
                            }
                        }
                    }

                    if ( redirectionStatusCodes.Contains( webResponse.StatusCode ) ) {
//.........这里部分代码省略.........
开发者ID:quamotion,项目名称:NScrape,代码行数:101,代码来源:WebClient.cs


注:本文中的System.Net.CookieCollection.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。