Couchdb....HttpWebRequest
I am having trouble with getting an HTTP PUT method working with a Couchdb create database. Nothing scary about the code, you have all seen it before so won't post it, as its too boring. Error message when using a put Method is Connection is Closed. Is there something I should be aware of....something really noddy. I am getting a 404 when I use a POST method which is correct in the context of creating a new DB with Couchdb. Any help much appreciated. Can HTTPWebRequest do a PUT method?, if it can not then I am baffled to why not.
more specific : the error is : The underlying connection was closed: The connection was closed unexpectedly.
I have checked Couchdb with CURL...and it works just fine.
error comes at this point...after setting the method to "PUT"
Stream requestStream = httpWebRequest.GetRequestStream();
code snippet:
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, re开发者_开发知识库questData.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;
}
}
this didn't work either :(
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;
}
}
I know that this isn't a solution to the problem, but is there some reason why you can't use the System.Net.WebClient
class if you just want to grab data from a URL? It would eliminate a lot of the cluttered code you wrote. You can literally use it with one line, like this:
string data = new WebClient().DownloadString(@"http://whateverURL.com/?options=1&somethingElse=5");
精彩评论