webclient c# example post
//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;
}
4
6
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);
}
Thank you!
6
0
4
10
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);
}
Thank you!
10
0
Are there any code examples left?
New code examples in category C#