C# http webservice wsdl soap xml 通过C#调用webservice基于sop协议的接口

一:注意事项

接口地址示例:https://********/DataExchangeForCBS?wsdl
http://
/soap/JHIPLIB.SoAP.BS.StreamService.cls?CfgItem=JH2006绩效系统信息日

1:基于soap协议的接口,传输的数据为xml格式,在对接其他系统接口的时候,接口文档可能会给出的参数格式为json,这时候需要注意,可能说的是xml包json的格式  例如
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://query.powerchinasupplier.epoint.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <quer:SendCbsPjInfo>
         <!--Optional:-->
         <quer:sendInfo>{
	"PingshenDate": "2022-04-17 00:00:00",
	"PingshenDateTo": "2022-04-18 15:11:00",
	"ValidateData": "djsz"
       }</quer:sendInfo>
      </quer:SendCbsPjInfo>
   </soapenv:Body>
</soapenv:Envelope>
    <quer:sendInfo>内部包的是实际的参数,但是必须有外部的xml包起来

在使用C#调用用http请求方式调用接口的时候,需要注意 contenttype = "text/xml;charset=utf-8";
还有就是header头中,需要加入 header.Add("SOAPAction", "http://*********/SendCbsPjInfo");
这里的SOAPAction代表的应该是对面接口的具体地址 SendCbsPjInfo代表的是提供的方法名
如果不知道SOAPAction地址是什么的话,可以通过SoapUI软件查看
SoapUI使用步骤:
1.在这里插入图片描述
2.在这里插入图片描述3.在这里插入图片描述
4.在这里插入图片描述这里就可以看到SOAPAction的地址了,拿过来用就行了

附上C#代码:

   string url = "接口地址";//如果是?wsdl结尾的  需要把?wsdl删除掉
    string contenttype = "text/xml;charset=utf-8";
    Hashtable header = new Hashtable();
    header.Add("SOAPAction", "上面获取到的SOAPAction地址");
    string data = LoadXmls(@"{'PingshenDate': '2022-04-17 00:00:00','PingshenDateTo': '2022-04-18 15:11:00','ValidateData': 'djsz'}");//这里是传递参数,拼接正规的完整xml入参
    string filexml = Http(url, "post", contenttype, header, data);
    XmlDocument doc1 = new XmlDocument();
    doc1.LoadXml(filexml);   //将获取到的结果加载为xml对象
    string model = doc1.InnerText;//直接读取返回值  也就是cdata内部的内容
       /// <summary>
        /// 接口入参拼接
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string LoadXmls(string data)
        {
            XmlDocument doc1 = new XmlDocument();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:quer='http://query.powerchinasupplier.epoint.com/'>
   <soapenv:Header/>
   <soapenv:Body>
      <quer:SendCbsPjInfo>
         <!--Optional:-->
         <quer:sendInfo>");
            stringBuilder.Append(data);
            stringBuilder.Append(@"</quer:sendInfo>
      </quer:SendCbsPjInfo>
   </soapenv:Body>
</soapenv:Envelope> ");
            return stringBuilder.ToString();
        }
   /// <summary>
    /// 调用soap协议接口   webservice    wsdl   
    /// </summary>
    /// <param name="url"></param>
    /// <param name="method"></param>
    /// <param name="contenttype"></param>
    /// <param name="header"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    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;
    }
  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值