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