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

PREVIOUS NEXT
Code Example
Csharp :: how to find length of list c# 
Csharp :: C# top down view movement 
Csharp :: exe path c# 
Csharp :: unity rigidbody freeze all rotation 
Csharp :: unity basic public options 
Csharp :: telerik winforms get value of selected rows from grid 
Csharp :: irrrtate throught an matrix c# 
Csharp :: create blazor server 
Csharp :: Formcollection asp.net form 
Csharp :: demand a Security action c# 
Csharp :: guicontrol text ahk 
Csharp :: disable alt + f4 in c# forms 
Csharp :: unity how to make gamemanager instance 
Csharp :: load a form from button c# 
Csharp :: c# trimend substring 
Csharp :: C# get filebase name 
Csharp :: how to make rabbitmq start and stop base on c# services 
Csharp :: disable version header c# 
Csharp :: modal barrier in flutter 
Csharp :: unity navmeshagent set destination 
Csharp :: dataset empty check C# 
Csharp :: ef core many to many fluent api 
Csharp :: C# Async Function without await 
Csharp :: c# comments 
Csharp :: c# sc create service 
Csharp :: unity action 
Csharp :: lightbox 
Csharp :: Storing Data within your TileEntity 
Csharp :: what does focusbale do in listview WPF 
Csharp :: c# convert string to base64 string 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =