使用WebService传递json数据的C#代码

时间:2014-07-11 11:55:52

标签: c#

response = requests.patch( "https://<manageraddress>/api/admin/configuration/v1/conference/1/", auth=('<user1>', '<password1>'), verify=False, data=json.dumps({'pin': '1234'}) https://tsmgr.tsecurevideo.com/api/admin/configuration/v1/conference/1/" 

我试过了

  HttpWebRequest httpWReq =(HttpWebRequest)WebRequest.Create(string.Format("https://tsmgr.tsecurevideo.com/api/admin/configuration/v1/conference/2/"));
   Encoding encoding = new UTF8Encoding();
   string postData = "{\"pin\":\"1234\"}";
   byte[] data = encoding.GetBytes(postData);

   httpWReq.ProtocolVersion = HttpVersion.Version11;
   httpWReq.Method = "POST";
   httpWReq.ContentType = "application/json";//charset=UTF-8";

   string credentials =   Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("admin" + ":" + "password"));
   httpWReq.Headers.Add("Authorization", "Basic " + credentials);
   httpWReq.ContentLength = data.Length;


   Stream stream = httpWReq.GetRequestStream();
   stream.Write(data, 0, data.Length);
   stream.Close();

   HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
   string s = response.ToString();
   StreamReader reader = new StreamReader(response.GetResponseStream());

我收到错误

  

远程服务器返回错误:(501)未实现。

2 个答案:

答案 0 :(得分:0)

尝试以这种方式发送凭据

string auth = string.Format("{0}:{1}", "admin","password");
string data = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
string credentials= string.Format("{0} {1}", "Basic", data );
httpWReq.Headers[HttpRequestHeader.Authorization] = credentials;

请参阅here以获取Encoding.ASCII

中的文档

答案 1 :(得分:0)

来自HTTP spec

  

501未实施

     

服务器不支持完成请求所需的功能。当服务器无法识别请求方法且无法为任何资源支持时,这是适当的响应。

听起来服务器不支持PATCH方法。