How to post JSON to a server using C#?

后端 未结 13 1804
臣服心动
臣服心动 2020-11-22 05:55

Here\'s the code I\'m using:

// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.Protoco         


        
13条回答
  •  梦如初夏
    2020-11-22 06:06

    I finally invoked in sync mode by including the .Result

    HttpResponseMessage response = null;
    try
    {
        using (var client = new HttpClient())
        {
           response = client.PostAsync(
            "http://localhost:8000/....",
             new StringContent(myJson,Encoding.UTF8,"application/json")).Result;
        if (response.IsSuccessStatusCode)
            {
                MessageBox.Show("OK");              
            }
            else
            {
                MessageBox.Show("NOK");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("ERROR");
    }
    

提交回复
热议问题