ASP core mvc server, standard template for VS2015 created

[HttpPost] public string Add([FromBody]string value) { var str = value; return str; } 

The client is simply on the button in the winform:

  private void button2_Click(object sender, EventArgs e) { var Url = "http://localhost:54327/message"; HttpWebResponse HResp; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = 120000; request.Accept = "text/xml"; var messageToSend = new XDocument(); XElement chatMessage = new XElement("ChatMessage"); XElement message = new XElement("Message", richTextBox3.Text); XElement createTime = new XElement("CreateTime", DateTime.Now); chatMessage.Add(message); chatMessage.Add(createTime); messageToSend.Add(chatMessage); string values =messageToSend.ToString(); request.Credentials = CredentialCache.DefaultCredentials; UTF8Encoding encoding = new UTF8Encoding(); var bytes = encoding.GetBytes(values); request.ContentLength = bytes.Length; request.KeepAlive = true; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); stream.Close(); } HResp = (HttpWebResponse)request.GetResponse(); Stream resStream = HResp.GetResponseStream(); StreamReader reader = new StreamReader(resStream); string ResponseReport = reader.ReadToEnd(); label1.Text = ResponseReport; reader.Dispose(); resStream.Dispose(); HResp.Close(); } 

Everything starts from the studio an instance of "server", an instance of the form. After pressing the button we get:

Unhandled "System.Net.WebException" type exception in System.dll Additional information: The remote server returned an error: (415) Unsupported Media Type.

if you make curl om

C: \ Program Files \ cURL \ bin> curl -H "Content-Type: text / xml" -d @ y.xml -X POST htt p: // localhost: 54327 / message -v * Trying :: 1 .. . * Connected to localhost (:: 1) port 54327 (# 0) POST / message HTTP / 1.1 Host: localhost: 54327 User-Agent: curl / 7.46.0 Accept: / Content-Type: text / xml Content-Length: 51

  • upload counterparties NET <Date: Wed, 16 Nov 2016 17:43:32 GMT <Content-Length: 0 <
  • Connection # 0 to host localhost left intact

The worst thing is that there is another project written at studio 2012 and everything works. Thoughts are there, what is the matter in IIS Express that the studio launches for debugging. But where to dig is completely incomprehensible. What could it be?

    1 answer 1

    It turned out that for some reason IIS Express, which comes with Vs2015 by default, does not know what exists and can be like this: request.ContentType = "application/x-www-form-urlencoded";

    Changing to 'application / json' is better. At least if you send a string, everything works.