Bạn cũng có thể đọc các contents of the Request.InputStream.
Cách này hữu ích hơn như đối với các trường hợp khi bạn muốn thực hiện xác thực hoặc các hành động khác trong WebMethod tùy thuộc vào nội dung của đầu vào.
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace SoapRequestEcho
{
[WebService(
Namespace = "http://soap.request.echo.com/",
Name = "SoapRequestEcho")]
public class EchoWebService : WebService
{
[WebMethod(Description = "Echo Soap Request")]
public XmlDocument EchoSoapRequest(int input)
{
// Initialize soap request XML
XmlDocument xmlSoapRequest = new XmlDocument();
// Get raw request body
Stream receiveStream = HttpContext.Current.Request.InputStream
// Move to begining of input stream and read
receiveStream.Position = 0;
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
// Load into XML document
xmlSoapRequest.Load(readStream);
}
// Return
return xmlSoapRequest;
}
}
}
LƯU Ý: cập nhật để phản ánh Johns bình luận dưới đây.
Nguồn
2011-05-21 02:01:22
lẽ hiển nhiên, nhưng bạn có thể cần phải đăng ký IHttpModule trong web.config –