码迷,mamicode.com
首页 > 其他好文 > 详细

快递查询

时间:2019-05-28 12:31:40      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:The   class   rip   pen   each   type   视图   script   私有字段   

视图:

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

 

快递查询

标签:The   class   rip   pen   each   type   视图   script   私有字段   

原文地址:https://www.cnblogs.com/lhl123/p/10936355.html

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