HttpClient 调用WebService

        public static string RequestWebService(string url, Dictionary<string, string> dic)
        {
            try
            {
                HttpClient client = new HttpClient();
                var jsonCon = string.Join("&", dic.Select(d => $"{d.Key}={HttpUtility.UrlEncode(d.Value)}"));
                HttpContent httpContent = new StringContent(jsonCon, Encoding.UTF8, "application/x-www-form-urlencoded");
                var Res = client.PostAsync(url, httpContent).Result;
                var stream = Res.Content.ReadAsStreamAsync().Result;
                XmlTextReader Reader = new XmlTextReader(stream);
                Reader.MoveToContent();
                return Reader.ReadInnerXml();
            }
            catch (Exception e)
            {
                return "";
            }
        }

 

调用方法

var url = "http://。。/Service.asmx/方法名称";
 var dic=new Dictionary<string,string>{{"参数名称",参数值}};
string result = Http.RequestWebService(url, dic);

 

posted @ 2020-05-15 16:19  simadi  阅读(752)  评论(0编辑  收藏  举报