请求参数:
封装参数:
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; }