CefSharp header转 HttpWebRequest header

主要片段

HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
                    wReq.CookieContainer = new CookieContainer();
                    wReq.CookieContainer.Add(GetCookieCollection.GetCookie(ios.read("cookie")));
                    string[] myhead= request.Headers.AllKeys;
                    foreach (var mykey in myhead)
                    {
                        switch (mykey)
                        {
                            case "Accept":
                                wReq.Accept = request.Headers[mykey];
                                break;
                            case "Host":
                                wReq.Host = request.Headers[mykey];
                                break;
                            case "User-Agent":
                                wReq.UserAgent = request.Headers[mykey];
                                break;
                            
                            default:
                                wReq.Headers[mykey] = request.Headers[mykey];
                                break;
                        }
                    }

示例

using CefSharp;
using kickwho;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;

namespace instagram
{
internal class Requesthandler : IRequestHandler
{
public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
//callback.Continue(ios.read(“daili_name”), ios.read(“password”));
return true;
}

public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//throw new System.NotImplementedException();

return null;
}

public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
{
//throw new System.NotImplementedException();
return false;
}

public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
//throw new System.NotImplementedException();
//www.instagram.com/graphql/query/
string url = request.Url;

string[] myhead1 = request.Headers.AllKeys;

foreach (var mykey in myhead1)
{
if (mykey == “X-CSRFToken”)
{
ios.write(“X-CSRFToken”, request.Headers[mykey]);
}
else if (mykey == “X-IG-App-ID”)
{
ios.write(“X-IG-App-ID”, request.Headers[mykey]);
}
else if (mykey == “X-Requested-With”)
{
ios.write(“X-Requested-With”, request.Headers[mykey]);
}
else if (mykey == “X-Instagram-AJAX”)
{
ios.write(“X-Instagram-AJAX”, request.Headers[mykey]);
}
else if (mykey == “X-Instagram-GIS”)
{
ios.write(“X-Instagram-GIS”, request.Headers[mykey]);
}
}

if (url.IndexOf(“www.instagram.com/graphql/query/”) > 0)
{
if (url.IndexOf(“fetch_mutual%22%3Afalse”) > 0)
{
ios.write(“query_hash”, url.Substring(52, 32));
}

}

if (url.IndexOf(“www.instagram.com/graphql/query/”) > 0 && ios.read(“MOUSEEVENTF_WHEEL”) == “1”)
{
ios.write(“url”, url);
try
{

HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
wReq.CookieContainer = new CookieContainer();
wReq.CookieContainer.Add(GetCookieCollection.GetCookie(ios.read(“cookie”)));
string[] myhead= request.Headers.AllKeys;

foreach (var mykey in myhead)
{
switch (mykey)
{
case “Accept”:
wReq.Accept = request.Headers[mykey];
break;
case “Host”:
wReq.Host = request.Headers[mykey];
break;
case “User-Agent”:
wReq.UserAgent = request.Headers[mykey];
break;

default:
wReq.Headers[mykey] = request.Headers[mykey];
break;
}

}

// Get the response instance.

System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)

using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
{
//ios.write(“data.txt”, reader.ReadToEnd());

//序列化参数

string api_url = “https://blog.mengyiju.com/api.php/Insjson”;
//通过抓包工具,可以看到是这个格式
var postData = “json=”+ reader.ReadToEnd();

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(api_url);
req.Method = “POST”;
req.Timeout = 30000;
req.AllowAutoRedirect = false;
req.ContentType = “application/x-www-form-urlencoded”;
req.KeepAlive = true;

byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData);
req.ContentLength = postBytes.Length;
System.IO.Stream postDataStream = req.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();

//接收数据
var response1 = (HttpWebResponse)req.GetResponse();
System.IO.Stream respStream1 = response1.GetResponseStream();
System.IO.StreamReader reader1 = new System.IO.StreamReader(respStream1);
String re = reader1.ReadToEnd().Trim();
if(re!=””)
{
ios.write(“data.txt”,ios.read(“data.txt”).Trim()+”\r\n”+ re);
}

}
}
catch (System.Exception ex)
{
ios.write(“error”, ex.Message);
//errorMsg = ex.Message;
}

}

return CefReturnValue.Continue;
}

public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
throw new System.NotImplementedException();
}

public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
throw new System.NotImplementedException();
}

public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
{
throw new System.NotImplementedException();
}

public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
throw new System.NotImplementedException();

}

public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
throw new System.NotImplementedException();
}

public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
{
throw new System.NotImplementedException();
}

public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
{
//throw new System.NotImplementedException();
}

public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
//throw new System.NotImplementedException();
}

public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
{
//throw new System.NotImplementedException();
}

public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//throw new System.NotImplementedException();
return false;
}
}
}

发表评论