我想要捕获本地POST请求并解析其数据 仅用于测试,FiddlerCore应仅响应它已解析的数据 这是FidlerCore封装的代码:
private void FiddlerApplication_BeforeRequest(Session oSession)
{
if (oSession.hostname != "localhost") return;
eventLog.WriteEntry("Handling local request...");
oSession.bBufferResponse = true;
oSession.utilCreateResponseAndBypassServer();
oSession.oResponse.headers.HTTPResponseStatus = "200 Ok";
oSession.oResponse["Content-Type"] = "text/html; charset=UTF-8";
oSession.oResponse["Cache-Control"] = "private, max-age=0";
string body = oSession.GetRequestBodyAsString();
oSession.utilSetResponseBody(body);
}
以下是请求发件人的代码:
const string postData = "This is a test that posts this string to a Web server.";
try
{
WebRequest request = WebRequest.Create("http://localhost/?action=print");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
request.ContentType = "text/html";
request.Method = "POST";
using (Stream stream = request.GetRequestStream())
{
stream.Write(byteArray, 0, byteArray.Length);
}
using (WebResponse response = request.GetResponse())
{
txtResponse.Text = ((HttpWebResponse)response).StatusDescription;
using (Stream stream = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(stream))
{
string responseFromServer = streamReader.ReadToEnd();
streamReader.Close();
txtResponse.Text = responseFromServer;
}
}
}
}
catch (Exception ex)
{
txtResponse.Text = ex.Message;
}
我收到以下错误:
服务器提交了协议违规。节= ResponseStatusLine
我做错了什么?
答案 0 :(得分:0)
通过改变来实现它:
WebRequest request = WebRequest.Create("http://localhost/?action=print");
到
WebRequest request = WebRequest.Create("http://localhost:8877/?action=print");
FiddlerCore正确拦截从浏览器调用此URL,而无需指定端口号。我不认为我应该插入监听端口,因为FiddlerCore应该拦截所有流量,对吗?