我需要将XML作为字符串发送到我使用ASP.Net开发的Web服务。
要做到这一点,我编码我需要作为webserivce的参数(字符串)发送的xml,然后我在webservice业务逻辑中解码它。
Webservice例如:
[WebMethod]
public void WebService_DoWork ( string xml )
{
try
{
MyBo bo = new MyBo ( HttpUtility.HtmlDecode ( xml ) ) ;
bo.DoWork() ;
}
catch ( Exception err )
{
//Logging Here
}
}
C#客户端例如:
public void CallMethod ()
{
string encodedXMLPayload = @"<?xml version=".." ;
WebService client = new WebService () ;
client.Url = @"http:\\myTestServer:port\WebService.asmx" ;
client.WebService_DoWork ( encodedXMLPayload ) ;
}
这里是完整的encodedXMLPayload数据:
<?xml version="1.0" encoding="utf-8"?>
<RawTransactionsByAllocCode>
<ReportData>
<Costcenter>849001</Costcenter>
<TransactionID>261246195</TransactionID>
<TimeStamp>28/01/2016 10:15:39 PM</TimeStamp>
<TransType>Take</TransType>
<ProductName>Test Product</ProductName>
<SuppliersPartNo>52796843</SuppliersPartNo>
<Quantity>5</Quantity>
<MaterialNumber>17096239</MaterialNumber>
<DeliveryNoteNumber></DeliveryNoteNumber>
<PurchaseOrderReference />
</ReportData>
</RawTransactionsByAllocCode>
如果我尝试从我的C#客户端调用我的web服务(webmethod WebService_DoWork),我会收到我发送的确切有效负载(仍然编码),一切都正确。
如果我尝试使用Postman,SoapUI或其他客户端,我会获得一个有效载荷,当我在webmethod中收到它时,它就被解码了(并且双重解码可能会有问题)。
以下是我与SoapUI或Postman一起使用的soap请求示例:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:WebService_DoWork>
<tem:xml>
<?xml version="1.0" encoding="utf-8"?>
<RawTransactionsByAllocCode>
<ReportData>
<Costcenter>849001</Costcenter>
<TransactionID>261246195</TransactionID>
<TimeStamp>28/01/2016 10:15:39 PM</TimeStamp>
<TransType>Take</TransType>
<ProductName>Test Product</ProductName>
<SuppliersPartNo>52796843</SuppliersPartNo>
<Quantity>5</Quantity>
<MaterialNumber>17096239</MaterialNumber>
<DeliveryNoteNumber></DeliveryNoteNumber>
<PurchaseOrderReference />
</ReportData>
</RawTransactionsByAllocCode>
</tem:xml>
</tem:WebService_DoWork>
</soapenv:Body>
</soapenv:Envelope>
所以我想知道为什么这个奇怪的行为,可能是C#客户端再次对它进行编码(引擎盖下),因为ASP.Net会自动解码它?
答案 0 :(得分:-1)
XML标记不会被编码。只需要对XML标记之间的文本进行编码。使用实际大于和小于符号<
>
和引号"
。在编写XML标记时。您只需要像这样编码XML标签的“值”。
<?xml version="1.0" encoding="utf-8"?>
<RootElement>
<ChildElement>
I need to encode quotes "e; and less than/greater than symbols < > because this is not in a tag
</ChildElement>
<ChildElement>
<DeeperChildElement some-attribute="these quotes aren't encoded">
Special Characters here need to be encoded
</DeeperChildElement>
</ChildElement>
</RootElement>