码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient模拟客户端请求实例

时间:2015-05-20 14:38:20      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

 

HttpClient Get请求:

/// <summary>
        /// Get请求模拟
        /// </summary>
        /// <param name="url">请求URL</param>
        public void HttpRequest(string url)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            Task<HttpResponseMessage> response = client.GetAsync(url);
            HttpResponseMessage result = response.Result;
            this.queryResultPane.Text = result.Content.ReadAsStringAsync().Result;
        }

 

 

HttpClient Post请求:

var aaa = new
            {
                Code = "139357a4-b38c-483e-a530-8f044a3dfe1c",
                WechatOpenID = "33265198",
                Area = "ShangHai",
                Birthday = DateTime.Parse("1986-8-18"),
                Gender = "male",
                Level = 80,
                Name = "Lynn",
                PhoneNumber = "13129623023",
                EmailType = "qq",
                IsHaveParent = "no",
                Province = "河南省",
                TheCity = "郑州市",
                //Age = 100,
                Labels = new List<LabelEntity>() { new LabelEntity() { Label = "uuuu" }, new LabelEntity() { Label = "yyyy" } }
            };

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //简单类型的参数,webApi方法里可以不定义参数,通过HttpContext.Current.Request.QueryString["Mobile"]来获取
            Task<HttpResponseMessage> response =
                //client.PostAsJsonAsync<ThreeMMember>(string.Format(U8688 + "MemberPoint/UpdateMemberInfo"), member);
                client.PostAsJsonAsync(string.Format(U8688 + "MemberPoint/SaveMember"), aaa);
            HttpResponseMessage result = response.Result;
            this.Response.Output.Write(result.Content.ReadAsStringAsync().Result);

HttpClient模拟客户端请求实例

标签:

原文地址:http://www.cnblogs.com/itjeff/p/4516974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!