本文介绍了如何从httpclient调用中获取内容主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试弄清楚如何读取httpclient调用的内容,但似乎无法理解。我得到的响应状态是200,但是我不知道如何获得返回的实际Json,这就是我所需要的!

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 get to the actual Json being returned, which is all I need!

以下是我的代码:

async Task<string> GetResponseString(string text)
{
    var httpClient = new HttpClient();

    var parameters = new Dictionary<string, string>();
    parameters["text"] = text;
    Task<HttpResponseMessage> response = 
        httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));

    return await response.Result.Content.ReadAsStringAsync();
}

而我只是从方法中调用它:

And I am getting it just calling it from a method:

Task<string> result =  GetResponseString(text);

这就是我得到的

更新:这是我当前根据Nathan的回答在下面使用的当前代码

Update: This is my current code per Nathan's response below

    async Task<string> GetResponseString(string text)
    {
        var httpClient = new HttpClient();

        var parameters = new Dictionary<string, string>();
        parameters["text"] = text;

        var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
        var contents = await response.Content.ReadAsStringAsync();

        return contents;
    }

我用这种方法称呼它。...

And I call it from this method....

 string AnalyzeSingle(string text)
    {
        try
        {
            Task<string> result = GetResponseString(text);
            var model = JsonConvert.DeserializeObject<SentimentJsonModel>(result.Result);

            if (Convert.ToInt16(model.pos) == 1)
            {
                _numRetries = 0;
                return "positive";
            }

            if (Convert.ToInt16(model.neg) == 1)
            {
                _numRetries = 0;
                return "negative";
            }

            if (Convert.ToInt16(model.mid) == 1)
            {
                _numRetries = 0;
                return "neutral";
            }

            return "";
        }
        catch (Exception e)
        {
            if (_numRetries > 3)
            {
                LogThis(string.Format("Exception caught [{0}] .... skipping", e.Message));
                _numRetries = 0;
                return "";
            }
            _numRetries++;
            return AnalyzeSingle(text);
        }
    }

它会一直运行,直到命中
var model = JsonConvert.DeserializeObject< SentimentJsonModel>(result.Result);
一次,它会继续运行而不会在另一个断点处停止。

And it keeps running forever, It hits the line var model = JsonConvert.DeserializeObject<SentimentJsonModel>(result.Result);Once, and it continues to go without stopping at another breakpoint.

当我暂停执行时,它说

。我继续执行,但它永远运行。不确定问题出在哪里

.. I Continue execution, but it just runs forever. Not sure what the problem is

推荐答案

您使用await / async的方式充其量是很差的,这使得很难跟随。您正在将 await Task’1.Result 混合在一起,这很令人困惑。但是,看起来您正在查看的是最终任务结果,而不是内容。

The way you are using await/async is poor at best, and it makes it hard to follow. You are mixing await with Task'1.Result, which is just confusing. However, it looks like you are looking at a final task result, rather than the contents.

我已经重写了您的函数和函数调用,这应该可以解决您的问题:

I've rewritten your function and function call, which should fix your issue:

async Task<string> GetResponseString(string text)
{
    var httpClient = new HttpClient();

    var parameters = new Dictionary<string, string>();
    parameters["text"] = text;

    var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
    var contents = await response.Content.ReadAsStringAsync();

    return contents;
}

最后一个函数调用:

Task<string> result = GetResponseString(text);
var finalResult = result.Result;

甚至更好:

var finalResult = await GetResponseString(text);

这篇关于如何从httpclient调用中获取内容主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:42