コード例 #1
0
        public async Task AddFormFiledAsyncTest()
        {
            var reqeust = new HttpApiRequestMessage();
            await Assert.ThrowsAsync <NotSupportedException>(() => reqeust.AddFormFieldAsync("name", "value"));

            reqeust.Method     = System.Net.Http.HttpMethod.Post;
            reqeust.RequestUri = new Uri("http://webapiclient.com");
            await reqeust.AddFormFieldAsync("name", "laojiu");

            await reqeust.AddFormFieldAsync(new[] { new KeyValue("age", "18") });

            var body = await reqeust.Content.ReadAsStringAsync();

            Assert.Equal("name=laojiu&age=18", body);
            Assert.True(reqeust.Content.Headers.ContentType.MediaType == "application/x-www-form-urlencoded");
        }
コード例 #2
0
        public async Task AddFormDataTextTest()
        {
            string get(string name, string value)
            {
                return($@"Content-Disposition: form-data; name=""{name}""

{HttpUtility.UrlEncode(value, Encoding.UTF8)}");
            }

            var reqeust = new HttpApiRequestMessage();

            reqeust.Method     = System.Net.Http.HttpMethod.Post;
            reqeust.RequestUri = new Uri("http://webapiclient.com");
            reqeust.AddFormDataText("name", "laojiu");
            reqeust.AddFormDataText(new[] { new KeyValue("age", "18") });

            await Assert.ThrowsAsync <NotSupportedException>(() => reqeust.AddFormFieldAsync("key", "value"));

            var body = await reqeust.Content.ReadAsStringAsync();

            Assert.Contains(get("name", "laojiu"), body);
            Assert.Contains(get("age", "18"), body);
            Assert.True(reqeust.Content.Headers.ContentType.MediaType == "multipart/form-data");
        }