2

I am trying to intercept the response and alter the response html body of the specific url. i am able to update the string in the html content but when i check in browser i am not able to find the changes i made.

i am using this for altering the response

private void FiddlerApplication_BeforeResponse(Session oSession)
 {
   //if (!oSession.fullUrl.ToLower().Contains(txtCaptureUrl.Text.Trim().ToLower()))
   //    return;

   if (oSession.fullUrl.ToLower().Contains("localhost"))
       return;

   //Search and replace in HTML.
   if (oSession.fullUrl.ToLower().Contains("prohance"))
      {
    if (oSession.HostnameIs("10.10.10.199") && oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html"))
       {
         oSession.bBufferResponse = true;
         // Remove any compression or chunking
         oSession.utilDecodeResponse();
         var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
         //oBody = ReplaceFirst(oBody, "</script>", "<script type='text/javascript'>alert(123)</script>");
         oBody = ReplaceFirst(oBody, "ATTENDANCE", "RAVIKANTH");
         oSession.utilSetResponseBody(oBody);
         oSession.utilDecodeResponse();
         var oBody1 = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
   }
   return;
   }
 }

public string ReplaceFirst(string text, string search, string replace)
 {
    int pos = text.IndexOf(search);
      if (pos < 0)
       {
         return text;
       }
      return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
  }

when i am debugging i am see the response has been modified but when i check in browse i am not able to see the desired result what may be the problem Before altering the respone

After altering the respone

Borislav Ivanov
  • 4,684
  • 3
  • 31
  • 55
Ravi Kanth
  • 1,182
  • 13
  • 38
  • What does `ReplaceFirst` do? Is that your code? – mjwills Jun 21 '17 at 08:40
  • ReplaceFirst is used to replace the first occurrence of the word in the response body with desired word.. – Ravi Kanth Jun 21 '17 at 08:46
  • Does it work if you replace `oSession.utilDecodeResponse(); var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);` with `oSession.utilDecodeResponse(); string oBody = oSession.GetResponseBodyAsString();` – mjwills Jun 21 '17 at 09:50
  • That doesn't make any change. my doubt is while debugging i can see that what ever content i am trying to replace is effecting but when i check through view page source it is not affecting there. am i doing anything wrong with binding the response.. – Ravi Kanth Jun 21 '17 at 10:00
  • How often does the word `ATTENDANCE` appear in the body? – mjwills Jun 21 '17 at 10:04
  • What is the url in your web browser? – mjwills Jun 21 '17 at 10:42

1 Answers1

2

Finally i solved it..

i just missed the setting oSession.bBufferResponse = true; on beforeRequest event..

FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;

private void FiddlerApplication_BeforeRequest(Session oSession)
 {
   oSession.bBufferResponse = true;
 }
Ravi Kanth
  • 1,182
  • 13
  • 38