How to post JSON to a server using C#?

后端 未结 13 1610
臣服心动
臣服心动 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:31

    Some different and clean way to achieve this is by using HttpClient like this:

    public async Task<HttpResponseMessage> PostResult(string url, ResultObject resultObject)
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = new HttpResponseMessage();
            try
            {
                response = await client.PostAsJsonAsync(url, resultObject);
            }
            catch (Exception ex)
            {
                throw ex
            }
            return response;
         }
    }
    
    0 讨论(0)
提交回复
热议问题