0
我想用.NET類而不是curl命令行復制一個Couch數據庫。我從來沒有使用過WebRequest或Httpwebrequest,但我正試圖用下面的腳本來發出請求。Httpwebrequest - 使用文本文件的POST方法
下面是CouchDB的複製JSON腳本(我知道這工作):
{ ""_id"":"database_replicate8/7/12", "source":sourcedb, ""target"":"targetDB", ""create_target"":true, ""user_ctx"": { ""roles"": ["myrole"] } }
以上腳本被放入一個文本文件,sourcefile.txt。我想採用這一行,並使用.NET功能將其放入POST Web請求中。
查看後,我選擇使用httpwebrequest類。下面是我迄今爲止 - 我得到這個從http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
HttpWebRequest bob = (HttpWebRequest)WebRequest.Create("sourceDBURL");
bob.Method = "POST";
bob.ContentType = "application/json";
byte[] bytearray = File.ReadAllBytes(@"sourcefile.txt");
Stream datastream = bob.GetRequestStream();
datastream.Write(bytearray, 0, bytearray.Length);
datastream.Close();
我要對這個正確?我對網絡技術比較陌生,並且仍然在學習http調用的工作方式。