[英]Unit testing a method with HTTP post request by mocking HttpWebRequest
[英]Read the HTTP POST request of HttpWebRequest
我需要创建 Http POST 请求和一些 GET 请求作为我正在编写的一些测试的字符串。 目前,我的测试使用 StringBuilder 和从 fiddler 中提取的硬编码 POST 请求构建它们,如下所示:
var builder = new StringBuilder();
builder.Append("POST https://some.web.pg HTTP/1.1\r\n");
builder.Append("Content-Type: application/x-www-form-urlencoded\r\n");
builder.Append("Referer: https://some.referer.com\r\n");
builder.Append("Accept-Language: en-us\r\n");
builder.Append("Accept-Encoding: gzip, deflate\r\n");
builder.Append("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\r\n");
builder.Append("Host: login.yahoo.com\r\n");
// ... other header info
builder.Append("\r\n");
builder.Append("post body......\r\n");
var postData = builder.ToString();
这很快使我的测试变得混乱,我希望有一种更简洁的方法来构建这些 POST 请求。 我一直在研究 HttpWebRequest 类,希望它可以为我创建这些。 我认为在背后一定有某种方法来构建我试图以某种形式创建的这个确切请求。 但是,唉,GetRequestStream 是一个可写的唯一流。
有没有办法读取 HttpWebRequest 将生成的请求流(然后将其更改为字符串)? 甚至任何关于如何生成这些 POST 请求的想法都可以。
这是一个发出 Get 请求的 msdn 示例:
using System;
使用 System.Net; 使用 System.IO;
namespace MakeAGETRequest_charp { /// /// Class1 的摘要描述。 /// class Class1 { static void Main(string[] args) { string sURL; sURL = " http://www.microsoft.com ";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
WebProxy myProxy = new WebProxy("myproxy",80);
myProxy.BypassProxyOnLocal = true;
wrGETURL.Proxy = WebProxy.GetDefaultProxy();
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine();
}
}
} 和这里的 post 请求(来自带有 post 的 HTTP 请求)
HttpWebRequest httpWReq =
(HttpWebRequest)WebRequest.Create("http:\\domain.com\page.asp");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user";
postData += "&password=pass";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream newStream = httpWReq.GetRequestStream())
{
newStream.Write(data,0,data.Length);
}
我建议您使用模拟,因为它是单元测试的最佳实践:请参阅堆栈单元测试 C# 中的 HTTP 请求上的此答案
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(yoururllink);
var c = HttpContext.Current;
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
string strResponse_copy = strRequest; //Save a copy of the initial info sent from your url link
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.