随笔 - 89  文章 - 0  评论 - 2  阅读 - 11万 

请求参数:

 

 

封装参数:

1、创建model

1
2
3
4
5
ublic class stContent
        {
            public string SendName { get; set; }
            public string SendMobile { get; set; }
        }

2、封装参数并发送请求

  

1
2
3
4
5
6
7
8
public ActionResult GetExpress(string sendname,stirng phone)
{
           stContent sc = new stContent();
            sc.SendName = sendname;
            sc.SendMobile = phone;
           string res = JsonConvert.SerializeObject(sc);//转成json数据
           GetST(res);发送请求
}

  

1
2
3
4
5
6
7
8
9
10
11
12
public string GetST(string data)
{
    Dictionary<string, string> stu = new Dictionary<string, string>();
     stu.Add("content", data);
     stu.Add("data_digest", data_digest);
     stu.Add("api_name", api_name);
     stu.Add("from_appkey", st_from_appkey);
      stu.Add("from_code", st_from_code);
      stu.Add("to_appkey", st_to_appkey);
      stu.Add("to_code", st_to_code);
      string res = stPost(url, stu);
}

  3、post请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static string stPost(string url, Dictionary<string, string> dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

  

 

posted on   我的梦想是开个小店  阅读(566)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2020-12-02 MVC框架,JS获取Session中保存的值。
点击右上角即可分享
微信分享提示