发送和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"]; //} }
时间: 2024-10-05 08:54:31