如何使HttpClient的中继流量Fidder酒店或查尔斯现身? [英] How to make HttpClient relay traffic show up in Fidder or Charles?

查看:209
本文介绍了如何使HttpClient的中继流量Fidder酒店或查尔斯现身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于这个例子中一个简单的Web API项目:
http://aspnet.$c$cplex.com/sourcecontrol/latest#Samples/WebApi/RelaySample/Program.cs

然而,继电器正在与本地服务器上面的示例中,在我的项目中继工作与一个真实的地址外部Web服务器; companyX.com

我通过浏览器使用的中继服务(或者,Web代理服务),例如,在浏览器请求relayService.com/companyX。中继服务从外部companyX.com网站数据响应。

继电器的伟大工程,但有些标题是不正确的,我需要看什么的HttpClient发送到远程服务器companyX.com。

在提琴手或从查尔斯我的浏览器,只请求/响应relayService.com上市之后,从HttpClient的到relayService.com请求/响应一直没有出现。

该relayService.com是我的机器上本地运行在IIS7中,我使用的hosts文件直接流量relayService.com。

我曾尝试以下几种变化创建HttpClient的时候:

  VAR ClientHandler的=新HttpClientHandler
        {
            的CookieContainer =饼干,
            UseCookies = TRUE,
            UseDefaultCredentials =假,
            代理=新WebProxy(HTTP://本地主机:8888),
            UseProxy = TRUE,
            AutomaticDecom pression = DECOM pressionMethods.GZip,
            AllowAutoRedirect =假,
            ClientCertificateOptions = ClientCertificateOption.Automatic
        };
        HttpClient的客户端=新的HttpClient(ClientHandler的);

更新

如果我更改 UseProxy = FALSE 的服务继续工作,当小提琴手打开或关闭。

使用 UseProxy = TRUE 那么该服务会失败,如果提琴手是开放的,我得到以下错误:

 对象引用未设置到对象的实例。在System.DomainNameHelper.IdnEquivalent(字符串主机名)在System.Net.HttpWebRequest.GetSafeHostAndPort(URI sourceUri,布尔addDefaultPort,布尔forcePuny code)在System.Net.HttpWebRequest.GenerateProxyRequestLine(的Int32 headersSize)在System.Net.HttpWebRequest .SerializeHeaders()在System.Net.HttpWebRequest.EndSubmitRequest()在System.Net.Connection.CompleteConnection(布尔异步,HttpWebRequest的要求)

使用 UseProxy = TRUE 和提琴手是封闭的,我得到以下(明显)错误:

 无连接可以作出,因为目标机器积极地拒绝它127.0.0.1:8888

在同一个解决方案,我使用HttpWebRequest的从网上下载的数据和,它显示在提琴手,所以它似乎是与HttpClient.GetAsync()

的问题

我已经试过这两台机器上使用相同的结果。

我一直整天疲于应付这一点,任何帮助将是非常美联社preciated。

只是为了回顾一下:
* relayService.com在我的机器上本地运行在IIS7


  • hosts文件127.0.0.1 relayService.com


  • relayService.com是使用HttpClient.GetAsync()从现场网上下载内容的MVC的Web API网站


  • 菲德勒/查尔斯是同一台机器上本地运行


  • 浏览器流量到当地relayService.com出现在菲德勒/查尔斯


  • HttpClient.GetAsync()住的网络流量不会出现在菲德勒/查尔斯


  • 菲德勒/查尔斯都是最新的版本。


再次感谢


解决方案

我通过HttpClient的静态固定的问题。

这工作正常(该程序的功能),但与上述小提琴手,在尝试使用代理抛出一个错误的问题:

 私人HttpClient的_client()
{    VAR ClientHandler的=新HttpClientHandler
    {
        UseCookies = TRUE,
        UseDefaultCredentials =假,
        代理=新WebProxy(HTTP://本地主机:8888),
        UseProxy = TRUE,
        AutomaticDecom pression = DECOM pressionMethods.GZip,
        AllowAutoRedirect = TRUE,
        ClientCertificateOptions = ClientCertificateOption.Automatic
    };    HttpClient的客户端=新的HttpClient(ClientHandler的);
    client.Timeout = TimeSpan.FromMinutes(20);    返回客户端;
}

客户端与创建:

 使用(HTT presponseMessage serviceResponse =等待_client()。GetAsync(的getURL(),HttpCompletionOption.ResponseHeadersRead))
    {
                //返回响应    }

但是,下面的也适用,所有车辆在提琴手显示出来!

 私有静态只读HttpClientHandler _clientHandler =新HttpClientHandler()
{
    // =的CookieContainer饼干,
    UseCookies = TRUE,
    UseDefaultCredentials =假,
    代理=新WebProxy(HTTP://本地主机:8888),
    UseProxy =假,
    AutomaticDecom pression = DECOM pressionMethods.GZip,
    AllowAutoRedirect =假,
    ClientCertificateOptions = ClientCertificateOption.Automatic,
};//创建HttpClient的一个共享实例,并设置请求超时
私人静态只读HttpClient的_client =新的HttpClient(_clientHandler)
{
    超时= TimeSpan.FromMinutes(20)
};

客户端用(唯一的区别在于以上_client后除去())中创建

 使用(HTT presponseMessage serviceResponse =等待_client.GetAsync(的getURL(),HttpCompletionOption.ResponseHeadersRead))
    {
                //返回响应    }

我不知道为什么这个工程。任何有识之士将AP preciated。

谢谢,

I have a simple web api project based on this example: http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/RelaySample/Program.cs

However, in the above sample the relay is working with a local server, in my project the relay is working with an external web server with a real address; companyX.com

I am using the relay service (or, web proxy service) through a browser, for example, in the browser request relayService.com/companyX. The relay service responds with data from the external companyX.com site.

The relay works great, however some headers are not correct and I need to see what the HttpClient is sending to the remote companyX.com server.

In fiddler or Charles, only the request/response from my browser to relayService.com is listed, the request/response from the HttpClient to relayService.com never shows up.

The relayService.com is running locally on my machine, in IIS7, I'm using the hosts file to direct traffic to relayService.com.

I have tried several variation of the following when creating the HttpClient:

        var clientHandler = new HttpClientHandler
        {
            CookieContainer = cookies,
            UseCookies = true,
            UseDefaultCredentials = false,
            Proxy = new WebProxy("http://localhost:8888"),
            UseProxy = true,
            AutomaticDecompression = DecompressionMethods.GZip, 
            AllowAutoRedirect = false, 
            ClientCertificateOptions = ClientCertificateOption.Automatic
        };


        HttpClient client = new HttpClient(clientHandler);

UPDATE

If I change UseProxy = false The service continues to work, when Fiddler is open or closed.

With UseProxy = true then the service will fail, if fiddler is open, I get the following error:

Object reference not set to an instance of an object.

at System.DomainNameHelper.IdnEquivalent(String hostname) at System.Net.HttpWebRequest.GetSafeHostAndPort(Uri sourceUri, Boolean addDefaultPort, Boolean forcePunycode) at System.Net.HttpWebRequest.GenerateProxyRequestLine(Int32 headersSize) at System.Net.HttpWebRequest.SerializeHeaders() at System.Net.HttpWebRequest.EndSubmitRequest() at System.Net.Connection.CompleteConnection(Boolean async, HttpWebRequest request)

With UseProxy = true and fiddler is CLOSED, I get the following (obvious) error:

No connection could be made because the target machine actively refused it 127.0.0.1:8888

In the same solution I am using HttpWebRequest to download data from the web and that does show up in Fiddler, so it seems to be an issue with the HttpClient.GetAsync()

I have tried this on two machines with identical results.

I have been struggling with this all day, any help would be much appreciated.

Just to recap: * relayService.com is running locally on my machine, in IIS7

  • hosts file has "127.0.0.1 relayService.com"

  • relayService.com is an MVC Web API site that uses HttpClient.GetAsync() to download content from the live web

  • Fiddler/Charles is running locally on same machine

  • browser traffic to the local relayService.com appears in Fiddler/Charles

  • HttpClient.GetAsync() to live web traffic does not appear in Fiddler/Charles

  • Fiddler/Charles are both up to date versions.

Thanks again

解决方案

I fixed the problem by making the HttpClient static.

This works fine (for the program functionality) but has the problem with fiddler described above, where trying to use the proxy throws an error:

private HttpClient _client()
{

    var clientHandler = new HttpClientHandler
    {
        UseCookies = true,
        UseDefaultCredentials = false,
        Proxy = new WebProxy("http://localhost:8888"),
        UseProxy = true,
        AutomaticDecompression = DecompressionMethods.GZip,
        AllowAutoRedirect = true,
        ClientCertificateOptions = ClientCertificateOption.Automatic
    };

    HttpClient client = new HttpClient(clientHandler);
    client.Timeout = TimeSpan.FromMinutes(20);

    return client;
}

The client was created with:

using (HttpResponseMessage serviceResponse = await _client().GetAsync(getURL(), HttpCompletionOption.ResponseHeadersRead))
    {
                // Return response

    }

However, the below also works and all traffic shows up in Fiddler!

private static readonly HttpClientHandler _clientHandler = new HttpClientHandler()
{
    //CookieContainer = cookies,
    UseCookies = true,
    UseDefaultCredentials = false,
    Proxy = new WebProxy("http://localhost:8888"),
    UseProxy = false,
    AutomaticDecompression = DecompressionMethods.GZip,
    AllowAutoRedirect = false,
    ClientCertificateOptions = ClientCertificateOption.Automatic, 
};

//Create a shared instance of HttpClient and set the request timeout
private static readonly HttpClient _client = new HttpClient(_clientHandler)
{
    Timeout = TimeSpan.FromMinutes(20)
};

The client was created with (only difference is removing the '()' after _client above):

using (HttpResponseMessage serviceResponse = await _client.GetAsync(getURL(), HttpCompletionOption.ResponseHeadersRead))
    {
                // Return response

    }

I have no clue why this works. Any insight would be appreciated.

Thanks,

这篇关于如何使HttpClient的中继流量Fidder酒店或查尔斯现身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆