Fiddler Script - Need Help Setting Body that is a ZipFile

158 views
Skip to first unread message

Grant Goulet

unread,
Nov 17, 2016, 12:01:06 AM11/17/16
to Fiddler
A program I use makes a connection to a server and the server sends a response back that contains a ZIP file in the ResponseBody.
I am able to save the ZIP file and I then extract it, and I massage the XML file that is inside the ZIP file.
I then ZIP it back up and attempt to update the ResponseBody with updated zip file.
The ReponseBody doesn't seem to be updating correctly. I use the Fiddler GUI / Dialogs to right click and Save the Response to a zip file to test it out, and the zip file won't open.
If I attempt to open the zip file that I set the ResponseBody to, it opens just fine.
Here's my code

var s_qs = (oSession.url + "?").split("?")[1];
var querystring = HttpUtility.ParseQueryString(s_qs);
var FileID = querystring.Get("fileid");
var UserProfileString = System.Environment.GetEnvironmentVariable("USERPROFILE");
oSession.SaveResponseBody(UserProfileString+"\\desktop\\fiddler\\"+FileID+".zip");
System.IO.Compression.ZipFile.ExtractToDirectory(UserProfileString+"\\desktop\\fiddler\\"+FileID+".zip", UserProfileString+"\\desktop\\fiddler");
var XMLFile = UserProfileString+"\\desktop\\fiddler\\"+FileID+".xml";
var doc = new XmlDocument();
doc.Load(XMLFile);
//I left the code out that massages the XML file as it's irrelevant
var NewXMLFile = UserProfileString+"\\desktop\\fiddler\\modified\\"+FileID+".xml";
doc.Save(NewXMLFile);
System.IO.Compression.ZipFile.CreateFromDirectory(UserProfileString+"\\desktop\\fiddler\\modified", UserProfileString+"\\desktop\\fiddler\\"+FileID+".zip");
//The code below is where the ResponseBody gets set from the newly created zip file. I believe this is where the problem is.
//I have attached the zip file to this post that the line below reads
var ThisFile = File.ReadAllText(UserProfileString+"\\desktop\\fiddler\\"+FileID+".zip");
oSession.utilSetResponseBody(ThisFile);
oSession.oResponse.headers.Remove("Content-Length"); 
oSession.oResponse.headers.Add("Content-Length", oSession.responseBodyBytes.Length); 

Thanks in advance for any help anyone can provide.
TESTING.zip

Grant Goulet

unread,
Nov 17, 2016, 12:23:51 PM11/17/16
to Fiddler
Solved my own problem with this line of code.

oSession["x-replywithfile"] = UserProfileString+"\\desktop\\fiddler\\"+FileID+".zip";

Eric Lawrence

unread,
Nov 17, 2016, 5:29:21 PM11/17/16
to Fiddler
If you're curious, the problem is that the ReadAllText API reads text, not bytes. You should use the ReadAllBytes API instead.
Reply all
Reply to author
Forward
0 new messages