当前位置: 首页>>代码示例>>C#>>正文


C# HttpRequest.ParsePostBody方法代码示例

本文整理汇总了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));
        }
开发者ID:RT-Projects,项目名称:RT.Servers,代码行数:97,代码来源:RequestParseTests.cs


注:本文中的HttpRequest.ParsePostBody方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。