HttpClient web request HttpGet and HttpPost

HTTPGET

Uri mCmpUrl = new Uri("http://www.domain.com/");
                
HttpClient oHttpClient = new HttpClient();
oHttpClient.BaseAddress = mCmpUrl;

oHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var url = "api/Account?UserId=1"&Password=123";
HttpResponseMessage response = oHttpClient.GetAsync(url).Result;

if (response.IsSuccessStatusCode)
{
   var result = response.Content.ReadAsStringAsync().Result;
   var AccModResult = JsonConvert.DeserializeObject<AccMod>(result);
}

HTTPPOST

Uri mCmpUrl = new Uri("http://www.domain.com/Account");

var json = JsonConvert.SerializeObject(obj);
HttpClient oHttpClient = new HttpClient();
var oTaskPostAsync = oHttpClient.PostAsync(mCmpUrl, new StringContent(json, Encoding.UTF8, "application/json"));

oTaskPostAsync.ContinueWith((oHttpResponseMessage) =>
{
                
    if (oHttpResponseMessage.IsCompleted)
    {
       //
    }
    else
    {
       //
    }
                
});

Comments

Popular posts from this blog

SQL Interview Questions and Answers

Generic Interface and Factory Pattern in C#

How to get device info IMEI programmatically in xamarin android