Adeel Khan
Adeel Khan

Reputation: 195

httpClient show me error c# winform

I have following code but its show error I am using framework 4.5. please help .

var baseAddress = new Uri("http://private-5e199-karhoofleetintegration.apiary-mock.com/");

using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept-charset", "utf-8");

    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", "Basic *sample_token*");

    using (var content = new StringContent("{  \"vehicles\": [    {      \"vehicle_type\": \""+ vehicale_type +"\",      \"vehicle_id\": \"" +vehicle_id+"\",  "\"heading\": 90      }    }  ]}", System.Text.Encoding.Default, "application/json"))
    {
        using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", content))
        {
            string responseData = await response.Content.ReadAsStringAsync();
        }
    }
}

enter image description here

Upvotes: 2

Views: 409

Answers (1)

Nasreddine
Nasreddine

Reputation: 37888

To use the async/await stuff you'll need to mark your method with the async keyword.

If your method is an event handler then use async void and if it's not then use async Task or async Task<ReturnType>. (make sure you replace "ReturnType" with actual type returned by your method)

Example:

public async Task GetDataFromTheWeb()
//     ^^^^^ add this keyword
{
    var baseAddress = new Uri("http://private-5e199-karhoofleetintegration.apiary-mock.com/");

    using (var httpClient = new HttpClient { BaseAddress = baseAddress })
    {
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept-charset", "utf-8");

        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", "Basic *sample_token*");

        using (var content = new StringContent("{  \"vehicles\": [    {      \"vehicle_type\": \""+ vehicale_type +"\",      \"vehicle_id\": \"" +vehicle_id+"\",  "\"heading\": 90      }    }  ]}", System.Text.Encoding.Default, "application/json"))
        {
            using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", content))
            {
                string responseData = await response.Content.ReadAsStringAsync();
            }
        }
    }
}

Upvotes: 1

Related Questions