我正在开发一个连接到Appcelerator Cloud Service的C#应用程序,到目前为止我可以进行查询和登录,但是当我尝试更新Custom Object
时,我收到以下错误:{{1} }。这是我的POST请求的代码:
The remote server returned an error: (401) Unauthorized
我有一个理论,它涉及登录,在我的应用程序开始时我做了一个返回ok状态的登录,但我想我需要以某种方式让我知道ACS我已经通过发送会话ID或其他东西登录就像我尝试更新 url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&noDisponibles=" + noDisponibles;
wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
wrGetUrl.Method = "POST";
wrGetUrl.ContentType = "application/json";
objStream = wrGetUrl.GetResponse().GetResponseStream(); // this is the line where the error is thrown
reader = new StreamReader(objStream);
时那样。
修改这就是我尝试在标题中添加Cookie的原因:
Custom Object
url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&noDisponibles=" + noDisponibles;
wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
wrGetUrl.Method = "POST";
wrGetUrl.ContentType = "application/json";
wrGetUrl.Headers.Add("Set-Cookie", "_session_id=" + session + "; path=/; HttpOnly");
objStream = wrGetUrl.GetResponse().GetResponseStream(); // this is the line where the error is thrown
reader = new StreamReader(objStream);
是我成功登录后获得的会话ID。但即使我在请求中将其添加到标题中,也会出现错误session
。
修改 我尝试了别的东西:
The remote server returned an error: (401) Unauthorized
我该如何解决这个问题?感谢您提前提供任何帮助。
答案 0 :(得分:0)
我解决了这个问题:
String fields = "fields={\"noDisponibles\":" + noDisponibles +"}";
String url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&_session_id="+session;
HttpWebRequest wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
wrGetUrl.Method = "PUT";
wrGetUrl.ContentLength = fields.Length;
wrGetUrl.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = wrGetUrl.GetRequestStream();
byte[] byteArray = Encoding.ASCII.GetBytes(fields);
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
Stream objStream = wrGetUrl.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(objStream);
我必须在我的HttpWebRequest的DataStream中写入我想要更新的字段。希望这有助于将来的人们。