Exemplo n.º 1
0
        public async void Update(Contact c) {
            string results = String.Empty;

            WebRequest req = WebRequest.Create(CONTACT_SERVICE_SINGLE_URL + c.Id.ToString());
            req.Method = "PUT";
            req.ContentType = @"application/json; charset=utf-8";

            string id = string.Format(@"{{""Id"": ""{0}""", c.Id);
            string firstName = string.Format(@"""FirstName"": ""{0}""", c.FirstName);
            string lastName = string.Format(@"""LastName"": ""{0}""", c.LastName);
            string email = string.Format(@"""Email"": ""{0}""}}", c.Email);
            string updateContactString = string.Format("{0},{1},{2},{3}", id, firstName, lastName, email);

            using (Stream stream = await req.GetRequestStreamAsync()) {
                stream.Write(Encoding.UTF8.GetBytes(updateContactString), 0, Encoding.UTF8.GetByteCount(updateContactString));
            }

            HttpWebResponse resp = null;
            try {
                resp = await req.GetResponseAsync() as HttpWebResponse;
                results = string.Format("Status Code: {0}, Status Description: {1}",
                    resp.StatusCode, resp.StatusDescription);
            }
            catch (WebException we) {
                results = we.Message;
            }
        }
Exemplo n.º 2
0
        public async void Delete(Contact c) {
            string results = String.Empty;

            if (c.Id == 0) {
                await new MessageDialog("Contact Id is 0", "Delete Error").ShowAsync();
                return;
            }

            WebRequest req = WebRequest.Create(CONTACT_SERVICE_SINGLE_URL + c.Id.ToString());
            req.Method = "DELETE";

            HttpWebResponse resp = await req.GetResponseAsync() as HttpWebResponse;
            if (resp.StatusCode == HttpStatusCode.OK) {
                using (Stream respStream = resp.GetResponseStream()) {
                    StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
                    results = reader.ReadToEnd();
                }
            }
        }