使用fiddler core

时间:2017-06-21 07:36:58

标签: c# .net fiddler fiddlercore fiddler-dev

我试图拦截响应并更改特定网址的响应html正文。我能够更新html内容中的字符串,但是当我在浏览器中检查时,我无法找到我所做的更改。

我用它来改变回应

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

当我正在调试时,我看到响应已被修改,但是当我检入浏览时,我无法看到所需的结果可能是什么问题 Before altering the respone

After altering the respone

1 个答案:

答案 0 :(得分:0)

最后我解决了..

我错过了在beforeRequest事件中设置oSession.bBufferResponse = true; ..

FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;

private void FiddlerApplication_BeforeRequest(Session oSession)
 {
   oSession.bBufferResponse = true;
 }