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 :: c# how to use inovke 
Csharp :: abril modal boostrap 
Csharp :: how to add a list to observablecollection in c# 
Csharp :: c# postmessage mouse click 
Csharp :: how to convert from hexadecimal to binary in c# 
Csharp :: reverse for loop unity 
Csharp :: how to execute linux command from c# 
Csharp :: wpf restart application c# 
Csharp :: c# convert string to int 
Csharp :: c# remove from list in foreach 
Csharp :: c# append multiline textbox 
Csharp :: dynamic convert type c# 
Csharp :: generate random name c# 
Csharp :: get working directory c# 
Csharp :: c# combine list of bool 
Csharp :: c# socket listen on port 
Csharp :: how to change the color of a sprite in unity 
Csharp :: maximize window c# console 
Csharp :: triangle minimum path sum c# 
Csharp :: asking for user input integer c# 
Csharp :: convert comma separated string to array c# 
Csharp :: console.writeline c# 
Csharp :: 2d list in c# 
Csharp :: c# making a folder 
Csharp :: c# string to byte[] 
Csharp :: how to destroy a gameobject in c# 
Csharp :: c# encode jpg hiight quality 
Csharp :: c# field vs property 
Csharp :: unity how to move an object 
Csharp :: c# messagebox result 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =