HttpClient模拟客户端请求实例

 

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);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。