本文整理汇总了C#中HttpRequest.ParsePostBody方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequest.ParsePostBody方法的具体用法?C# HttpRequest.ParsePostBody怎么用?C# HttpRequest.ParsePostBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.ParsePostBody方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestParsePost
public void TestParsePost()
{
string inputStr = @"-----------------------------265001916915724
Content-Disposition: form-data; name=""y""
This is what should be found in ""y"" at the end of the test.
-----------------------------265001916915724
Content-Disposition: form-data; name=""What a wonderful day it is today; so wonderful in fact, that I'm inclined to go out and meet friends""
<CRLF>(this)<CRLF>
-----------------------------265001916915724
Content-Disposition: form-data; name=""documentfile""; filename=""temp.htm""
Content-Type: text/html
<html>
<head>
</head>
<body>
<form action='http://localhost:8988/index.php' method='post' enctype='multipart/form-data'>
<input type='hidden' name='x' value='a b'>
<textarea name='y'>a b</textarea>
<input type='file' name='z'>
<input type='submit'>
</form>
</body>
</html>
-----------------------------265001916915724--
";
byte[] testCase = Encoding.UTF8.GetBytes(inputStr);
var directoryNotToBeCreated = @"C:\serverstests";
int i = 1;
while (Directory.Exists(directoryNotToBeCreated))
{
i++;
directoryNotToBeCreated = @"C:\serverstests_" + i;
}
for (int cs = 1; cs < testCase.Length; cs++)
{
HttpRequest r = new HttpRequest
{
Url = new HttpUrl("example.com", "/"),
Headers = new HttpRequestHeaders
{
ContentLength = inputStr.Length,
ContentMultipartBoundary = "---------------------------265001916915724",
ContentType = HttpPostContentType.MultipartFormData
},
Method = HttpMethod.Post
};
using (Stream f = new SlowStream(new MemoryStream(testCase), cs))
{
r.ParsePostBody(f, directoryNotToBeCreated);
var gets = r.Url.Query.ToList();
var posts = r.Post;
var files = r.FileUploads;
Assert.IsTrue(files.ContainsKey("documentfile"));
Assert.AreEqual("temp.htm", files["documentfile"].Filename);
Assert.AreEqual("text/html", files["documentfile"].ContentType);
using (var stream = files["documentfile"].GetStream())
{
string fileContent = Encoding.UTF8.GetString(stream.ReadAllBytes());
Assert.AreEqual(@"<html>
<head>
</head>
<body>
<form action='http://localhost:8988/index.php' method='post' enctype='multipart/form-data'>
<input type='hidden' name='x' value='a b'>
<textarea name='y'>a b</textarea>
<input type='file' name='z'>
<input type='submit'>
</form>
</body>
</html>",
fileContent);
}
Assert.AreEqual(0, gets.Count);
Assert.AreEqual(2, posts.Count);
Assert.AreEqual(1, files.Count);
Assert.IsTrue(posts.ContainsKey("y"));
Assert.AreEqual(@"This is what should be found in ""y"" at the end of the test.", posts["y"].Value);
Assert.IsTrue(posts.ContainsKey("What a wonderful day it is today; so wonderful in fact, that I'm inclined to go out and meet friends"));
Assert.AreEqual("\r\n<CRLF>(this)<CRLF>\r\n",
posts["What a wonderful day it is today; so wonderful in fact, that I'm inclined to go out and meet friends"].Value);
}
}
Assert.IsFalse(Directory.Exists(directoryNotToBeCreated));
}