哈喽^_^
一般我们在编写网页爬虫的时候经常会使用到Fiddler这个工具来分析http包,而且通常并不是分析一个包就够了的,所以为了把更多的时间放在分析http包上,自动化生成封包代码就尤为重要了(其实之前已经有第三方的插件了,自己编写主要是为了适配自定义的http请求模块)!
首先注册拖拽事件 允许直接在Fiddler Session列表中拖入Session到插件界面
private void TrCode_Load(object sender, System.EventArgs e)
{
AllowDrop = true;
DragDrop += TrCode_DragDrop;
DragOver += TrCode_DragOver;
}
private void TrCode_DragOver(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent("Fiddler.Session[]") ? DragDropEffects.Copy : DragDropEffects.None;
} private void TrCode_DragDrop(object sender, DragEventArgs e)
{
//获取拖入的Session数据 数据格式Fiddler.Session[]
var data = (Session[])e.Data.GetData("Fiddler.Session[]");
if (_sessions == data)
{
return;
}
_sessions = data;
if ((data == null)) return;
if (data.Length > )
{
AnalyticSession(data);
}
else
{
//多个也只获取第一个进行解析
AnalyticSession(data[]);
}
}
获取到Session了 开始解析 并且生成代码 (生成的代码可以根据自己的情况去自定义 )
/// <summary>
/// 生成TrCode
/// </summary>
/// <param name="session"></param>
/// <param name="addMethodName"></param>
private void GenerateTrCode(Session session, bool addMethodName = false)
{ var stringBuilder = new StringBuilder();
string uri;
if (cbxGetPlaceholder.Checked && session.fullUrl.Split('?').Length > )
{
var str = session.fullUrl;
var getUrl = str.Split('?');
if (getUrl.Length == )
{
str = getUrl[];
str = GetUrlParameters(str);
}
uri = getUrl[] + "?" + str;
}
else
{
uri = session.fullUrl;
}
if (!addMethodName)
{
var strParameter = string.Empty;
var list = new List<ParameterInfo>();
if (cbxPostPlaceholder.Checked)
{
list.AddRange(_listParameterInfo.Where(i => i.SubmitMethod == "Post"));
}
if (cbxGetPlaceholder.Checked)
{
list.AddRange(_listParameterInfo.Where(i => i.SubmitMethod == "Get"));
}
foreach (var parameter in list)
{
strParameter += $",string {parameter.ParameterName}";
}
if (!string.IsNullOrWhiteSpace(strParameter))
{
strParameter = strParameter.Remove(, );
}
stringBuilder.AppendLine($@"
public void Submit({strParameter})
{{");
}
stringBuilder.AppendLine($"var submitType = new SubmitType();");
stringBuilder.AppendLine($"submitType.Url=\"{uri}\";");
if (session.RequestMethod == "POST")
{
if (session.RequestBody.Length > )
{
string submitString;
if (cbxPostPlaceholder.Checked)
{
var str = Uri.UnescapeDataString(Encoding.UTF8.GetString(session.RequestBody));
submitString = GetUrlParameters(str);
stringBuilder.AppendLine($"submitType.SubmitString = $\"{submitString}\";");
}
else
{
submitString = Uri.UnescapeDataString(Encoding.UTF8.GetString(session.RequestBody));
//字符 转义
submitString = submitString.Replace("{", "{{");
submitString = submitString.Replace("}", "}}");
submitString = submitString.Replace("\"", "\\\"");
stringBuilder.AppendLine($"submitType.SubmitString = \"{submitString}\";");
}
}
}
var nameValue = new NameValueCollection();
var addedContentType = false;
foreach (var headers in session.RequestHeaders)
{
switch (headers.Name)
{
case "Accept":
stringBuilder.AppendLine($"submitType.Accept = \"{headers.Value}\";");
break;
case "Accept-Encoding":
stringBuilder.AppendLine($"submitType.AcceptEncoding = \"{headers.Value}\";");
break;
case "Accept-Language":
stringBuilder.AppendLine($"submitType.AcceptLanguage = \"{headers.Value}\";");
break;
case "User-Agent":
stringBuilder.AppendLine($"submitType.UserAgent = \"{headers.Value}\";");
break;
case "Content-Type":
addedContentType = true;
stringBuilder.AppendLine($"submitType.ContentType = \"{headers.Value}\";");
break;
case "Referer":
stringBuilder.AppendLine($"submitType.Referer = \"{headers.Value}\";");
break;
case "Content-Length":
break;
case "Host":
break;
case "Connection":
break;
case "Cookie":
stringBuilder.AppendLine(checkBoxCookie.Checked
? "submitType.Cookies = \"{cookies}\";"
: $"submitType.Cookies = \"{headers.Value}\";");
break;
case "Origin":
stringBuilder.AppendLine($"submitType.Origin = \"{headers.Value}\";");
break;
case "Cache-Control":
stringBuilder.AppendLine($"submitType.CacheControl = \"{headers.Value}\";");
break;
case "If-Modified-Since":
stringBuilder.AppendLine("//浏览器端缓存页面的最后修改时间,一般可以不使用,使用请使用上一个响应的结果");
stringBuilder.AppendLine("//submitType.IfModifiedSince = \"\";");
break;
default:
nameValue.Add(headers.Name, headers.Value);
break;
}
}
//如果不包含Content-Type 则给空
if (!addedContentType)
{
stringBuilder.AppendLine("submitType.ContentType = string.Empty;");
}
if (checkBoxProxy.Checked)
{ stringBuilder.AppendLine("submitType.AddProxyUri(runDataModel.ProxyUri);");
}
stringBuilder.AppendLine("submitType.UseCookieHelper = true;");
foreach (var key in nameValue.AllKeys)
{
stringBuilder.AppendLine($"submitType.RequestHeaders.Add(\"{key}\",\"{nameValue[key]}\");");
} if (cbxImg.Checked)
{
var str = $@"
var image = {(session.RequestMethod == "POST" ? "Post" : "Get")}.Image(submitType);
var validateCode = new ValidateCode {{ Image = image,Cookies = submitType.ReturnCookies }};
return validateCode;
";
stringBuilder.AppendLine(str);
}
else
{
stringBuilder.AppendLine(session.RequestMethod == "POST" ? "var html = Post.Html(submitType);" : "var html = Get.Html(submitType);"); }
if (!addMethodName)
{
stringBuilder.AppendLine(@"
}");
}
txtCode.Text = stringBuilder.ToString();
}
到这里就可以直接通过拖拽生成代码了