example HttpClient c# Post
async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
var finalResult = await GetResponseString(text);
4.2
10
//Base code from: http://zetcode.com/csharp/httpclient/
public async string Example()
{
//The data that needs to be sent. Any object works.
var pocoObject = new
{
Name = "John Doe",
Occupation = "gardener"
};
//Converting the object to a json string. NOTE: Make sure the object doesn't contain circular references.
string json = JsonConvert.SerializeObject(pocoObject);
//Needed to setup the body of the request
StringContent data = new StringContent(json, Encoding.UTF8, "application/json");
//The url to post to.
var url = "https://httpbin.org/post";
var client = new HttpClient();
//Pass in the full URL and the json string content
var response = await client.PostAsync(url, data);
//It would be better to make sure this request actually made it through
string result = await response.Content.ReadAsStringAsync();
//close out the client
client.Dispose();
return result;
}
Thank you!
10
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity