C# 使用Fiddler捕获本地HttpClient发出的请求操作
作者:丨背水丨 时间:2022-06-28 04:10:34
示例接口定义
为了方便笔者先自己写了个简单的接口用于展示,返回当前时间,具体如下
/// <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");
来源:https://blog.csdn.net/ZUFE_ZXh/article/details/108574353
标签:C#,Fiddler,HttpClient,请求
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析Java中Split函数的用法技巧
2023-02-19 09:13:27
Java实现单向链表反转
2023-11-18 01:03:11
![](https://img.aspxhome.com/file/2023/4/78854_0s.png)
VsCode使用EmmyLua插件调试Unity工程Lua代码的详细步骤
2022-12-25 14:13:25
![](https://img.aspxhome.com/file/2023/0/81130_0s.png)
java中map与实体类的相互转换操作
2023-03-14 01:42:35
![](https://img.aspxhome.com/file/2023/9/71219_0s.png)
JDK1.8安装与配置超详细教程
2023-09-20 16:17:50
![](https://img.aspxhome.com/file/2023/9/62809_0s.jpg)
Unity键盘WASD实现物体移动
2021-06-14 13:46:21
![](https://img.aspxhome.com/file/2023/3/71073_0s.jpg)
入门JDK集合之HashMap解析
2023-11-24 02:45:03
![](https://img.aspxhome.com/file/2023/1/59901_0s.png)
JAXB简介_动力节点Java学院整理
2021-09-15 00:09:32
MyEclipse去除网上复制下来的代码带有的行号(正则去除行号)
2023-09-15 21:59:23
![](https://img.aspxhome.com/file/2023/5/63045_0s.jpg)
Java 线程对比(Thread,Runnable,Callable)实例详解
2022-10-20 21:34:08
![](https://img.aspxhome.com/file/2023/5/75765_0s.png)
全面解析Java支持的数据类型及Java的常量和变量类型
2022-03-25 16:54:10
SpringBoot Security前后端分离登录验证的实现
2023-03-09 10:30:07
![](https://img.aspxhome.com/file/2023/8/71588_0s.png)
Java SpringBoot在RequestBody中高效的使用枚举参数原理案例详解
2022-03-28 01:47:18
![](https://img.aspxhome.com/file/2023/4/71684_0s.jpg)
zookeeper概述图文详解
2021-12-07 15:46:03
![](https://img.aspxhome.com/file/2023/6/71946_0s.png)
Java中Future和FutureTask的示例详解及使用
2023-01-29 11:48:42
![](https://img.aspxhome.com/file/2023/6/69296_0s.png)
学习Java设计模式之观察者模式
2023-07-03 05:17:46
![](https://img.aspxhome.com/file/2023/1/66541_0s.jpg)
Spring Cloud Gateway整合sentinel 实现流控熔断的问题
2022-01-18 23:10:05
![](https://img.aspxhome.com/file/2023/7/60407_0s.png)
SpringBoot 如何实时刷新静态文件
2023-11-02 13:27:09
![](https://img.aspxhome.com/file/2023/5/58765_0s.png)
Java动态 代理的应用详解
2023-11-25 08:15:24
轻松理解Java面试和开发中的IoC(控制反转)
2023-08-10 03:00:35
![](https://img.aspxhome.com/file/2023/4/58184_0s.jpg)