示例接口定义 为了方便笔者先自己写了个简单的接口用于展示,返回当前时间,具体如下 /// summary/// GetToday./// /summary/// returns/returns[HttpGet][Route("GetTime/Today")]public MyDateTime GetToday(){ var
示例接口定义
为了方便笔者先自己写了个简单的接口用于展示,返回当前时间,具体如下
/// <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已经抓到了
补充知识:解决Fiddler无法捕获本地HttpWebRequest(C#.net)请求和HttpURLConnection(Java)请求
方法很简单,就是设置本地代理
C#
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = new WebProxy("127.0.0.1:8888", true);
Java
jre -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888 MyApp
或者
System.setProperty("http.proxyHost", "localhost"); System.setProperty("http.proxyPort", "8888"); System.setProperty("https.proxyHost", "localhost"); System.setProperty("https.proxyPort", "8888");
以上这篇C# 使用Fiddler捕获本地HttpClient发出的请求操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。