我在让HTTP PUT方法与Couchdb create数据库一起工作时遇到了问题。代码没什么可怕的,你们都看过了,所以不会发布,因为它太无聊了。使用put方法is Connection is Closed时的错误消息。有没有什么我应该知道的,of....something真的点了头。当我使用POST方法时,我得到了404,这在使用Couchdb创建新数据库的上下文中是正确的。任何帮助都非常感谢。HTTPWebRequest能做PUT方法吗?如果不能,我不明白为什么不能。
更具体地说:错误是:底层连接关闭:连接意外关闭。
我已经用CURL...and检查过Couchdb,它工作得很好。
此point...after将方法设置为"PUT“时出错
Stream requestStream = httpWebRequest.GetRequestStream();代码片段:
private string DataViaHTTP(string url, Dictionary<string, string> parameters, string content, string contentType, int timeout, bool contentIsParam, string method)
{
byte[] requestData;
try
{
HttpWebRequest httpWebRequest;
if (contentIsParam == false)
{
requestData = System.Text.Encoding.ASCII.GetBytes(content);
httpWebRequest = (HttpWebRequest)WebRequest.Create(BuildParamString(url, parameters));
}
else
{
requestData = System.Text.Encoding.ASCII.GetBytes(BuildParamString(null, parameters));
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
}
httpWebRequest.Method = method;
httpWebRequest.ContentType = contentType;
if (timeout > 0)
{
httpWebRequest.Timeout = timeout;
}
httpWebRequest.ContentLength = requestData.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(requestData, 0, requestData.Length);
requestStream.Close();
// Read and return the response stream
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream outStream = httpWebResponse.GetResponseStream();
var stringStream = String.Empty;
using (StreamReader streamReader = new StreamReader(outStream))
{
stringStream = streamReader.ReadToEnd();
}
return stringStream;
}
catch (WebException e)
{
throw e;
}
catch (Exception e)
{
throw e;
}
}这也不起作用:(
public string PutCommand(string url)
{
try
{
using (WebClient webclient = new WebClient())
{
webclient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.89 Safari/532.5";
webclient.Encoding = System.Text.Encoding.ASCII;
var x = webclient.UploadData(url, "PUT", new byte[] {});
return System.Text.Encoding.ASCII.GetString(x);
}
}
catch (Exception e)
{
throw e;
}
}发布于 2011-04-29 04:19:43
我知道这不是问题的解决方案,但是如果您只是想从一个网址中获取数据,那么为什么不能使用System.Net.WebClient类呢?它将消除你所写的大量杂乱的代码。你可以只用一行字来使用它,就像这样:
string data = new WebClient().DownloadString(@"http://whateverURL.com/?options=1&somethingElse=5");https://stackoverflow.com/questions/5811436
复制相似问题