Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSTing JSON to URL via WebClient in C#

Tags:

c#

webclient

I have some JavaScript code that I need to convert to C#. My JavaScript code POSTs some JSON to a web service that's been created. This JavaScript code works fine and looks like the following:

var vm = { k: "1", a: "2", c: "3", v: "4" }; $.ajax({   url: "http://www.mysite.com/1.0/service/action",   type: "POST",   data: JSON.stringify(vm),   contentType: "application/json;charset=utf-8",   success: action_Succeeded,   error: action_Failed });  function action_Succeeded(r) {   console.log(r); }  function log_Failed(r1, r2, r3) {   alert("fail"); } 

I'm trying to figure out how to convert this to C#. My app is using .NET 2.0. From what I can tell, I need to do something like the following:

using (WebClient client = new WebClient()) {   string json = "?";   client.UploadString("http://www.mysite.com/1.0/service/action", json); } 

I'm a little stuck at this point. I'm not sure what json should look like. I'm not sure if I need to set the content type. If I do, I'm not sure how to do that. I also saw UploadData. So, I'm not sure if I'm even using the right method. In a sense, the serialization of my data is my problem.

Can someone tell me what I'm missing here?

Thank you!

like image 777
Eels Fan Avatar asked Feb 26 '13 14:02

Eels Fan


People also ask

How do you post data on WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.

What is the use of WebClient in C#?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


2 Answers

The question is already answered but I think I've found the solution that is simpler and more relevant to the question title, here it is:

var cli = new WebClient(); cli.Headers[HttpRequestHeader.ContentType] = "application/json"; string response = cli.UploadString("http://some/address", "{some:\"json data\"}"); 

PS: In the most of .net implementations, but not in all WebClient is IDisposable, so of cource it is better to do 'using' or 'Dispose' on it. However in this particular case it is not really necessary.

like image 120
sarh Avatar answered Sep 23 '22 05:09

sarh


The following example demonstrates how to POST a JSON via WebClient.UploadString Method:

var vm = new { k = "1", a = "2", c = "3", v=  "4" }; using (var client = new WebClient()) {    var dataString = JsonConvert.SerializeObject(vm);    client.Headers.Add(HttpRequestHeader.ContentType, "application/json");    client.UploadString(new Uri("http://www.contoso.com/1.0/service/action"), "POST", dataString); } 

Prerequisites: Json.NET library

like image 43
Vadim Gremyachev Avatar answered Sep 19 '22 05:09

Vadim Gremyachev