如何设置FiddlerCore代码以从WPF应用程序捕获REST调用

时间:2014-03-12 21:05:15

标签: wpf rest fiddlercore

我有一个WPF应用程序。我正在进行REST调用。

我想改变其余服务的响应XML / JSON。

我正在使用FiddlerCore来拦截电话。

我需要收听本地计算机中的所有端口。

    List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();

                FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
                oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.DecryptSSL);

                //Fiddler.FiddlerApplication.Startup(8080, true, true);
                FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
                {

                };

                FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS)
                {

                }
                };
Fiddler.FiddlerApplication.Startup(0, true, false);

1 个答案:

答案 0 :(得分:1)

此问题已解决 - 请查看以下链接

https://gist.githubusercontent.com/timiles/4079321/raw/268f71249f381649a06f4b48ebfb54cbaa8ee282/MockWebProxyHelper.cs

using System;
using System.Net;
// http://www.fiddler2.com/fiddler/Core/
using Fiddler;

public static class MockWebProxyHelper
{
    public enum HttpMethods
    {
        GET, POST, PUT, Unknown
    }

    public class Response
    {
        public Response(string header = "HTTP/1.1 200 OK", string body = "", string contentType = "application/json")
        {
            Header = header;
            Body = body;
            ContentType = contentType;
        }

        public string Header { get; private set; }
        public string Body { get; private set; }
        public string ContentType { get; private set; }
    }

    public static Func<HttpMethods, string, Response> GetMockResponse = delegate { return new Response(); };
    public static Func<HttpMethods, string, bool> InterceptRequest = delegate { return true; };

    public static void SetUp(bool registerAsSystemProxy = false)
    {
        const int port = 18833;
        FiddlerApplication.Startup(port, FiddlerCoreStartupFlags.DecryptSSL
                                            | (registerAsSystemProxy ? FiddlerCoreStartupFlags.RegisterAsSystemProxy : FiddlerCoreStartupFlags.None));
        WebRequest.DefaultWebProxy = new WebProxy("localhost", port);

        FiddlerApplication.BeforeRequest += BeforeRequest;
    }

    private static void BeforeRequest(Session session)
    {
        var httpMethod = GetHttpMethod(session);
        var url = session.url;
        if (InterceptRequest(httpMethod, url))
        {
            session.utilCreateResponseAndBypassServer();

            var response = GetMockResponse(httpMethod, url);
            session.oResponse.headers = Parser.ParseResponse(response.Header);
            session.oResponse.headers.Add("Content-Type", response.ContentType);

            session.utilSetResponseBody(response.Body);
        }
    }

    private static HttpMethods GetHttpMethod(Session session)
    {
        return session.HTTPMethodIs("GET") ? HttpMethods.GET
                    : session.HTTPMethodIs("POST") ? HttpMethods.POST
                            : session.HTTPMethodIs("PUT") ? HttpMethods.PUT : HttpMethods.Unknown;
    }

    public static void TearDown()
    {
        FiddlerApplication.BeforeRequest -= BeforeRequest;
        FiddlerApplication.oProxy.Detach();
        FiddlerApplication.Shutdown();
    }
}