简体   繁体   中英

Modify the response using fiddler core

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 更改重做之前

更改后

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM