因为业务需求 与 第三方系统 做交互,第三方系统接口需要模拟 Post 提交 传递参数。以下是 测试案例。
HttpWebRequest request = null; request = WebRequest.Create("http://**********") as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; //Encoding encoding = Encoding.GetEncoding("gb2312"); string data_digest = ""; string logistics_interface ""; StringBuilder PostStr = new StringBuilder(); MD5 md5 = new MD5CryptoServiceProvider(); byte[] data = Encoding.UTF8.GetBytes(data_digest); //byte[] data =HttpUtility.UrlEncodeToBytes(data_digest,Encoding.UTF8); byte[] md5data = md5.ComputeHash(data); md5.Clear(); string str = ""; for (int i = 0; i < md5data.Length ; i++) { str += md5data[i].ToString("x").PadLeft(2, '0'); } byte[] bc = Encoding.UTF8.GetBytes(str); //byte[] bc = HttpUtility.UrlEncodeToBytes(str, Encoding.UTF8); //转成 Base64 形式的 System.String str = Convert.ToBase64String(bc); PostStr.Append("data_digest=" + str + "&logistics_interface=" + HttpUtility.UrlEncode(logistics_interface, Encoding.UTF8)); //byte[] SomeBytes = HttpUtility.UrlEncodeToBytes(PostStr.ToString(), Encoding.UTF8); byte[] SomeBytes = Encoding.UTF8.GetBytes(PostStr.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(SomeBytes, 0, SomeBytes.Length); } HttpWebResponse res1 = request.GetResponse() as HttpWebResponse; StreamReader sr = new StreamReader(res1.GetResponseStream(), Encoding.UTF8); string strResult = sr.ReadToEnd(); sr.Close();