• 快递查询


    视图:

    <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;
            }
  • 相关阅读:
    如何给 mongodb 设置密码
    Tips of Oracle
    Tips of Loadrunner
    Tips of 吞吐量
    压力逐渐加大 tps下降,响应时间没有变化,系统资源不饱和,为什么?【续】
    压力逐渐加大 tps下降,响应时间没有变化,系统资源不饱和,为什么?
    Tips:测试用例
    Tips:项目需求以及详细设计评审
    有关安全测试方面
    Tips of life
  • 原文地址:https://www.cnblogs.com/lhl123/p/10936355.html
Copyright © 2020-2023  润新知