MVC website does not receive string from client

Customer:

WebClient wc = new WebClient(); try { string json = wc.UploadString("http://localhost:50001/Client/Index", "1"); dynamic receivedData = JsonConvert.DeserializeObject(json); Console.WriteLine("Result: {0};",receivedData.data); } catch (Exception e) { Console.WriteLine("Oh bother"); Console.WriteLine(); Console.WriteLine(e.Message); } 

Basically sends a “1” to the Index action in the Client controller.

Here is the controller:

 [HttpPost] public ActionResult Index(string k) { Debug.WriteLine(String.Format("Result: {0};", k)); return Json(new { data = k}, JsonRequestBehavior.DenyGet); } 

The result of the Client is simply "Result :;". The controller debug output is also "Result :;". This means that data is lost somewhere between the client and the site. But when I debug, Visual Studio says there was one request.

+5
source share
4 answers

By adding a title and specifying the parameter name, I managed to get this to work (in your calling method):

  static void Main(string[] args) { WebClient wc = new WebClient(); try { wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; string json = wc.UploadString("http://localhost:49847/Home/Index", "k=1"); dynamic receivedData = JsonConvert.DeserializeObject(json); Console.WriteLine("Result: {0};", receivedData.data); } catch (Exception e) { Console.WriteLine("Oh bother"); Console.WriteLine(); Console.WriteLine(e.Message); } } 

From MSDN :

... set the HTTP Content-Type header to application / x-www-form-urlencoded to notify the server that is generating the data attached to the message.

I did not start the violinist to check where (by default) headers are sent, but I suspect that the reason for this does not work without a header - the receiving client does not know where to find the parameters of the query string passed through.

From fooobar.com/questions/11080 / ... :

When you receive a POST request, you should always expect a "payload", or, in terms of HTTP: the body of the message. The body of the message itself is pretty useless as there is no standard (as far as I can tell. Application / octet-stream?). The body format is determined by the Content-Type header. When using an HTML FORM element with method = "POST", this is usually application / x-www-form-urlencoded.

+2
source

WebAPI can interpret your argument as a URI argument.

Try the following:

 [HttpPost] public ActionResult Index([FromBody] string k) { Debug.WriteLine(String.Format("Result: {0};", k)); return Json(new { data = k}, JsonRequestBehavior.DenyGet); } 

This tells the WebAPI to expect this argument to be removed from the request body (e.g. JSON payload)

+2
source

Try

  string parameters = string.Concat("k=","1"); string url = "http://localhost:50001/Client/Index"; using (Var wc = new WebClient()){ wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded;charset=UTF-8"; string result = wc.UploadString(url, parameters); JObject obj = JObject.Parse(result); Console.WriteLine("Result: {0};", obj.data); }` 
+2
source

You need to slightly modify your action to retrieve the published value from the request stream:

 [HttpPost] public ActionResult Index(string k) { var stream = Request.InputStream; string value = string.Empty; using (var reader = new StreamReader(stream)) { value = reader.ReadToEnd(); } Debug.WriteLine(String.Format("Result: {0};", value)); return Json(new { data = value}, JsonRequestBehavior.DenyGet); } 
+1
source

Source: https://habr.com/ru/post/1266467/


All Articles