@manhng

Welcome to my blog!

HTTP POST with WebClient

April 21, 2020 11:49

HTTP POST with WebClient (edit)

Post Files: 

https://dzimchuk.net/uploading-a-file-over-http-in-net/

https://blog.bitscry.com/2018/08/29/multipart-form-file-uploads-using-webclient/

https://www.c-sharpcorner.com/article/upload-any-file-using-http-post-multipart-form-data/

https://stackoverflow.com/questions/1131425/send-a-file-via-http-post-with-c-sharp

Post Json:

https://www.c-sharpcorner.com/blogs/authorize-and-login-to-instagram-in-asp-net-c-sharp (HAY)

https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request (HAY HAY HAY)

https://www.c-sharpcorner.com/UploadFile/dacca2/http-request-methods-get-post-put-and-delete/ (HAY)

http://zetcode.com/csharp/httpclient/ (HAY)

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

ASP.NET Web API 2

public class ValuesController : ApiController
{
// POST api/<controller>
public IHttpActionResult Post([System.Web.Http.FromBody]AccessToken value)
{
return Ok(new { success = true, access_token = "123456" });
...
}
public class AccessToken
{
public string client_id { get; set; }
public string client_secret { get; set; }
public string grant_type { get; set; }
public string redirect_uri { get; set; }
public string code { get; set; }
}

Console App

private static void Main(string[] args)
{
System.Collections.Specialized.NameValueCollection parameters = new System.Collections.Specialized.NameValueCollection();
parameters.Add("client_id", "Your client id");
parameters.Add("client_secret", "Your app secret");
parameters.Add("grant_type", "authorization_code");
parameters.Add("redirect_uri", "http://localhost:4220/Account/OAuth");
parameters.Add("code", "code");
System.Net.WebClient client = new System.Net.WebClient();
var result = client.UploadValues("http://localhost:4220/api/values", "POST", parameters);
var response = System.Text.Encoding.Default.GetString(result);
var jsResult = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(response);
string accessToken = (string)jsResult["access_token"];
...
}

Categories

Recent posts