版權聲明:本文爲博主原創文章,未經博主容許不得轉載。可點擊關注博主 ,不明白的進羣191065815 個人羣裏問 https://blog.csdn.net/u010919083/article/details/80913948
廢話很少說直接上代碼:html
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; } }