2010-04-01 54 views
2

我想使用HttpWebRequest張貼附件到CouchDB。但是,當我嘗試「響應=(HttpWebResponse)httpWebRequest.GetResponse();」我收到一條WebException消息,其中「底層連接已關閉:預計將保持活動狀態的連接已被服務器關閉」。KeepAliveException當使用HttpWebRequest.GetResponse

我發現一些文章指出將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); 
    } 
+0

你要發佈什麼樣的服務器?另外,當你使用HTTP/1.0時會得到什麼錯誤。你在使用認證嗎?如果是這樣,什麼樣的認證? – feroze 2010-04-01 23:11:22

+0

CouchDB有自己的Web服務器,所以他們的自定義服務器。與1.0相同的錯誤。基本認證。 – Lucas 2010-04-03 21:17:05

回答

0

經過對該主題的大量研究後,我決定使用PUT。雖然蒲團使用POST方法,但它是無證的。對於將來閱讀此內容的任何人,請使用PUT方法,它會讓您的生活更輕鬆。

相關問題