我正在调用外部HTTPS webservice。在将SOAP消息发送到.NET中的WebService之前获取SOAP消息
为了检查错误,所有者需要我发送的SOAP请求。
我有一个Web引用,并在2008年产生VS生成的代理类......
有没有办法看到SOAP消息只是在发送之前?
我想在一些.NET代码...因为我试过的嗅探器没有“看到”web服务调用不知道为什么。
我正在调用外部HTTPS webservice。在将SOAP消息发送到.NET中的WebService之前获取SOAP消息
为了检查错误,所有者需要我发送的SOAP请求。
我有一个Web引用,并在2008年产生VS生成的代理类......
有没有办法看到SOAP消息只是在发送之前?
我想在一些.NET代码...因为我试过的嗅探器没有“看到”web服务调用不知道为什么。
如果你 在更受限制的环境中工作,并没有使用像Fiddler这样的应用程序的奢侈品,您可以执行以下操作:
using (var reader = new System.IO.StreamReader(Request.InputStream)) { result = reader.ReadToEnd(); }
这不是一个理想的或漂亮的解决方案,但是如果您在适度受限的环境中工作,就可以完成工作。
你可以简单的序列化请求对象,subtmit之前,像这样:
var sreq = new SomeSoapRequest();
// ... fill in here ...
var serxml = new System.Xml.Serialization.XmlSerializer(sreq.GetType());
var ms = new MemoryStream();
serxml.Serialize(ms, sreq);
string xml = Encoding.UTF8.GetString(ms.ToArray());
// in xml string you have SOAP request
您可以使用IClientMEssageInspector和IEndpointBehavior到fullfill这一点。我发现用这种方式可以捕捉可能的提琴手一个确切的SOAP请求:
在同一个项目中创建一个这样的类:
public class ClientMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
{
#region IClientMessageInspector Members
public string LastRequestXml { get; private set; }
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
string requestHeaderName = request.Headers.Action.Replace("urn:#",string.Empty);
LastRequestXml = request.ToString();
string serializedRequestFile = string.Format(requestHeaderName + "_request_{0}.xml", DateTime.Now.ToString("yyyyMMddHHmmss"));
string exportedFolder = ConfigurationManager.AppSettings["SubmittedRequestXmLocation"];
printSoapRequest(request, exportedFolder, serializedRequestFile);
return request;
}
public void printSoapRequest(System.ServiceModel.Channels.Message request, string exportedFolder, string fileName)
{
if (exportedFolder.Equals(string.Empty))
return;
if (!Directory.Exists(exportedFolder))
{
Directory.CreateDirectory(exportedFolder);
}
string exportedFile = string.Format("{0}\\{1}", exportedFolder, fileName);
if (File.Exists(exportedFile))
{
File.Delete(exportedFile);
}
string strRequestXML = request.ToString();
XDocument xDoc = XDocument.Parse(strRequestXML);
XmlWriter xw = XmlWriter.Create(exportedFile);
xDoc.Save(xw);
xw.Flush();
xw.Close();
LogOutput("Request file exported: " + exportedFile);
}
}
public class CustomInspectorBehavior : IEndpointBehavior
{
private readonly ClientMessageInspector clientMessageInspector = new ClientMessageInspector();
public string LastRequestXml
{
get { return clientMessageInspector.LastRequestXml; }
}
public string LastResponseXml
{
get { return clientMessageInspector.LastRequestXml; }
}
public void AddBindingParameters(
ServiceEndpoint endpoint,
System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(clientMessageInspector);
}
}
然后,你可以把它像下面这样:
ProxyClass _class = new ProxyClass();
var requestInterceptor = new CustomInspectorBehavior();
_client.Endpoint.Behaviors.Add(requestInterceptor);
当你调用服务方法时,它会自动执行拦截器并打印输出。使用这种方式,您还可以在发送到服务器之前操作soap消息!
我使用*** Web服务参考***,***不是WCF参考*** – Kiquenet 2017-10-31 13:16:25
第二个链接有多个选项。接受的答案不一定是最好的(所有这三种解决方案都是好的)。仅供参考,我最喜欢Fiddler选项。 – Nullius 2013-07-08 08:39:49