C# 发送 soap 请求(找了很多文章的最终解决方案)

private string QuerySoapWebService()
    {
        /*
        StringBuilder soapRequestData = new StringBuilder();
        soapRequestData.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        soapRequestData.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        soapRequestData.Append("<soap:Body>");
        soapRequestData.Append("<getOwners xmlns=\"http://WebXml.com.cn/\">");
        soapRequestData.Append("</getOwners>");
        soapRequestData.Append("</soap:Body>");
        soapRequestData.Append("</soap:Envelope>");*/

        /*    
        StringBuilder soapRequestData = new StringBuilder("");
        soapRequestData.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        soapRequestData.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        soapRequestData.Append("<soap:Body>");
        soapRequestData.Append(" <getContentNum xmlns=\"http://WebXml.com.cn/\">");
        soapRequestData.Append("<owner>" + 参数值1 + "</owner>");
        soapRequestData.Append("<contentid>" + 参数值2 + "</contentid>");
        soapRequestData.Append(" </getContentNum>");
        soapRequestData.Append(" </soap:Body>");
        soapRequestData.Append("</soap:Envelope>");*/

        //发起请求
        Uri uri = new Uri("输入url地址");
        WebRequest webRequest = WebRequest.Create(uri);
        webRequest.ContentType = "text/xml;charset=utf-8";
        webRequest.Method = "POST";
        webRequest.Headers.Add("SoapAction", "http://WebXml.com.cn/Request");    // 与soapXML中的一致,需要加/Request
        using (Stream requestStream = webRequest.GetRequestStream())
        {
            byte[] paramBytes = Encoding.UTF8.GetBytes(soapRequestData.ToString());
            requestStream.Write(paramBytes, 0, paramBytes.Length);
        }

        //响应
        //WebResponse webResponse = webRequest.GetResponse();
        HttpWebResponse hwRes = webRequest.GetResponse() as HttpWebResponse;
        //是否返回成功
        if (hwRes.StatusCode == System.Net.HttpStatusCode.OK)
        {
            XmlDocument readXmlRes = ReadXmlResponse(hwRes);
            
            XmlNodeList list = readXmlRes.SelectNodes("/Table/row");
        }
        else
        {
            
        }

        
        return "";
    }

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用C#的wsdlSOAP请求的示例代码: ```csharp using System; using System.IO; using System.Net; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string url = "http://www.example.com/Service.asmx"; string soapStr = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <Add xmlns=""http://www.example.com/""> <a>1</a> <b>2</b> </Add> </soap:Body> </soap:Envelope>"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "text/xml;charset=utf-8"; request.Headers.Add("SOAPAction", "http://www.example.com/Add"); request.ContentLength = soapStr.Length; using (Stream stream = request.GetRequestStream()) { byte[] bytes = Encoding.UTF8.GetBytes(soapStr); stream.Write(bytes, 0, bytes.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string result = reader.ReadToEnd(); Console.WriteLine(result); } } } } } ``` 上述代码中,我们首先定义了请求的URL和SOAP请求XML字符串。然后,我们创建了一个HttpWebRequest对象,并设置了请求的方法、内容类型、SOAPAction头和请求体的长度。接下来,我们将SOAP请求XML字符串转换为字节数组,并将其写入请求流中。最后,我们使用HttpWebResponse对象获取响应,并将响应流中的内容读取为字符串并输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值