C#的wsdlSOAP请求
时间: 2023-12-08 13:37:57 浏览: 76
wsdl2java源码-camel-cxf-soap-client:使用2向SSL(客户端身份验证)演示客户端SOAP请求/回复
以下是使用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对象获取响应,并将响应流中的内容读取为字符串并输出。
阅读全文