gpt4 book ai didi

.net - 如何通过 SSL 跟踪来自 .Net DataServiceContext 的 oData 请求?

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:20 25 4
gpt4 key购买 nike

我正在开发一个 .Net 客户端来从 oData 服务中检索数据。我的客户使用 System.Data.Services.Client.DataServiceContext。

我希望看到发送到服务器的实际 oData 请求。服务器使用 SSL 连接,并且无法轻易嗅探对服务器的请求。我试着用 Burp 来做到这一点.我添加了一个 SendingRequest EventHandler 并设置了 Request.Proxy 属性。但这给出了一个异常(exception):

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

Wireshark仅显示加密数据。

或者,我可以联系 oData 提供商获取服务器日志,但这需要一些时间。我正在寻找瞬时痕迹。


这是一个示例客户端,它使用 Microsoft Northwind oData 服务。

  1. 创建类库;
  2. 添加对 Northwind 的服务引用;
  3. 创建一个类oDataClient;
  4. 添加一个方法Customers();
  5. 创建一个测试项目;
  6. 在其中为 Customers() 创建一个单元测试;
  7. 运行测试。

方法:

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();
}

最佳答案

您可以使用 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());
}
}
}

关于.net - 如何通过 SSL 跟踪来自 .Net DataServiceContext 的 oData 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com