我正在开发一个.Net客户端来从oData服务中检索数据。我的客户端使用System.Data.Services.Client.DataServiceContext。
我想看到发送到服务器的实际oData请求。服务器使用SSL连接,对服务器的请求不容易被嗅探。我尝试用Burp执行此操作。我添加了一个SendingRequest EventHandler并设置了Request.Proxy属性。但是这给了一个例外:
基础连接已关闭:无法为SSL / TLS安全通道建立信任关系。
Wireshark只显示加密数据。
或者,我可以联系oData提供商获取服务器日志,但这需要一些时间。我正在寻找一个瞬间的痕迹。
以下是使用Microsoft Northwind oData服务的示例客户端。
Customers()
; Customers()
; 方法:
public class oDataClient
{
public void Customers()
{
{
var context = new Northwind.NorthwindEntities(new System.Uri("http://services.odata.org/V3/Northwind/Northwind.svc/"));
var customers = from c in context.Customers
where c.CompanyName.StartsWith("A")
select c;
int count = customers.Count();
}
}
}
测试:
[TestMethod()]
public void CustomersTest()
{
oDataClient target = new oDataClient();
target.Customers();
}
答案 0 :(得分:1)
在写下这个问题并创建示例客户端时,我突然发现提交给服务器的实际请求并未隐藏在.Net库的深处,但可以很容易地被监视!
如果您运行测试并逐步执行代码,则在查询上下文后,对象客户只需显示该URL。在这种情况下:
http://services.odata.org/V3/Northwind/Northwind.svc/Customers()?$filter=startswith(CompanyName,'A')
实际上是底层基类DataServiceQuery实例的RequestUri属性。
所以,我希望这个答案对于像我这样忽视这一点的人来说非常有用。
答案 1 :(得分:1)
您可以使用SendingRequest2,ReceivingResponse事件在客户端跟踪外出的OData请求。有关详细信息,请参阅以下示例。
using Microsoft.OData.Client;
using Simple.OData.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ODataClientApp
{
class Program
{
private static string fileName = "TraceLog.log";
private static Default.Container container;
static void Main(string[] args)
{
try
{
var uri = "http://localhost:32097/odata";
container = new Default.Container(new Uri(uri));
container.SendingRequest2 += Container_SendingRequest2;
container.ReceivingResponse += Container_ReceivingResponse;
var moviles = container.Movies.Execute().ToList();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
private static void Container_SendingRequest2(object sender, Microsoft.OData.Client.SendingRequest2EventArgs e)
{
var request = e.RequestMessage as HttpWebRequestMessage;
var x = container;
var url = request.Url.AbsoluteUri;
var method = request.Method;
var authenticationLevel = request.HttpWebRequest.AuthenticationLevel;
var impersonationLevel = request.HttpWebRequest.ImpersonationLevel.ToString();
var headers = request.Headers;
var sb = new StringBuilder();
sb.AppendLine(DateTime.Now.ToString() + "------------------------------SendingRequest2 Begin---------------------------");
sb.AppendLine("Url:" + url);
sb.AppendLine("Method:" + method);
sb.AppendLine("Authentication Level:" + authenticationLevel);
sb.AppendLine("Impersonation Level:" + impersonationLevel);
sb.AppendLine();
sb.AppendLine("Header Info:-");
foreach (var header in headers)
{
sb.AppendFormat("{0}:{1}", header.Key, header.Value);
sb.AppendLine();
}
sb.AppendLine(DateTime.Now.ToString() + "------------------------------SendingRequest2 End-----------------------------");
File.AppendAllText(fileName, sb.ToString());
}
private static void Container_ReceivingResponse(object sender, Microsoft.OData.Client.ReceivingResponseEventArgs e)
{
var response = e.ResponseMessage as HttpWebResponseMessage;
var statusCode = response.StatusCode.ToString();
var headers = response.Headers;
var sb = new StringBuilder();
sb.AppendLine(DateTime.Now.ToString() + "------------------------------ReceivingResponse Begin-------------------------");
sb.AppendLine("Status Code:" + statusCode);
sb.AppendLine();
sb.AppendLine("Header Info:-");
foreach (var header in headers)
{
sb.AppendFormat("{0}:{1}", header.Key, header.Value);
sb.AppendLine();
}
sb.AppendLine(DateTime.Now.ToString() + "------------------------------ReceivingResponse End---------------------------");
File.AppendAllText(fileName, sb.ToString());
}
}
}