我可以重复使用的HttpWebRequest不从服务器断开?不从、重复使用、服务器、HttpWebRequest

2023-09-02 01:50:34 作者:爷、独闯疯人院

我试图调试a具体的问题,我的ASP.NET应用程序。客户端运行以下code:

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
 }

写()抛出一个异常,无法将数据写入传输连接:一个已建立的连接被中止通过在主机中的软件文本。我用 System.Net跟踪,发现出错时,我发送带有内容长度集合的请求。

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.

具体而言,如果我省略了一切,这是里面的使用语句在code服务器上面及时与 WWW验证回复键,然后在客户端转播与请求 WWW验证键,一切都正常,只是在没有上传的文件,并要求晚得多失败。

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验证,然后用 WWW身份验证重蹈覆辙和数据。所以,我试图修改code以上:首先设置所有的参​​数,然后调用的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 似乎是不可重用。

我如何重新使用它重新发送请求而不关闭连接?

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.

请注意,您的不的需要处置的 WebResponse类 request.GetResponse - 否则,底层基础架构将不知道你实际上是用它做,并且将无法再使用该连接

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.)