Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HttpWebRequest.GetResponse时KeepAliveException_C#_Httpwebrequest_Couchdb_Getresponse - Fatal编程技术网

C# 使用HttpWebRequest.GetResponse时KeepAliveException

C# 使用HttpWebRequest.GetResponse时KeepAliveException,c#,httpwebrequest,couchdb,getresponse,C#,Httpwebrequest,Couchdb,Getresponse,我正在尝试使用HttpWebRequest向CouchDB发布附件。但是,当我尝试“response=(HttpWebResponse)httpWebRequest.GetResponse();”时,我收到一个WebException,其中包含消息“底层连接已关闭:服务器已关闭一个预期保持活动状态的连接” 我发现一些文章指出,将keepalive设置为false,将httpversion设置为1.0可以解决这种情况。我发现它不会产生完全相同的错误,而且我不想采用这种方法,因为我不想使用1.0版本

我正在尝试使用HttpWebRequest向CouchDB发布附件。但是,当我尝试“response=(HttpWebResponse)httpWebRequest.GetResponse();”时,我收到一个WebException,其中包含消息“底层连接已关闭:服务器已关闭一个预期保持活动状态的连接”

我发现一些文章指出,将keepalive设置为false,将httpversion设置为1.0可以解决这种情况。我发现它不会产生完全相同的错误,而且我不想采用这种方法,因为我不想使用1.0版本,因为它处理连接的方式

欢迎提出任何建议或想法。我要把它们都试一下,直到有一个起作用为止

public ServerResponse PostAttachment(Server server, Database db, Attachment attachment)
    {
        Stream dataStream;
        HttpWebResponse response = null;
        StreamReader sr = null;
        byte[] buffer;
        string json;
        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
        string headerTemplate = "Content-Disposition: form-data; name=\"_attachments\"; filename=\"" + attachment.Filename + "\"\r\n Content-Type: application/octet-stream\r\n\r\n";
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(headerTemplate);
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");


        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + server.Host + ":" + 
            server.Port.ToString() + "/" + db.Name + "/" + attachment.Document.Id);
        httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
        httpWebRequest.Method = "POST";
        httpWebRequest.KeepAlive = true;
        httpWebRequest.ContentLength = attachment.Stream.Length + headerbytes.Length + boundarybytes.Length;

        if (!string.IsNullOrEmpty(server.EncodedCredentials))
            httpWebRequest.Headers.Add("Authorization", server.EncodedCredentials);

        if (!attachment.Stream.CanRead)
            throw new System.NotSupportedException("The stream cannot be read.");

        // Get the request stream
        try
        {
            dataStream = httpWebRequest.GetRequestStream();
        }
        catch (Exception e)
        {
            throw new WebException("Failed to get the request stream.", e);
        }


        buffer = new byte[server.BufferSize];
        int bytesRead;

        dataStream.Write(headerbytes,0,headerbytes.Length); 

        attachment.Stream.Position = 0;
        while ((bytesRead = attachment.Stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            dataStream.Write(buffer, 0, bytesRead);
        }

        dataStream.Write(boundarybytes, 0, boundarybytes.Length);
        dataStream.Close();

        // send the request and get the response
        try
        {
            response = (HttpWebResponse)httpWebRequest.GetResponse();
        }
        catch (Exception e)
        {
            throw new WebException("Invalid response received from server.", e);
        }

        // get the server's response json
        try
        {
            dataStream = response.GetResponseStream();
            sr = new StreamReader(dataStream);
            json = sr.ReadToEnd();
        }
        catch (Exception e)
        {
            throw new WebException("Failed to access the response stream.", e);
        }

        // close up all our streams and response
        sr.Close();
        dataStream.Close();
        response.Close();

        // Deserialize the server response
        return ConvertTo.JsonToServerResponse(json);
    }

在对这个主题进行了大量研究之后,我决定使用PUT。虽然Futon使用POST方法,但它没有记录在案。对于将来阅读本文的人,请使用PUT方法,它将使您的生活更加轻松。

在对该主题进行了大量研究之后,我决定使用PUT。虽然Futon使用POST方法,但它没有记录在案。对于将来阅读此文章的人,请使用PUT方法,它将使您的生活更加轻松。

您将向哪种服务器发帖?另外,使用HTTP/1.0时会出现什么错误。您正在使用身份验证吗?如果是,什么样的身份验证?CouchDB有自己的web服务器,所以他们的自定义服务器。与1.0相同的错误。基本身份验证。您要发布到哪种服务器?另外,使用HTTP/1.0时会出现什么错误。您正在使用身份验证吗?如果是,什么样的身份验证?CouchDB有自己的web服务器,所以他们的自定义服务器。与1.0相同的错误。基本身份验证。