1

我正在尝试拦截响应并更改特定 url 的响应 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);
  }

当我调试时,我看到响应已被修改,但是当我在浏览中签入时,我无法看到所需的结果可能是什么问题 在更改响应之前

更改响应后

4

1 回答 1

2

最后我解决了..

我只是错过了oSession.bBufferResponse = true; beforeRequest 事件的设置..

FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;

private void FiddlerApplication_BeforeRequest(Session oSession)
 {
   oSession.bBufferResponse = true;
 }
于 2017-06-22T05:02:40.140 回答