C# Soap调WebService

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

 

posted on 2017-12-12 14:26  梦想总是这么遥远  阅读(2565)  评论(1编辑  收藏  举报