• 发送带参数post请求


    请求参数:

    封装参数:

    1、创建model

    ublic class stContent
            {
                public string SendName { get; set; }
                public string SendMobile { get; set; }
            }
    

    2、封装参数并发送请求

      

    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);发送请求
    }
    

      

    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请求

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

      

  • 相关阅读:
    命令行构建Unity项目
    Android AIDL实例解析
    Android aidl Binder框架浅析
    Uri详解之——Uri结构与代码提取
    Glide使用详解(一)
    ImageView的scaleType详解
    关于Android导入开源项目:Error:Unable to load class 'org.gradle.api.publication.maven.internal.DefaultMavenFa
    15 个 Android 通用流行框架大全
    动画 节拍器
    Android background tint颜色渲染
  • 原文地址:https://www.cnblogs.com/syeacfpl/p/15632614.html
Copyright © 2020-2023  润新知