HttpWebRequest CONNECT方法 - 连接后写入数据 [英] HttpWebRequest CONNECT method - writing data after connection

查看:270
本文介绍了HttpWebRequest CONNECT方法 - 连接后写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用HttpWebRequest通过代理成功连接到远程服务器,并将Method属性设置为"CONNECT"。但是,只有在连接后才能从流中读取数据,这使连接变得无用,因为我无法与作为CONNECT目标的服务器进行交互。

示例代码:


I have successfully connected to a remote server through a proxy using HttpWebRequest with the Method property set to "CONNECT". However it only seems possible to read data from the stream once connected, making the connection useless since I can't interact with the server that was the target of the CONNECT.

Sample code:


HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://remote.server.name:999");
request.Method = WebRequestMethods.Http.Connect;
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
byte[] input = new byte[10240];
stream.Read(input, 0, input.Length);
byte[] output = GetData();
// I would like to be able to do the following, but a NotSupportedException is thrown (Message: "The stream does not support writing.")
stream.Write(output, 0, output.Length);

推荐答案

根据HTTP RFC,您不应该在代码中执行CONNECT操作:< 9.9连接本规范保留方法名称CONNECT,用于可以动态切换为隧道的代理(例如SSL
隧道[ 44])。

如果您要去的服务器需要SSL(https),HttpWebRequest将为您发送CONNECT请求

如果您打算发送数据到服务器,您需要将动词更改为POST并写入RequestStream,而不是像 http://msdn.microsoft.com/en-us/library/d4cek6cc(VS.71).aspx

我希望这些信息有助于您修改代码

谢谢
You should not perform the CONNECT operation in your code, according to the HTTP RFC:

9.9 CONNECT

   This specification reserves the method name CONNECT for use with a
   proxy that can dynamically switch to being a tunnel (e.g. SSL
   tunneling [44]).

HttpWebRequest will send the CONNECT request for you in the case you're going to a server requiring SSL (https)

If your intention is to send data to the server, you need to change the verb to a POST and write to the RequestStream, not the ResponseStream as in the code samples at http://msdn.microsoft.com/en-us/library/d4cek6cc(VS.71).aspx

I hope this information helps you in modifying your code

Thanks


这篇关于HttpWebRequest CONNECT方法 - 连接后写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆