0

I'm working on a C# application that connects to the Appcelerator Cloud Service, so far I can make queries and login, but when I tried to update a Custom Object I got the following error: The remote server returned an error: (401) Unauthorized. This is my code for my POST request:

           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);

I have a theory and it involves the login, at the beginning of my application I make a login which return an ok status, but I think I need to somehow let know ACS I logged in already by sending the session id or something like that when I try to update the Custom Object.

Edit That is why I tried to add the cookie in the header like this:

                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);

Where session is the session id I got from my successful login. But even though I added this to the headers in my request the error The remote server returned an error: (401) Unauthorized.

Edit: I tried something else:

                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("Cookie", session); //changed the header but it didn't work
                objStream = wrGetUrl.GetResponse().GetResponseStream(); // still throws the same error
                reader = new StreamReader(objStream);

How can I solve this problem? Thanks for any help in advance.

4

1 回答 1

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 中写入以发送我想要更新的字段。希望这可以帮助将来的人们。

于 2012-08-23T23:15:03.533 回答