我正在調用外部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