Search code examples
requestresponsefiddlerfiddlercorefiddler-dev

Fiddler 2: Resend request through FiddlerScript


Hello StackOverflow Folks,

I'm new to Fiddler 2, but I seem to be getting along with it pretty good. Although I have one problem that I can't seem to solve.

What I want to do is actually very simple I think. I want to intercept a request, let it run, but if the response doesn't suit me I want it to resend the request and make the initial request nonexcistent. This using FiddlerScript.

Why is this useful: in cases where you send a request but the response is different everytime. and you just want the right kind of response.

What I have so far:

 static function OnBeforeResponse(oSession: Session)
 {
 if (oSession.uriContains("/stackoverflowexample"))
    {
        if(oSession.GetRequestBodyAsString().Contains("GetRandomItem"))
        {

            if(oSession.GetResponseBodyAsString().ToString().Contains("ItemID"))
            {
                var body = oSession.GetResponseBodyAsString();
                var item = 0;
                for(var i = 0; i< body.Length; i++)
                {
                    if(i < body.Length -7)
                    {
                        if(body.Substring(i, 6) == "ItemID")
                        {
                            item= Convert.ToInt32(body.Substring(i+7,1));
                        }
                    }
                }
                MessageBox.Show(item.ToString());
                if(item < 2536) //for example itemid must be higher than 2536
                {
                    //STOP / MAKE this session nonexcistent
                    //RESEND CURRENT REQUEST to get new response

                    oSession.state = SessionStates.SendingRequest;
                    FiddlerObject.utilIssueRequest(oSession.oRequest.ToString());

                }
            }
        }
    }
  }

Every possible solution using Fiddlerscript is welcome.

Thank you Stackoverflow cummunity! (and Fiddler developers)


Solution

  • Setting the state of the Session back to SendingRequest doesn't do what you'd hope.

    Here's an example of the sort of thing you're trying to do. https://groups.google.com/forum/#!searchin/httpfiddler/retry/httpfiddler/3OZQVmQZdR0/uvqTyl3w2BAJ

    if (!String.IsNullOrEmpty(oSession["X-RetryNumber"])) return;
    for(var iRetry: int = 1; iRetry < 5; ++iRetry)
     { 
       var oSD = new System.Collections.Specialized.StringDictionary();
        oSD.Add("X-RetryNumber", iRetry.ToString());
        var newSession = FiddlerApplication.oProxy.SendRequestAndWait(oSession.oRequest.headers, oSession.requestBodyBytes, oSD, null);
        if (200 == newSession.responseCode) // <--- UPDATE THIS TO WHATEVER YOU LIKE
        {
           // If we were successful on a retry, bail here!
           oSession.oResponse.headers = newSession.oResponse.headers;
           oSession.responseBodyBytes = newSession.responseBodyBytes;
          oSession.oResponse.headers["Connection"] = "close"; // Workaround limitation
    
         break;
        }
    }