快递查询
Posted lhl123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快递查询相关的知识,希望对你有一定的参考价值。
视图:
<script> function save() { var id = $("#Text1").val(); $("#id1").empty(); $.ajax({ url: "/gj/GetRoom", type: "post", data: { number: id }, success: function (data1) { var t = JSON.parse(data1); var obj = t.result; var obj1 = obj.list; $(obj1).each(function () { $("#id1").append("<p>" + "<span>" + this.time + "</span>" + "<span>" + this.status + "</span>" + "<span>" + this.type + "</span>" + "</p>"); }) } }) } </script>
控制器:
HttpClientHelp he = new HttpClientHelp("https://api.jisuapi.com/express/query"); [HttpPost] public JsonResult GetRoom(string number) { var result = he.Get("?appkey=859a1de85a002dd8&type=auto&number=" + number); return Json(result, JsonRequestBehavior.AllowGet); }
HttpClientHelp:
// 定义个私有字段 private HttpClient client; public HttpClientHelp(string ba) { client = new HttpClient(); client.BaseAddress = new Uri(ba); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); } // 添加 public string Get(string url) { // 初始化 string result = ""; //信息不是实例化 //信息要有方法 HttpResponseMessage msg = client.GetAsync(url).Result; if (msg.IsSuccessStatusCode) { result = msg.Content.ReadAsStringAsync().Result; } return result; } //添加 public string Post(string url, string data) { string result = ""; // 设置头信息 HttpContent content = new StringContent(data); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage msg = client.PostAsync(url, content).Result; if (msg.IsSuccessStatusCode) { result = msg.Content.ReadAsStringAsync().Result; } return result; } // 删除 public string Delete(string url) { // 初始化 string result = ""; //信息不是实例化 //信息要有方法 HttpResponseMessage msg = client.DeleteAsync(url).Result; if (msg.IsSuccessStatusCode) { result = msg.Content.ReadAsStringAsync().Result; } return result; } //修改 public string Put(string url, string data) { string result = ""; // 设置头信息 HttpContent content = new StringContent(data); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpResponseMessage msg = client.PutAsync(url, content).Result; if (msg.IsSuccessStatusCode) { result = msg.Content.ReadAsStringAsync().Result; } return result; }
以上是关于快递查询的主要内容,如果未能解决你的问题,请参考以下文章