我可以在不与服务器断开连接的情况下重用 HttpWebRequest 吗? [英] Can I reuse HttpWebRequest without disconnecting from the server?
问题描述
我正在尝试调试一个特定问题使用我的 ASP.NET 应用程序.客户端运行以下代码:
I'm trying to debug a specific issue with my ASP.NET application. The client runs the following code:
void uploadFile( string serverUrl, string filePath )
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.
Create( serverUrl );
CredentialCache cache = new CredentialCache();
cache.Add( new Uri( serverUrl ), "Basic", new NetworkCredential( "User", "pass" ) );
request.Credentials = cache;
request.Method = "POST";
request.ContentType = "application/octet-stream";
request.Timeout = 60000;
request.KeepAlive = true;
using( BinaryReader reader = new BinaryReader(
File.OpenRead( filePath ) ) ) {
request.ContentLength = reader.BaseStream.Length;
using( Stream stream = request.GetRequestStream() ) {
byte[] buffer = new byte[1024];
while( true ) {
int bytesRead = reader.Read( buffer, 0, buffer.Length );
if( bytesRead == 0 ) {
break;
}
stream.Write( buffer, 0, bytesRead );
}
}
}
HttpWebResponse result = (HttpWebResponse)request.GetResponse();
//handle result - not relevant
}
和 Write()
抛出异常无法将数据写入传输连接:已建立的连接被主机中的软件中止."文本.我使用了 System.Net tracking 并发现当我发送带有 Content-Length
设置的请求时出现问题.
and Write()
throws an exception with "Unable to write data to the transport connection: An established connection was aborted by the software in your host machine." text. I used System.Net tracing and found that something goes wrong when I send the request with Content-Length
set.
特别是如果我在上面的代码中省略了 using
语句中的所有内容,服务器会立即回复 WWW-Authenticate
然后客户端用 重新发布请求WWW-Authenticate
一切正常,除了文件未上传并且请求稍后失败.
Specifically if I omit everything that is inside using
statement in the code above the server promptly replies with WWW-Authenticate
and then the client reposts the request with WWW-Authenticate
and everything goes fine except the file in not uploaded and the request fails much later.
我想做以下事情:发送一个没有数据的请求,等待 WWW-Authenticate
,然后用 WWW-Authenticate
和数据重复它.所以我尝试修改上面的代码:首先设置所有参数,然后调用 GetResponse()
,然后发送,但是当我尝试设置 ContentLength
属性时,异常是抛出写入开始后无法设置此属性"文本.
I'd like to do the following: send an request without data, wait for WWW-Authenticate
, then repeat it with WWW-Authenticate
and data. So I tried to modify the code above: first set all the parameters, then call GetResponse()
, then do sending, but when I try to set ContentLength
property an exception is thrown with "This property cannot be set after writing has started" text.
所以 HttpWebRequest
似乎是不可重用的.
So HttpWebRequest
seems to be non-reusable.
如何在不关闭连接的情况下重用它来重新发送请求?
How do I reuse it for resending the request without closing the connection?
推荐答案
您不会重复使用请求 - 顾名思义,它是一个请求.但是,如果您对同一主机发出多个请求,.NET 将默认重用底层网络连接.
You don't reuse a request - as the name suggests, it's one request. However, if you issue multiple requests for the same host, .NET will reuse the underlying network connection by default.
请注意,您确实需要处理 request.GetResponse
返回的 WebResponse
- 否则底层基础架构不会知道您'实际上已经完成了,并且将无法重用连接.
Note that you do need to dispose of the WebResponse
returned by request.GetResponse
- otherwise the underlying infrastructure won't know that you're actually done with it, and won't be able to reuse the connection.
(顺便说一句,你为什么使用BinaryReader
?直接使用File.OpenRead
返回的流即可.)
(As an aside, why are you using BinaryReader
? Just use the stream returned by File.OpenRead
directly.)
这篇关于我可以在不与服务器断开连接的情况下重用 HttpWebRequest 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!