示例接口定义
为了方便笔者先自己写了个简单的接口用于展示,返回当前时间,具体如下
/// <summary>
/// GetToday.
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetTime/Today")]
public MyDateTime GetToday()
{
var now = DateTime.Now;
return new MyDateTime
{
Year = now.Year,
Month = now.Month,
Day = now.Day,
Hour = now.Hour,
Minute = now.Minute,
Second = now.Second,
};
}
上面的MyDateTime
是展示用的自定义类,其中的属性已在return
中表示。
使用HttpClient发起请求
var httpClient = new HttpClient();
var url = "http://localhost:5000/GetTime/Today";
var response = await httpClient.GetAsync(new Uri(url));
不出意外Fiddler
是抓不到的。
配置Fiddler
打开Tools->Options
然后切换到Connections
选项卡,我们可以看到Fiddler
默认监听的是8888
端口,这就好办了我们需要开启HttpClient
中的代理。
设置HttpClient
修改请求为:
var httpClient = new HttpClient(new HttpClientHandler
{
UseProxy = true, // 使用代理,
Proxy = new WebProxy(new Uri("http://localhost:8888")) // port:8888就是Fiddler默认监听的端口
});
var url = "http://localhost:5000/GetTime/Today";
var response = await httpClient.GetAsync(new Uri(url));
再次发起请求
Fiddler
已经抓到了