FiddlerCore 包[Asm] 纯文本查看 复制代码 NuGet\Install-Package FiddlerCore -Version 4.6.2
其他看图安装包
上代码
[C#] 纯文本查看 复制代码 public class douyin
{
public static void start()
{
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.Startup(8888, true, true, true);
remove();
install();
// 创建代{过}{滤}理对象
Proxy proxy = new Proxy();
// 设置代{过}{滤}理服务器地址和端口
proxy.HttpProxy = "127.0.0.1:8888";
proxy.SslProxy = "127.0.0.1:8888";
ChromeOptions options = new ChromeOptions();
// 将代{过}{滤}理对象附加到 ChromeOptions
options.Proxy = proxy;
// 创建Edge浏览器的WebDriver实例
IWebDriver driver = new ChromeDriver(options);
// 导航到52pojie网站
driver.Navigate().GoToUrl("用户的主页");
// 等待用户输入并保持程序运行
Console.WriteLine("按下任意键停止...");
Console.ReadLine();
// 关闭浏览器
driver.Quit();
// 停止 FiddlerCore
FiddlerApplication.Shutdown();
}
private static void FiddlerApplication_BeforeRequest(Session oSession)
{
if (oSession.url.StartsWith("www.douyin.com/aweme/v1/web/aweme"))
{
// 移除"Accept-Encoding"请求头
if (oSession.oRequest.headers.Exists("Accept-Encoding"))
{
oSession.oRequest.headers.Remove("Accept-Encoding");
}
}
}
static string tmp = "-------------------------------------";
private static void FiddlerApplication_AfterSessionComplete(Session oSession)
{
if (oSession.RequestMethod == "POST" || oSession.RequestMethod == "GET")
{
if (oSession.url.Contains("www.douyin.com/aweme/v1/web/aweme"))
{
byte[] responseBodyBytes = oSession.responseBodyBytes;
// 转换解压后的字节数组为字符串
string responseBodyString = Encoding.UTF8.GetString(responseBodyBytes);
var json = JObject.Parse(responseBodyString);
var videoList = json["aweme_list"];
foreach (var video in videoList)
{
var preview_title = video["preview_title"];
var play_addr = video["video"]["play_addr"]["url_list"][0];
Console.WriteLine(preview_title);
Console.WriteLine(play_addr);
}
}
}
}
public static void install()
{
if (!CertMaker.rootCertExists())
{
CertMaker.createRootCert();
CertMaker.trustRootCert();
}
}
public static void remove()
{
if (CertMaker.rootCertExists())
{
CertMaker.removeFiddlerGeneratedCerts();
}
}
}
代码解释
[C#] 纯文本查看 复制代码 private static void FiddlerApplication_BeforeRequest(Session oSession)
{
if (oSession.url.StartsWith("www.douyin.com/aweme/v1/web/aweme"))
{
// 移除"Accept-Encoding"请求头
if (oSession.oRequest.headers.Exists("Accept-Encoding"))
{
oSession.oRequest.headers.Remove("Accept-Encoding");
}
}
}
这段代码是因为douyin在使用http传输时使用了br压缩方式,所以这里移除请求头,干掉br压缩
[C#] 纯文本查看 复制代码 private static void FiddlerApplication_AfterSessionComplete(Session oSession)
{
if (oSession.RequestMethod == "POST" || oSession.RequestMethod == "GET")
{
if (oSession.url.Contains("www.douyin.com/aweme/v1/web/aweme"))
{
byte[] responseBodyBytes = oSession.responseBodyBytes;
// 转换解压后的字节数组为字符串
string responseBodyString = Encoding.UTF8.GetString(responseBodyBytes);
var json = JObject.Parse(responseBodyString);
var videoList = json["aweme_list"];
foreach (var video in videoList)
{
var preview_title = video["preview_title"];
var play_addr = video["video"]["play_addr"]["url_list"][0];
Console.WriteLine(preview_title);
Console.WriteLine(play_addr);
}
}
}
}
拦截对应的请求
前边干掉了br压缩,这里我们直接对byte[]转string进行json解析
最终效果图
因为是通过接口拿到的数据,这里随便怎么输出,我这里只把作品标题还有视频链接输出 |