C#爬虫----Fiddler 插件开发 自动生成代码

2021-03-22 06:25

阅读:429

标签:post   ret   url   sub   builder   date   tla   nbsp   修改时间   

  哈喽^_^

  一般我们在编写网页爬虫的时候经常会使用到Fiddler 这个工具来分析http包,而且通常并不是分析一个包就够了的,所以为了把更多的时间放在分析http包上,自动化生成封包代码就尤为重要了(其实之前已经有第三方的插件了,自己编写主要是为了适配自定义的http请求模块)!

 首先注册拖拽事件 允许直接在Fiddler Session列表中拖入Session到插件界面

1         private void TrCode_Load(object sender, System.EventArgs e)
2         {
3             AllowDrop = true;
4             DragDrop += TrCode_DragDrop;
5             DragOver += TrCode_DragOver;
6         }

 1         private void TrCode_DragOver(object sender, DragEventArgs e)
 2         {
 3             e.Effect = e.Data.GetDataPresent("Fiddler.Session[]") ? DragDropEffects.Copy : DragDropEffects.None;
 4         }
 5 
 6         private void TrCode_DragDrop(object sender, DragEventArgs e)
 7         {
 8             //获取拖入的Session数据 数据格式Fiddler.Session[]
 9             var data = (Session[])e.Data.GetData("Fiddler.Session[]");
10             if (_sessions == data)
11             {
12                 return;
13             }
14             _sessions = data;
15             if ((data == null)) return;
16             if (data.Length > 1)
17             {
18                 AnalyticSession(data);
19             }
20             else
21             {
22                 //多个也只获取第一个进行解析
23                 AnalyticSession(data[0]);
24             }
25         }

获取到Session了 开始解析 并且生成代码 (生成的代码可以根据自己的情况去自定义 )

  1         /// 
  2         /// 生成TrCode
  3         /// 
  4         /// 
  5         /// 
  6         private void GenerateTrCode(Session session, bool addMethodName = false)
  7         {
  8 
  9             var stringBuilder = new StringBuilder(500);
 10             string uri;
 11             if (cbxGetPlaceholder.Checked && session.fullUrl.Split(?).Length > 1)
 12             {
 13                 var str = session.fullUrl;
 14                 var getUrl = str.Split(?);
 15                 if (getUrl.Length == 2)
 16                 {
 17                     str = getUrl[1];
 18                     str = GetUrlParameters(str);
 19                 }
 20                 uri = getUrl[0] + "?" + str;
 21             }
 22             else
 23             {
 24                 uri = session.fullUrl;
 25             }
 26             if (!addMethodName)
 27             {
 28                 var strParameter = string.Empty;
 29                 var list = new List();
 30                 if (cbxPostPlaceholder.Checked)
 31                 {
 32                     list.AddRange(_listParameterInfo.Where(i => i.SubmitMethod == "Post"));
 33                 }
 34                 if (cbxGetPlaceholder.Checked)
 35                 {
 36                     list.AddRange(_listParameterInfo.Where(i => i.SubmitMethod == "Get"));
 37                 }
 38                 foreach (var parameter in list)
 39                 {
 40                     strParameter += $",string {parameter.ParameterName}";
 41                 }
 42                 if (!string.IsNullOrWhiteSpace(strParameter))
 43                 {
 44                     strParameter = strParameter.Remove(0, 1);
 45                 }
 46                 stringBuilder.AppendLine($@"
 47         public void Submit({strParameter})
 48         {{");
 49             }
 50             stringBuilder.AppendLine($"var submitType = new SubmitType();");
 51             stringBuilder.AppendLine($"submitType.Url=\"{uri}\";");
 52             if (session.RequestMethod == "POST")
 53             {
 54                 if (session.RequestBody.Length > 0)
 55                 {
 56                     string submitString;
 57                     if (cbxPostPlaceholder.Checked)
 58                     {
 59                         var str = Uri.UnescapeDataString(Encoding.UTF8.GetString(session.RequestBody));
 60                         submitString = GetUrlParameters(str);
 61                         stringBuilder.AppendLine($"submitType.SubmitString = $\"{submitString}\";");
 62                     }
 63                     else
 64                     {
 65                         submitString = Uri.UnescapeDataString(Encoding.UTF8.GetString(session.RequestBody));
 66                         //字符 转义
 67                         submitString = submitString.Replace("{", "{{");
 68                         submitString = submitString.Replace("}", "}}");
 69                         submitString = submitString.Replace("\"", "\\\"");
 70                         stringBuilder.AppendLine($"submitType.SubmitString = \"{submitString}\";");
 71                     }
 72                 }
 73             }
 74             var nameValue = new NameValueCollection();
 75             var addedContentType = false;
 76             foreach (var headers in session.RequestHeaders)
 77             {
 78                 switch (headers.Name)
 79                 {
 80                     case "Accept":
 81                         stringBuilder.AppendLine($"submitType.Accept = \"{headers.Value}\";");
 82                         break;
 83                     case "Accept-Encoding":
 84                         stringBuilder.AppendLine($"submitType.AcceptEncoding = \"{headers.Value}\";");
 85                         break;
 86                     case "Accept-Language":
 87                         stringBuilder.AppendLine($"submitType.AcceptLanguage = \"{headers.Value}\";");
 88                         break;
 89                     case "User-Agent":
 90                         stringBuilder.AppendLine($"submitType.UserAgent = \"{headers.Value}\";");
 91                         break;
 92                     case "Content-Type":
 93                         addedContentType = true;
 94                         stringBuilder.AppendLine($"submitType.ContentType = \"{headers.Value}\";");
 95                         break;
 96                     case "Referer":
 97                         stringBuilder.AppendLine($"submitType.Referer = \"{headers.Value}\";");
 98                         break;
 99                     case "Content-Length":
100                         break;
101                     case "Host":
102                         break;
103                     case "Connection":
104                         break;
105                     case "Cookie":
106                         stringBuilder.AppendLine(checkBoxCookie.Checked
107                             ? "submitType.Cookies = \"{cookies}\";"
108                             : $"submitType.Cookies = \"{headers.Value}\";");
109                         break;
110                     case "Origin":
111                         stringBuilder.AppendLine($"submitType.Origin = \"{headers.Value}\";");
112                         break;
113                     case "Cache-Control":
114                         stringBuilder.AppendLine($"submitType.CacheControl = \"{headers.Value}\";");
115                         break;
116                     case "If-Modified-Since":
117                         stringBuilder.AppendLine("//浏览器端缓存页面的最后修改时间,一般可以不使用,使用请使用上一个响应的结果");
118                         stringBuilder.AppendLine("//submitType.IfModifiedSince = \"\";");
119                         break;
120                     default:
121                         nameValue.Add(headers.Name, headers.Value);
122                         break;
123                 }
124             }
125             //如果不包含Content-Type 则给空
126             if (!addedContentType)
127             {
128                 stringBuilder.AppendLine("submitType.ContentType = string.Empty;");
129             }
130             if (checkBoxProxy.Checked)
131             {
132 
133                 stringBuilder.AppendLine("submitType.AddProxyUri(runDataModel.ProxyUri);");
134             }
135             stringBuilder.AppendLine("submitType.UseCookieHelper = true;");
136             foreach (var key in nameValue.AllKeys)
137             {
138                 stringBuilder.AppendLine($"submitType.RequestHeaders.Add(\"{key}\",\"{nameValue[key]}\");");
139             }
140 
141             if (cbxImg.Checked)
142             {
143                 var str = $@"
144                 var image = {(session.RequestMethod == "POST" ? "Post" : "Get")}.Image(submitType);
145                 var validateCode = new ValidateCode {{ Image = image,Cookies = submitType.ReturnCookies }};
146                 return validateCode;
147                 ";
148                 stringBuilder.AppendLine(str);
149             }
150             else
151             {
152                 stringBuilder.AppendLine(session.RequestMethod == "POST" ? "var html = Post.Html(submitType);" : "var html = Get.Html(submitType);");
153 
154             }
155             if (!addMethodName)
156             {
157                 stringBuilder.AppendLine(@"
158     }");
159             }
160             txtCode.Text = stringBuilder.ToString();
161         }

到这里就可以直接通过拖拽生成代码了

 

技术分享图片

 

C#爬虫----Fiddler 插件开发 自动生成代码

标签:post   ret   url   sub   builder   date   tla   nbsp   修改时间   

原文地址:https://www.cnblogs.com/trevan/p/9494101.html

上一篇:C# Redis写入程序

下一篇:C#转义字符


评论


亲,登录后才可以留言!