首页 > 其他分享 >使用CEfSharp之旅(7)CEFSharp 拦截 http 请求 websocket 内容

使用CEfSharp之旅(7)CEFSharp 拦截 http 请求 websocket 内容

时间:2022-10-28 21:32:37浏览次数:53  
标签:CEfSharp http string request CEFSharp IWebBrowser IBrowser var public


废话不多说直接上代码: 不清楚的加群:群 :191065815

private void Form1_Load(object sender, EventArgs e)
{
try
{

//更改设置浏览器语言
CefSettings settings = new CefSettings();
settings.Locale = "zh-CN";
settings.AcceptLanguageList = "zh-CN";
Cef.Initialize(settings);

var re = new request();

//接收和发送的http消息
re.msg += Re_msg;
re.msg2 += Re_msg2; ... 这里是使用

     private void Re_msg(string obj)
        {   //可打印Obj 内容即可看到 }

 

public class request : IRequestHandler
{
public event Action<string> msg;
public event Action<string,object> msg2;
public event Action<string> msgGetuser;
public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy,
string host, int port, string realm, string scheme, IAuthCallback callback)
{

return false;
}

public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{

if (response.MimeType.ToLower().IndexOf("text") > -1 || response.MimeType.ToLower().IndexOf("json") > -1)
{

var filter = new AppendResponseFilter(request.Url, response.MimeType);

filter.VOIDFUN += Filter_VOIDFUN;
return filter;
//var filter2 = FilterManager.CreateFilter(request.Identifier.ToString());

//return filter2;

}

return null;

//var filter1 = FilterManager.CreateFilter(request.Identifier.ToString());

//return filter1;

}
private void Filter_VOIDFUN(string arg1, string arg2, string arg3, long arg4)
{
msg2?.Invoke(arg1,arg2);
}


public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request,
bool isRedirect)
{

var m = request.Method;
msg?.Invoke(request.Url);
msg?.Invoke(m);
if (request.Method == "POST")
{
using (var postData = request.PostData)
{
if (postData != null)
{
var elements = postData.Elements;

var charSet = request.GetCharSet();

foreach (var element in elements)
{
if (element.Type == PostDataElementType.Bytes)
{
var body = element.GetBody(charSet);
msg?.Invoke(body);
}
}
}
}
}

return false;
}

public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var m = request.Method;
msg?.Invoke(request.Url);
msg?.Invoke(m);
if (request.Method == "POST")
{
using (var postData = request.PostData)
{
if (postData != null)
{
var elements = postData.Elements;

var charSet = request.GetCharSet();

foreach (var element in elements)
{
if (element.Type == PostDataElementType.Bytes)
{
var body = element.GetBody(charSet);
msg?.Invoke(body);
}
}
}
}
}

return CefReturnValue.Continue;
}

public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
return true;
}

public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}

public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
{

}

public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
return false;
}

public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
return false;
}

public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
{

}

public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
{

}

public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
//if (request.Url.Contains("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?") || request.Url.Contains("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?"))
//{
// //if (!response.ResponseHeaders["Content-Type"].Contains("application/json"))
// //{
// // return;
// //}
// var filter1 = FilterManager.GetFileter(request.Identifier.ToString()) as AppendResponseFilter;

// if (filter1 != null)
// Console.WriteLine(filter1.DataAll);
// byte[] bys = filter1.DataAll.ToArray();
// string s1 = System.Text.Encoding.UTF8.GetString(bys);

// msgGetuser.Invoke(s1);
//}

}

public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
{

}

public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{

}

public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false; ;

}

public bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
{
return true;
}
}

 

 

 

标签:CEfSharp,http,string,request,CEFSharp,IWebBrowser,IBrowser,var,public
From: https://blog.51cto.com/51souta/5805266

相关文章

  • ThreadHttpRequest
    importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONException;importcom.alibaba.fastjson.JSONObject;importjava.io.IOException;importj......
  • HttpUtils
    importorg.apache.commons.lang3.StringUtils;importorg.apache.http.Consts;importorg.apache.http.HttpEntity;importorg.apache.http.client.entity.UrlEncoded......
  • linux服务器,搭建socks5代理和http代理privoxy
    ssh-qTNf-D0.0.0.0:7070root@ip地址然后会提示输入密码即可,然后查看进程psaux|grepssh 看到这个,表示已经成功开启了7070端口的一个socks5的监听了,浏览器......
  • Wireshark解密HTTPS流量的两种方法
    原理我们先回顾一下SSL/TLS的整个握手过程:Clienthello:发送客户端的功能和首选项给服务器,在连接建立后,当希望重协商、或者响应服务器的重协商请求时会发送。versio......
  • 解决轻量级服务器 HttpProcessor TcpClient 跨域问题
    添加:processor.httpHeaders.Add("Access-Control-Allow-Origin:*");//(正确)processor.httpHeaders.Add("Access-Control-Allow-Method......
  • 使用CEfSharp之旅(1) 加载网络页面
    新建工程winform,添加Nuget程序包Cefsharp.winform 本工程使用57.0版本 Cefsharp。请注意使用的版本  staticChromiumWebBrowserweb;privatevoidBrowser......
  • 使用CEfSharp之旅(4)cefsharp 调用F12
    privatevoidbutton1_Click_1(objectsender,EventArgse){web.ShowDevTools();}......
  • selenium模拟登录使用https协议的网址时报错的解决方法
    公司的测试地址是https协议的,使用使用默认的selenium登录时会出现安全提示的报错,导致无法模拟自动化。对此,笔者通过网上查找资料和实践,发现只需要简单的修改一下浏览器驱......
  • http大文件上传如何做断点续传
    ​ 前言文件上传是一个老生常谈的话题了,在文件相对比较小的情况下,可以直接把文件转化为字节流上传到服务器,但在文件比较大的情况下,用普通的方式进行上传,这可不是一个好......
  • Access to XMLHttpRequest at xxxx from origin xxx has been blocked by CORS poli
    错误:控制台报错; network自动发起了请求: 解决办法:找到\node_modules\sockjs-client\dist\sockjs.js 文件 然后crtl+g快捷键跳转到1603行,把他注释掉......