交易重复提交自动化工具

2018-08-30  本文已影响0人  七里台吴彦祖

WindowsFormApp1

思路

image.png

调用库

工具->NuGet包管理器->管理解决方案的NuGet程序包,安装几个包:

FiddlerCore2:代理服务器的

HtmlAgilityPack:解析html的

Newtonsoft.Json:解析json的

上面三个包很好用,别的几个导入了之后没有用到。

在程序开头再using一下就OK了。

using Fiddler;

using HtmlAgilityPack;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

button4_Click:intercept on

开启代理:

FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default);

接下来,

Fiddler.FiddlerApplication.BeforeRequest += delegate (Fiddler.Session oS){
    Monitor.Enter(oAllSessions);
    oAllSessions.Add(oS);
    Monitor.Exit(oAllSessions);}

把所有通过的session都存在 List<Fiddler.Session> oAllSessions

直到运行Fiddler.FiddlerApplication.Shutdown();为止

展示Session:Next、Last和List

Next是 button1_Click()函数,
Last是 button5_Click()函数,
List是 button3_Click_1()函数。

现在我们有个oAllSessions,里面有很多Session。
于是先来遍历一遍,看看里面的Session都长什么样。

Monitor.Enter(oAllSessions);
foreach (Session oS in oAllSessions)
                {
                    testConsole.Text += String.Format("{0} {1} {2}\n{3} {4}\n\n", oS.id, oS.oRequest.headers.HTTPMethod, Ellipsize(oS.fullUrl, 60), oS.responseCode, oS.oResponse.MIMEType);
                    testConsole.Text += "\r\n";
                }

奇形怪状的。

下面我用了一个全局变量index来记录现在操作的是哪个Session。
Session oS
oS.id == index

然后那三个按钮就是用来index++index--……

把乱七八糟的Session过滤掉:

Monitor.Enter(oAllSessions);

            LOOP: foreach (Session oS in oAllSessions)
            {
                
                if (oS.id == index)
                {
                    //过滤掉CONNECT
                    if (oS.RequestMethod == "CONNECT")
                    {
                        index++;
                        goto LOOP;
                        //用goto不好,有时间改写成循环
                    }
                    else if(oS.host != "vip.dccnet.com.cn:449")
                    {
                        index++;
                        goto LOOP;
                        //用goto不好,有时间改写成循环
                    }
                    else
                        ViewSession(oS);
                   
                }

            }

Monitor.Exit(oAllSessions);

Session的结构

我有个特别好的东西,展示oS.oRequest,oS.oResponse里面还有什么属性和方法:Fiddler ScriptEditor

button6_Click:重发

HTTPRequestHeaders reHeader = oS.RequestHeaders;
byte[] reBody = oS.requestBodyBytes;

                    if (oS.RequestMethod == "GET")
                    {
                        MessageBox.Show("GET!");
                        //GET方法
                        HttpClient httpClient = new HttpClient();
                        //httpClient.MaxResponseContentBufferSize = 256000;

                        //把这个BufferSize调大
                        httpClient.MaxResponseContentBufferSize = 25600000;

                        String url = oS.fullUrl;
                        //request.Url;
                        HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
                        //HttpResponseMessage response = httpClient.GetAsync(request.RequestUri).Result;

                        String result = response.Content.ReadAsStringAsync().Result;

                        //Resend.AppendText("\r\n");
                        //System.Environment.NewLine
                        Resend.Clear();
                        Resend.Text += result;
                    }

POST也差不多。

button7_Click:比较

代码前一部分还是把Session展示出来。
然后重发的部分也和上面一样。
(应该放到函数里……)

str1是Session的request
str1 = oS.GetResponseBodyAsString();
str2是重发后返回的
str2 = response.Content.ReadAsStringAsync().Result;

Compare()函数比较两个字符串,这个逻辑还比较清晰。

上一篇 下一篇

猜你喜欢

热点阅读