1 /// <summary> 2 /// Post方法 3 /// </summary> 4 /// <param name="url">webService的URL</param> 5 /// <param name="method">调用的方法</param> 6 /// <param name="reqBodys">参数组合</param> 7 /// <returns></returns> 8 public static string WebServiceHttpPost(string URL, string Method, List<ReqBody> ReqBodys) 9 { 10 string param = string.Empty; 11 switch (ReqBodys.Count) 12 { 13 case 0: 14 break; 15 case 1: 16 param = HttpUtility.UrlEncode(ReqBodys[0].Key) + "=" + HttpUtility.UrlEncode(ReqBodys[0].Value); 17 break; 18 default: 19 param = HttpUtility.UrlEncode(ReqBodys[0].Key) + "=" + HttpUtility.UrlEncode(ReqBodys[0].Value); 20 for (int i = 1; i < ReqBodys.Count; i++) 21 { 22 param += "&" + HttpUtility.UrlEncode(ReqBodys[i].Key) + "=" + HttpUtility.UrlEncode(ReqBodys[i].Value); 23 } 24 break; 25 } 26 byte[] byteArray = Encoding.UTF8.GetBytes(param); 27 28 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL + "/" + Method); 29 webRequest.Method = "POST"; 30 webRequest.ContentType = "application/x-www-form-urlencoded"; 31 webRequest.ContentLength = byteArray.Length; 32 webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length); //把参数数据写入请求数据的Stream对象 33 34 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); //获得响应 35 36 #region 这种方式读取到的是一个返回的结果字符串 37 using(XmlTextReader reader = new XmlTextReader(webResponse.GetResponseStream())) //获取响应流 38 { 39 reader.MoveToContent(); 40 return reader.ReadInnerXml(); 41 } 42 #endregion 43 44 #region 这种方式读取到的是一个Xml格式的字符串 45 //using(StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)); 46 //{ 47 // return sr.ReadToEnd(); 48 //} 49 #endregion 50 }