首页 > 代码库 > 使用HttpClient发送数据 到WebApi
使用HttpClient发送数据 到WebApi
发送和JSON数据
/=============================webAPI接受POST的JOSN数据=============================/ POST api/<controller>[HttpPost]public string Post([FromBody]string value){ var requestContent = value; var result = "[{\"name\":\"12\"}]"; return result;}// 发送方法public void SendStringToWebApi(){ #region 调用API发送字典数据 // (orders+vehicles+appkey+timestamp+version+appsecret) var timestamp = DateTime.Now.ToString("yyyyMMddHHmmssSSS"); var sign = DigitalSignature.MD5(JsonConvert.SerializeObject(listOrder) + appkey + timestamp + "1" + appsecret); var qid = timestamp + sign; var url = "http://www.routeapi.com/api/Order"; var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler)) { // 发送的JSON数据 var _TrasnTask = new TrasnTask() { Orders = listOrder, appkey = appkey, timestamp = timestamp, version = "1", sign = sign }; var postJsonContent = JsonConvert.SerializeObject(_TrasnTask); http.DefaultRequestHeaders.Accept.Clear(); // 指定提交的数据格式 // http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // HttpResponseMessage response; // var action = Uri.EscapeUriString(url); //Obj is passed into this, currently it is of type File // var content = new StringContent(postJsonContent, Encoding.UTF8, "application/json"); // response = http.PostAsync(action, content).Result; response = http.PostAsJsonAsync<string>(url, postJsonContent).Result; if (response.IsSuccessStatusCode) { var responseContent = response.Content; string responseString = responseContent.ReadAsStringAsync().Result; TempData["responseLocation"] = responseString; } }}
发送字段数据
public void SendDicToWebApi(){ //// (orders+vehicles+appkey+timestamp+version+appsecret) //var timestamp = DateTime.Now.ToString("yyyyMMddHHmmssSSS"); //var sign = DigitalSignature.MD5(JsonConvert.SerializeObject(listOrder) + appkey + timestamp + "1" + appsecret); //var qid = timestamp + sign; //var url = "http://www.routeapi.com/api/Order/PostToDic"; //var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //using (var http = new HttpClient(handler)) //{ // var formDic = new Dictionary<string, string>() // { // {"orders",JsonConvert.SerializeObject(listOrder)}, // {"vehicles","vehiclesVlaues"}, // {"appkey",appkey}, // {"timestamp",timestamp}, // {"version","1"}, // {"sign",sign} // }; // var content = new FormUrlEncodedContent(formDic); // // 发送字典集合 // var response = http.PostAsync(url, content).Result; // // 接受返回值 // var httpStatusCode = response.IsSuccessStatusCode; // var responseLocation = response.Headers.Location; // TempData["responseLocation"] = response.Content.ToString(); //}///// <summary> ///// 接受普通文本 ///// </summary> ///// <param name="value"></param> ///// <returns></returns> //[HttpPost] //// public string Post([FromBody]string value) //public string PostToDic(FormDataCollection value) //{ // var orders_1 = value.GetValues("orders"); // return "文本已经接受到了"; // ///// 接受地址栏的参数 // //var requestUri = Request.RequestUri.ParseQueryString(); // //var orders = requestUri["orders"]; // //var vehicles = requestUri["vehicles"]; // //var appkey = requestUri["appkey"]; // //var timestamp = requestUri["timestamp"]; // //var version = requestUri["version"]; // //var sign = requestUri["sign"]; //}}
使用HttpClient发送数据 到WebApi
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。