小编d0m*_*d0m的帖子

通过 HTTP 代理进行双向通信

我试图想出最合适的方法来通过 HTTP 代理建立双向套接字连接 - 假设这是一个 telnet 风格的协议。不幸的是,除了我无法预测的任何其他未来身份验证机制之外,我还需要支持 NTLM 身份验证(使用代理)以及基本身份验证和摘要身份验证。

如果只是基本和摘要,我会自己处理连接,但我真的不想陷入 NTLM 的泥潭。看看底层的 AuthenticationManager API,它看起来与 HttpWebRequest 非常相关,所以如果我使用套接字/tcpclient/任何东西,甚至编写新的 WebRequest 派生,我就无法利用该功能。

使用 HttpWebResponse 会产生一个无法写入的流,在检索响应流后使用 RequestStream 会产生并发 io 异常。

在尝试了我能想到的所有可能性之后,我想出了一些令人讨厌的代码,这些代码获取与 HttpWebRequest 关联的 NetworkStream,该 HttpWebRequest 允许两种方式通信:

  .....
  HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  Stream str = resp.GetResponseStream();

  System.Type type = str.GetType();
  PropertyInfo info = type.GetProperty("Connection", BindingFlags.NonPublic|BindingFlags.Instance| BindingFlags.Public);
  object obj = info.GetValue(str, null);
  type = obj.GetType();
  info = type.GetProperty("NetworkStream", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
  object obj2 = info.GetValue(obj, null);
  NetworkStream networkStream = obj2 as NetworkStream;
Run Code Online (Sandbox Code Playgroud)

我对此相当反感(它一开始就不能与 Mono 一起工作),所以我想知道是否有更好的方法使用公共 …

c# proxy http

5
推荐指数
1
解决办法
4082
查看次数

标签 统计

c# ×1

http ×1

proxy ×1