通过HttpWebRequest将{XML>发布到WebService

时间:2015-10-07 16:15:30

标签: c# xml asp.net-mvc-4 soap

我已经创建了一个C#SOAP WebService,但是希望能够通过HttpWebRequest发布XML文档而不向我的项目添加服务引用,这可能吗?

WebService.asmx

public class WebApplication1 : WebService
{
    [WebMethod(Description = "Test")]
    public string Test(XmlDocument xmlDocument)
    {
        return "Test";
    }
}

输出

POST /WebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Test"

<?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>
    <Test xmlns="http://tempuri.org/">
      <xmlDocument>xml</xmlDocument>
    </Test>
  </soap:Body>
</soap:Envelope>

XML

<?xml version="1.0" encoding="utf-16"?>
<Affiliate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" leadinnumber="ABCDABCD" xmlns:xsd="http://www.w3.org/2001/XMLSchema" mediaid="30000">
  <Details>
    <Amount>5000</Amount>
    <FirstName>Delivery</FirstName>
    <Surname>Testing</Surname>
    <Tel>02034056978</Tel>
    <Email>adfsdfsdfsd@live.co.uk</Email>
  </Details>
</Affiliate>

我相信你必须创建一个SoapEnvelope,我有以下代码:

    using (var writer = new XmlTextWriter(file, Encoding.UTF8) { Formatting = Formatting.Indented })
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
        writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        writer.WriteStartElement("soap", "Body", null);         
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
    }

有点不确定如何嵌入XML。

0 个答案:

没有答案
相关问题