0% found this document useful (0 votes)
22 views

Async Call

Async Call

Uploaded by

Santosh Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Async Call

Async Call

Uploaded by

Santosh Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

http://www.codeproject.

com/Articles/234085/Fixing-WCF-to-build-highly-scalable-a
sync-REST-API
http://blog.anthonybaker.me/2013/05/consuming-json-rest-api-asynchronously.html
http://aleksanderspro.wordpress.com/2013/02/19/consuming-rest-service-async-in-p
cl/
http://blog.anthonybaker.me/2013/05/how-to-consume-json-rest-api-in-net.html
http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net
-client
http://blogs.msdn.com/b/henrikn/archive/2012/02/11/httpclient-is-here.aspx
public string Post<t>(T data)
{
using (var httpClient = NewHttpClient())
{
var requestMessage = GetHttpRequestMessage<t>(data);
var result = httpClient.PostAsync(_endpoint, requestMessage.Content)
.Result;
return result.Content.ToString();
}
}

HttpClient client = new HttpClient();


client.BaseAddress = new Uri("http://localhost:56851/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var user = new Users();
user.FirstName = txtFirst.Text;
user.Company = txtCompany.Text;
user.LastName = txtLas.Text;
user.Email = txtEmail.Text;
user.PhoneNo = txtPhone.Text;
user.Email = txtEmail.Text;
var response = client.PostAsJsonAsync("api/User", user).Result;
if (response.IsSuccessStatusCode)
{
MessageBox.Show("User Added");
txtFirst.Text = "";
txtLas.Text = "";
txtPhone.Text = "";
txtEmail.Text = "";
txtCompany.Text = "";
GetData();

}
else
{
MessageBox.Show("Error Code" +
response.StatusCode + " : Message - " + response.ReasonPhrase);
}

HttpResponseMessage response = client.GetAsync(this.url + customerId).Result;


Customer obj = JsonConvert.DeserializeObject<Customer>(response.Content.
ReadAsStringAsync().Result);
Customer[] data = new Customer[1];
data[0] = obj;
return data;

You might also like