Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# HttpClient POST request

using System;
using System.Text;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace HttpClientPost
{
    class Person
    {
        public string Name { get; set; }
        public string Occupation { get; set; }

        public override string ToString()
        {
            return $"{Name}: {Occupation}";
        }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            var person = new Person();
            person.Name = "John Doe";
            person.Occupation = "gardener";

            var json = JsonConvert.SerializeObject(person);
            var data = new StringContent(json, Encoding.UTF8, "application/json");

            var url = "https://httpbin.org/post";
            using var client = new HttpClient();

            var response = await client.PostAsync(url, data);

            string result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
        }
    }
}
Comment

httpclient post c# example

using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri("http://somesite.com");
            var content = new FormUrlEncodedContent(new[] 
            {
                new KeyValuePair<string, string>("accountidentifier", accountID),
                new KeyValuePair<string, string>("type", "add"),
                new KeyValuePair<string, string>("seriesid", seriesId),

            });

            httpClient.PostAsync("/api/User_Favorites.php", content);
}
Comment

webclient c# example post

string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
}
Comment

httpclient post call c# basic authentication

HttpResponseMessage httpResponseMessage = await httpClient.PostAsJsonAsync("/post", request).ConfigureAwait(false);
2
PersonResponse personResponse = await httpResponseMessage.Content.ReadAsAsync<PersonResponse>().ConfigureAwait(false);
Comment

PREVIOUS NEXT
Code Example
Csharp :: failed to read the request form. missing content-type boundary .net core 
Csharp :: c# datagridview selected row index 
Csharp :: exit button unity code 
Csharp :: What is the difference between String and string in C#? 
Csharp :: total months between two dates c# 
Csharp :: c# console header 
Csharp :: how to use the mouse scroll wheel to move the camera in unity 
Csharp :: c-sharp - get current page url/path/host 
Csharp :: read all lines from txt c# 
Csharp :: visual studio console clear 
Csharp :: c# convert stream to memorystream 
Csharp :: how to generate random number in unity 
Csharp :: c# calculator 
Csharp :: c# access session in class 
Csharp :: clamp vector3 unity 
Csharp :: c# add multiple items to list 
Csharp :: turn list of string to csv c# 
Csharp :: CS0101 Unity Error Code 
Csharp :: c# mongodb update multiple fields 
Csharp :: unity audio manager 
Csharp :: c# round double 
Csharp :: c# remove first line from textbox 
Csharp :: how to save a dictionary as a csv file in c# 
Csharp :: get key value from object c# 
Csharp :: hcf of numbers 
Csharp :: c# get total milliseconds from datetime 
Csharp :: unity 2d 
Csharp :: trim c# 
Csharp :: c# get type of class 
Csharp :: c# create array with n elements 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =