复制、修改和发送请求 POST Fiddler

Posted

技术标签:

【中文标题】复制、修改和发送请求 POST Fiddler【英文标题】:Copying, modifying and sending request POST Fiddler 【发布时间】:2021-12-12 14:04:40 【问题描述】:

对不起我的英语)))

我一直在尝试解决我的问题很长时间,但不幸的是到目前为止没有成功(((( 我有一个特定类型的 POST 请求,我需要对其进行更改并将其发送到另一个地址,并且原始请求应该进一步发送到它的地址。我正在尝试与交易所合作。更重要的是,我有这样的要求:

https://api.binance.com/api/v3/order?symbol=CELRBTC&orderId=73902412&recvWindow=55000&timestamp=1635266807744&signature=556b9c4121eb819d96a440b54661181e75ab2324a9d3b5fe0a73dd793626prec>

我需要在原始请求之后发送此请求

https://fapi/binance.com/fapi/v1/order?symbol=KEEPUSDT&side=BUY&type=MARKET&quantity=100&timeInForce=GTC&recvWindow=5000&timestamp=1591702613943&signature= 3c661234138461fcc7a7d8746c6558c9842d4e17084040840

另外,公共API是在header中传递的

连接:保持活动状态
内容类型:application/x-www-form-urlencoded
接受:application/json,text/plain; q=0.9,文本/html;q=0.8,
接受字符集:UTF-8,*;q=0.8
接受编码:gzip,放气
X-MBX-APIKEY:YIGXAtXbQg0790CnIvKzo3oIQPmEPwiySjdQj28h0H3g87D2rwcun0kRWvh4m9J6
主办方:api.binance.com

我尝试在 onBeforeRequest 方法中完成所有这些工作

static function OnBeforeRequest(oSession: Session) 
   
    
if (oSession.RequestMethod == "POST" &&  oSession.uriContains("order") ) 
   
     oSession.utilDecodeResponse()
    var strBody=oSession.GetRequestBodyAsString();


       // timestamp, time ms
        
        var timestampSTART= oSession.url.IndexOf("timestamp=") + 10; 
        
        var timestampEND= oSession.url.IndexOf("&", timestampSTART);   
       
        var timestamp = oSession.url.Substring(timestampSTART, 13);
        
       
        // SYMBOL
        
        var symbolStart = oSession.url.IndexOf("symbol=")+7;
       
        var symbolend = oSession.url.IndexOf("BTC", symbolStart)+3;
   
        var symbol = oSession.url.Substring(symbolStart, symbolend-symbolStart);
        
        
       //  Signature (timestamp+SecretAPIKey=Hmac Sha256)   theoretically, it can be taken from the original request, but it is more reliable to make your own 
       var signStart = oSession.url.IndexOf("signature=")+10;
                     
       var sign = oSession.url.Substring(signStart);
        
       
        //PRICE
        
     var PriceStart = oSession.url.IndexOf("price=")+6;
     var PriceEND = oSession.url.IndexOf("&", PriceStart);
     var priceStr = oSession.url.Substring(PriceStart, PriceEND-PriceStart);
     var price = parseFloat(priceStr);
        
                                
        // Quantity
        
        var quantity = 50/ price*63000;
  
        var apiBIN =  "https://fapi.binance.com/fapi/v1/order?" ;
      
       
     //    var result = apiBIN+"symbol="+symbol+"&side=BUY&type=MARKET&quantity="+ quantity+"&timeInForce=GTC&recvWindow=5000&timestamp="+timestamp+"&signature="+sign;
            
     //    oSession.utilSetRequestBody(result)
  
     //   FiddlerApplication.oProxy.SendRequest(oSession.RequestHeaders, oSession.requestBodyBytes, null);

已从请求中选择了必要的参数,但我不明白如何以任何方式发送它,而且我还丢失了带有 API 密钥的标头。 另外,我想注意“签名”参数,它是使用 Hmac Sha256 算法从秘密 API 密钥和时间创建的,我也无法弄清楚如何在代码中描述它。

如果有任何帮助,我将不胜感激,并有可能为实质性帮助支付一些费用。

【问题讨论】:

FiddlerScript 基于 .Net 技术,因此所有 .Net 类都可用,对于 HMACSHA256,您可以使用 docs.microsoft.com/en-us/dotnet/api/… new System.Security.Cryptography.HMACSHA256(secretKey); 非常感谢您的帮助,这非常重要!!!剩下的只是弄清楚如何在发送主要请求后发送单独的请求。目前,我无法运行“会话完成后的静态函数(会话):会话”方法,它没有运行(((我应该在哪个地方正确写呢? 如果您想主请求之后发出第二个请求,您应该考虑将其发送到OnBeforeResponse。通过会话对象,请求应该仍然可以访问以生成修改后的第二个请求。 经过多次尝试,我几乎成功了!我还有 1 个主要问题。如何使用密钥加密查询字符串。我找到了指定 System.Security class.Cryptography.HMAC SHA256() 的描述,但是由于我的技能较弱,我不明白如何正确使用它。我需要使用我的密钥加密请求正文,我该怎么做?在我找到的示例中,我看到了密钥如何仍然被翻译成字节码,然后哈希两次,然后我完全糊涂了。您能否举例说明如何使用 FiddlerScript 的密钥对参数进行加密? 使用 HMAC 不能加密任何东西。 HMAC 生成消息验证码 (MAC),即对称签名(不同于 RSA 等非对称签名)。 HMAC 是一个哈希函数加上一个密钥。 【参考方案1】:

我使用 C# 来解决这个问题。我不知道 Fiddler 可以选择语言。从技术上讲,我已经实现了我的目标,但目前财务正在编写一个不正确的请求。我将单独处理这个问题。特别感谢罗伯特的帮助。

    public static void OnBeforeResponse(Session oSession)
    
       
    
        if (oSession.HTTPMethodIs("POST") && oSession.uriContains("order")) 
         


              String strBody =  oSession.GetRequestBodyAsString(); 


                 //Price 

               int PriceStart = strBody.IndexOf("price=")+6;

               int PriceEND = strBody.IndexOf("&", PriceStart);

               string priceStr = strBody.Substring(PriceStart, PriceEND-PriceStart);

               float priceF = float.Parse(priceStr, System.Globalization.CultureInfo.InvariantCulture);
 

                 // SYMBOL
        
               int symbolStart = strBody.IndexOf("symbol=")+7;
       
               int symbolend = strBody.IndexOf("BTC", symbolStart);
   
               string symbol = strBody.Substring(symbolStart, symbolend-symbolStart);
        


                // Quantity
        
                int quantStart = strBody.IndexOf("quantity=")+9;
                   
                int quantend = strBody.IndexOf("&price", quantStart); 

                string quant = strBody.Substring(quantStart, quantend-quantStart);

                float quantity = float.Parse(quant, System.Globalization.CultureInfo.InvariantCulture)*2;
    

                // timestamp

                decimal timestamp = Math.Round(Convert.ToDecimal(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds), 0);

       

                //Sign

                Encoding ascii = Encoding.ASCII;

                string secretKey = "lfaeGkjitNwvyG2lqDueMhSAOzRFlzL73w5pKRCAvSy7YrxyTkvwKCcHBHj...";


                HMACSHA256 hmac = new HMACSHA256(ascii.GetBytes(secretKey));


                //   string query_string_LIMIT = "symbol="+symbol+"USDT&side=BUY&type=LIMIT&timeInForce=GTC&quantity="+quantity+"&price="+priceF+"&recvWindow=5000&timestamp="+timestamp+"&signature=";

                string result = "symbol="+symbol+"USDT&side=BUY&type=MARKET&quantity="+ quantity+"&recvWindow=5000&timestamp="+timestamp+"&signature=";
        

                String signature = BitConverter.ToString(hmac.ComputeHash(ascii.GetBytes(result))).Replace("-","");

    
   



                oSession.host="fapi.binance.com";
  
                string resultRequest = "symbol="+symbol+"USDT&side=BUY&type=MARKET&quantity="+ quantity+"&recvWindow=5000&timestamp="+timestamp+"&signature="+signature;


                

                byte[] resulByte =   System.Text.Encoding.ASCII.GetBytes(resultRequest);


                //oSession.utilReplaceInRequest("api/v3/order","fapi/v1/order?"+resultFin);

                oSession.url = oSession.url.Replace("api/v3/order", "fapi/v1/order?"+resultRequest);      


                FiddlerApplication.oProxy.SendRequest (oSession.RequestHeaders, resulByte, null);            

           

【讨论】:

以上是关于复制、修改和发送请求 POST Fiddler的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Web 浏览器发送 POST 请求? [复制]

如何在 webclient 的 post 请求中发送 x-www-form-urlencoded? [复制]

如何从我的 NodeJS Web 应用程序的前端 JS 文件中的函数发送 POST 请求? [复制]

发送post请求的接口

fiddler模拟发送post请求

使用 HttpClient 和 C# 在 post 请求中发送 json