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

标签 .net ssl odata trace

我正在开发一个 .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/

相关文章:

iphone - 我在尝试通过我的 iPhone 应用程序与服务器通信时遇到 SSL 错误

node.js - 如何使用 .pfx 类型的证书修复 nodejs 中缺少的中间/链证书

internet-explorer - 即 : Choosing a digital certificate from a blank list every first visit to my website

c# - 尽管 Comparer.Equals 返回 true,但字典不包含键

c# - 为什么我需要将空合并运算符放在方括号中?

c# - 如何确定我的程序当前占用了多少内存

c# - 有没有办法修改 IQueryable 中为 OData 请求返回的数据?

.net-core - .net 核心如何将内容范围添加到标题

azure - 如何使用 OData 筛选器对逻辑应用中的唯一姓氏筛选 Azure 存储表?

c# - 如何将 PolicyHttpMessageHandler 用作 "standalone"?