Web请求:如何确定请求'内容类型?

时间:2016-02-22 08:27:16

标签: javascript c# html chromium-embedded cefsharp

在回答我的问题之前,请先了解平台和可用资源。

我在Windows 10上使用:.NET c#4.5框架。 我使用基于CefSharp铬的框架。由于有些人不熟悉cef,我特意分享了处理程序类,我知道我可以使用请求对象。我专注于以下方法:

   CefReturnValue IRequestHandler.OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)

我想在下面实现这个列表:它是一个干净的内容类型列表:我很薄我应该像下面这样转换mime类型的元素类型(来自相应处理程序事件的请求类型)

 case 1:    return "script";
    case 2:    return "image";
    case 4:    return "background";
    case 8:    return "stylesheet";
    case 16:   return "object";
    case 32:   return "sub_frame";
    case 64:   return "object_subrequest";
    case 128:  return "media";
    case 256:  return "other";
    case 512:  return "xmlhttprequest";
    case 1024: return "main_frame";
    case 2048: return "elemhide";
    case 4096: return "popup";
    default:   return "selector";

为了更清楚,我想举个例子: 想象一下,我运行了chrome web浏览器,我进入了www.hurriyet.com.tr

如果我在处理程序类上调试它;我看到主域名:www.hurriyet.com.tr以及来自其他域名的多个请求,例如syndication.google.com/sub/main.html

如您所见,这是一个请求,网址为" syndication.google.com/sub/main.html"所以它是一个子框架

加载页面之前的另一个请求;第二个请求是" adspub.mediacatatogry.com/img/banner.gif"如你所见,这是一个图像请求。

请帮我为每个请求获取正确的内容类型。我想根据上面引用的列表对这些请求进行分类。

谢谢。

摘自处理程序类:

// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using CefSharp.Example.Filters;

namespace CefSharp.Example
{
    public class RequestHandler : IRequestHandler
    {    
        CefReturnValue IRequestHandler.OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            //Example of how to set Referer
            // Same should work when setting any header

            // For this example only set Referer when using our custom scheme
            var url = new Uri(request.Url);
            if (url.Scheme == CefSharpSchemeHandlerFactory.SchemeName)
            {
                var headers = request.Headers;

                headers["Referer"] = "http://google.com";

                request.Headers = headers;
            }

            //NOTE: If you do not wish to implement this method returning false is the default behaviour
            // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource.
            //callback.Dispose();
            //return false;

            //NOTE: When executing the callback in an async fashion need to check to see if it's disposed
            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    if (request.Method == "POST")
                    {
                        using (var postData = request.PostData)
                        {
                            if(postData != null)
                            { 
                                var elements = postData.Elements;

                                var charSet = request.GetCharSet();

                                foreach (var element in elements)
                                {
                                    if (element.Type == PostDataElementType.Bytes)
                                    {
                                        var body = element.GetBody(charSet);
                                    }
                                }
                            }
                        }
                    }

                    //Note to Redirect simply set the request Url
                    //if (request.Url.StartsWith("https://www.google.com", StringComparison.OrdinalIgnoreCase))
                    //{
                    //    request.Url = "https://github.com/";
                    //}

                    //Callback in async fashion
                    //callback.Continue(true);
                    //return CefReturnValue.ContinueAsync;
                }
            }

            return CefReturnValue.Continue;
        }
    }
}

您怎么看?他们有一个开关盒块?那么如何将这些mime类型分组到上面的这个特定列表中..有什么想法吗?

0 个答案:

没有答案