c# make http request
private static readonly HttpClient client = new HttpClient();
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
c# make http request
private static readonly HttpClient client = new HttpClient();
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
httpclient C#
//example code
//var lst = await service.ConnectHttpClient<User, List<User>>("api/User/GetUserInfoByUserId", new User() { UserId = 1 });
//var lst = await service.ConnectRestClient<User, List<User>>("api/User/GetUserInfoByUserId", new User() { UserId = 1 });
public async Task<M> ConnectHttpClient<R, M>(string apiUrl, R reqModel)
{
M model = default(M);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, reqModel);
if (response.IsSuccessStatusCode)
{
var res = response.Content.ReadAsStringAsync().Result;
model = JsonConvert.DeserializeObject<M>(res);
}
return model;
}
public async Task<M> ConnectRestClient<R, M>(string apiUrl, R reqModel)
{
M model = default(M);
RestClient restClient = new RestClient(baseUrl);
RestRequest restRequest = new RestRequest(apiUrl, Method.POST, DataFormat.Json);
restRequest.AddJsonBody(reqModel);
IRestResponse restResponse = await restClient.ExecuteAsync(restRequest);
if (restResponse.IsSuccessful)
{
string response = restResponse.Content;
model = JsonConvert.DeserializeObject<M>(response);
}
return model;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us