public Contact Post(Contact contact) { if (contact == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } contact.Id = totalContacts++; contacts.Add(contact); return contact; }
public Contact Post(Contact contact) { if (contact == null) { throw new ArgumentNullException("contact"); } contact.Id = totalContacts++; contacts.Add(contact); return contact; }
protected void PostNewContact_Click(object sender, EventArgs e) { string address = "http://localhost:8081/contacts"; HttpClient client = this.GetClient(address); var contact = new Contact { Name = this.TextBox3.Text, Id = 5 }; var serializer = new XmlSerializer(typeof(Contact)); var stream = new MemoryStream(); serializer.Serialize(stream, contact); stream.Position = 0; var request = new HttpRequestMessage(HttpMethod.Post, address); request.Content = new StreamContent(stream); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); var response = client.Send(request); var receivedContact = response.Content.ReadAsObject<Contact>(); this.Result.Text = string.Format(CultureInfo.InvariantCulture, "\r\n Contact Name: {0}, Contact Id: {1}", receivedContact.Name, receivedContact.Id); }
protected void PostNewContact_Click(object sender, EventArgs e) { string uri = "http://localhost:8300/contacts"; HttpClient client = this.GetClient(uri); var contact = new Contact { Name = this.TextBox3.Text, Id = 5 }; var request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri)); request.Content = new ObjectContent<Contact>(contact, "application/xml"); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); var response = client.Send(request); var receivedContact = response.Content.ReadAs<Contact>(); this.Result.Text = string.Format(CultureInfo.InvariantCulture, "\r\n Contact Name: {0}, Contact Id: {1}", receivedContact.Name, receivedContact.Id); }