public class WebServiceHelper { /// /// Soap協議Post方法 /// /// WebService地址 /// 傳入Soap協議格式數據 /// public static string SendToReturn(Uri _ur... ...
public class WebServiceHelper { /// <summary> /// Soap協議Post方法 /// </summary> /// <param name="uri">WebService地址</param> /// <param name="str">傳入Soap協議格式數據</param> /// <returns></returns> public static string SendToReturn(Uri _uri, string _str) { string _returnstr = ""; //發起請求 WebRequest webRequest = WebRequest.Create(_uri); webRequest.ContentType = "text/xml; charset=utf-8"; webRequest.Method = "POST"; using (Stream requestStream = webRequest.GetRequestStream()) { byte[] paramBytes = Encoding.UTF8.GetBytes(_str); requestStream.Write(paramBytes, 0, paramBytes.Length); } //響應 try { WebResponse webResponse = webRequest.GetResponse(); using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { _returnstr = myStreamReader.ReadToEnd(); } } catch (WebException ex) { _returnstr = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); } return _returnstr; } }