2017-06-21 65 views
0

我试图拦截响应并更改特定url的响应html正文。我能够更新html内容中的字符串,但是当我登录浏览器时,我无法找到我所做的更改。使用fiddler核心修改响应

我用这对改变响应

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

+0

'ReplaceFirst'做了什么?那是你的代码吗? – mjwills

+0

ReplaceFirst用于用所需的单词替换响应正文中第一个单词。 –

+0

如果替换'oSession.utilDecodeResponse(); var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);'with'oSession.utilDecodeResponse(); 字符串oBody = oSession.GetResponseBodyAsString();' – mjwills

回答

0

最后我解决了它..

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

FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest; 

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