我正在使用 Fiddler 的“FiddlerScript”来修改来自 Web 服务器的响应,以便我可以在我的应用程序中测试响应。
这是我的 OnBeforeResponse 函数:
static function OnBeforeResponse(oSession: Session) {
// This code was already here, leaving it
if (m_Hide304s && oSession.responseCode == 304) {
oSession["ui-hide"] = "true";
}
// Here is new code to modify server's response
if(oSession.HostnameIs("mydomain.com") && oSession.uriContains("config")) {
// Color this response, so we can spot it in Fiddler
oSession["ui-backcolor"] = "lime";
// Convert the request body into a string
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
var j: Fiddler.WebFormats.JSON.JSONParseResult;
// Convert the text into a JSON object
// In this case our JSON root element is a dictionary (HashTable)
j = Fiddler.WebFormats.JSON.JsonDecode(oBody);
// Inside of our dictionary, we have an array (ArrayList) called "placements"
var testObject = j.JSONObject["test"];
/* Change this to different values, e.g. "0.0", 0.0, null, "", etc. */
/* This works */
testObject["consent_version"] = "";
/* This works */
testObject["consent_version"] = 0.0;
/* This works */
testObject["consent_version"] = "0.0";
/* This fails */
testObject["consent_version"] = null;
// Convert back to a byte array
var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
// Convert json to bytes, storing the bytes in request body
var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
oSession.ResponseBody = mod;
}
}
我可以将 testObject["consent_version"] 设置为除 null 之外的任何值。如果我将其设置为 null,Fiddler 会创建没有任何值的 JSON,并且 JSON 格式不正确,如下所示:
"consent_version":,
请注意,“consent_version”之后和逗号之前没有值。
有谁知道我如何使用 FiddlerScript(基于 JScript.Net)来设置空值?