How to get content body from a httpclient call?

前端 未结 2 829
一个人的身影
一个人的身影 2020-12-02 08:06

I\'ve been trying to figure out how to read the contents of a httpclient call, and I can\'t seem to get it. The response status I get is 200, but I can\'t figure out how to

2条回答
  •  庸人自扰
    2020-12-02 08:28

    If you are not wanting to use async you can add .Result to force the code to execute synchronously:

    private string GetResponseString(string text)
    {
        var httpClient = new HttpClient();
    
        var parameters = new Dictionary();
        parameters["text"] = text;
    
        var response = httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters)).Result;
        var contents = response.Content.ReadAsStringAsync().Result;
    
        return contents;
     }  
    

提交回复
热议问题