Here\'s the code I\'m using:
// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.Protoco
The HttpClient type is a newer implementation than the WebClient
and HttpWebRequest
.
You can simply use the following lines.
string myJson = "{'Username': 'myusername','Password':'pass'}";
using (var client = new HttpClient())
{
var response = await client.PostAsync(
"http://yourUrl",
new StringContent(myJson, Encoding.UTF8, "application/json"));
}
When you need your HttpClient
more than once it's recommended to only create one instance and reuse it or use the new HttpClientFactory
.