Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

.NET HttpClient. How to POST string value?

Writer Emily Wong

How can I create using C# and HttpClient the following POST request:User-Agent: Fiddler Content-type: application/x-www-form-urlencoded Host: localhost:6740 Content-Length: 6

I need such a request for my WEB API service:

[ActionName("exist")]
[HttpPost]
public bool CheckIfUserExist([FromBody] string login)
{ return _membershipProvider.CheckIfExist(login);
}
4

5 Answers

using System;
using System.Collections.Generic;
using System.Net.Http;
class Program
{ static void Main(string[] args) { Task.Run(() => MainAsync()); Console.ReadLine(); } static async Task MainAsync() { using (var client = new HttpClient()) { client.BaseAddress = new Uri(""); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("", "login") }); var result = await client.PostAsync("/api/Membership/exists", content); string resultContent = await result.Content.ReadAsStringAsync(); Console.WriteLine(resultContent); } }
}
8

Below is example to call synchronously but you can easily change to async by using await-sync:

var pairs = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("login", "abc") };
var content = new FormUrlEncodedContent(pairs);
var client = new HttpClient {BaseAddress = new Uri("")}; // call sync
var response = client.PostAsync("/api/membership/exist", content).Result;
if (response.IsSuccessStatusCode)
{
}
2

Here I found this article which is send post request using JsonConvert.SerializeObject() & StringContent() to HttpClient.PostAsync data

static async Task Main(string[] args)
{ var person = new Person(); person.Name = "John Doe"; person.Occupation = "gardener"; var json = Newtonsoft.Json.JsonConvert.SerializeObject(person); var data = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json"); var url = ""; using var client = new HttpClient(); var response = await client.PostAsync(url, data); string result = response.Content.ReadAsStringAsync().Result; Console.WriteLine(result);
}

There is an article about your question on asp.net's website. I hope it can help you.

How to call an api with asp net

Here is a small part from the POST section of the article

The following code sends a POST request that contains a Product instance in JSON format:

// HTTP POST
var gizmo = new Product() { Name = "Gizmo", Price = 100, Category = "Widget" };
response = await client.PostAsJsonAsync("api/products", gizmo);
if (response.IsSuccessStatusCode)
{ // Get the URI of the created resource. Uri gizmoUrl = response.Headers.Location;
}
3

You could do something like this

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 6;
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();

And then strReponse should contain the values returned by your webservice

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy