C#对接webservice Soapxml报文post请求

1.发起请求方法

StringBuilder soap = new StringBuilder();
            soap.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.controller.eip.wisdytech.com/\">");
            soap.Append("<soapenv:Header>");
            soap.Append("<web:LoginSoapHeader>");
            soap.Append("<EquUserID>0725</EquUserID>");
            soap.Append("<EquPassword>0725</EquPassword>");
            soap.Append("<OperatorUserID>0725</OperatorUserID>");
            soap.Append("<OperatorPassword>0725</OperatorPassword>");
            soap.Append("</web:LoginSoapHeader>");
            soap.Append("</soapenv:Header>");
            soap.Append("<soapenv:Body>");
            soap.Append("<web:OperatorLogin>");
            soap.Append("<model>");
            soap.Append("<EquipmentCode>SR4ML1DXOCV01</EquipmentCode>");
            soap.Append("<ResourceCode>SR4ML1DXOCV01</ResourceCode>");
            soap.Append("<LocalTime>2022-06-25T10:11:00</LocalTime>");
            soap.Append("<OperatorUserID>0725</OperatorUserID>");
            soap.Append("<OperatorPassword>0725</OperatorPassword>");
            soap.Append("</model>");
            soap.Append("</web:OperatorLogin>");
            soap.Append("</soapenv:Body>");
            soap.Append("</soapenv:Envelope>");

     //发起请求
     var response = Http(url, "post", "text/xml; charset=utf-8", null, soap.ToString());

2.请求封装方法

 public static string Http(string url, string method = "post", string contenttype = "text/xml;charset=utf-8", Hashtable header = null, string data = null)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = string.IsNullOrEmpty(method) ? "post" : method;
            request.ContentType = string.IsNullOrEmpty(contenttype) ? "text/xml;charset=utf-8" : contenttype;
            if (header != null)
            {
                foreach (var i in header.Keys)
                {
                    request.Headers.Add(i.ToString(), header[i].ToString());
                }
            }
            if (!string.IsNullOrEmpty(data))
            {
                Stream RequestStream = request.GetRequestStream();
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                RequestStream.Write(bytes, 0, bytes.Length);
                RequestStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream ResponseStream = response.GetResponseStream();
            StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
            string re = StreamReader.ReadToEnd();
            StreamReader.Close();
            ResponseStream.Close();
            return re;
        }

3.xml报文转成json

//xml转json 
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
string json = JsonConvert.SerializeXmlNode(doc);
string Code = Convert.ToString(DiGuiGteJSON(json, "Code"));  //获取接送嵌套的值
string Msg = Convert.ToString(DiGuiGteJSON(json, "Msg"));

public static object DiGuiGteJSON(string json, string key)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            Dictionary<string, object> js = javaScriptSerializer.DeserializeObject(json) as Dictionary<string, object>;
            object value = null;
            foreach (KeyValuePair<string, object> item in js)
            {
                if (item.Key == key)
                {
                    value = item.Value;
                    break;
                }
                else if (item.Value is Dictionary<string, object>)
                {
                    object se = di(item.Value as Dictionary<string, object>, key);
                    if (se != null)
                    {
                        value = se;
                        break;
                    }
                }
                else if (item.Value is object[])
                {
                    object[] se = item.Value as object[];
                    if (se.Length > 0)
                    {
                        foreach (object o in se)
                        {
                            if (o is Dictionary<string, object>)
                            {
                                object ses = di(item.Value as Dictionary<string, object>, key);
                                if (ses != null)
                                {
                                    value = ses;
                                    break;
                                }
                            }
                        }
                    }
                }


            }
            return value;

        }


        private static object di(Dictionary<string, object> dic, string key)
        {
            object vale = null;
            foreach (KeyValuePair<string, object> item in dic)
            {
                if (item.Key == key)
                {
                    vale = item.Value;
                    break;
                }
                else if (item.Value is Dictionary<string, object>)
                {
                    object fan = di(item.Value as Dictionary<string, object>, key);
                    if (fan != null)
                    {
                        vale = fan;
                        break;
                    }
                }
                else if (item.Value is object[])
                {
                    object[] se = item.Value as object[];
                    if (se.Length > 0)
                    {
                        foreach (object o in se)
                        {
                            if (o is Dictionary<string, object>)
                            {
                                object ses = di(o as Dictionary<string, object>, key);
                                if (ses != null)
                                {
                                    vale = ses;
                                    break;
                                }
                            }
                        }
                    }
                }

            }
            return vale;
        }

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值