2017-06-21 66 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; 
}