首页 > 解决方案 > NullReferenceException 异步等待

问题描述

我写了一个类来简化 HTTP POST 请求。但我在这里得到一个 NullReferenceException :

    public async Task ResponseString() {
        m_responseString = await m_response.Content.ReadAsStringAsync();
    }

我用调试器检查了 m_response,它是 null :

    public async Task Response(string requestUri) {
        m_response = await m_client.PostAsync(requestUri, m_content);
    }

我认为这是同步的问题,这里是主要功能:

    static void Main(string[] args) {
        /*var values = new Dictionary<string, string> {
            { "thing1", "hello"},
            { "thing2", "world"}
        };

        var content = new FormUrlEncodedContent(values);

        var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

        var responseString = await response.Content.ReadAsStringAsync();*/

        HttpsRequestPostExample httpsRequestPostExample = new HttpsRequestPostExample();

        Dictionary<string, string> values = httpsRequestPostExample.Values(
                new Dictionary<string, string> {
                    { "thing1", "hello"},
                    { "thing2", "world"}
                });

        FormUrlEncodedContent content = httpsRequestPostExample.Content();
        Task response = httpsRequestPostExample.Response("http://www.example.com/recepticle.aspx");
        Task responseString = httpsRequestPostExample.ResponseString();
    }

ResponseString 在 Response 之后被调用。

标签: c#.net

解决方案


推荐阅读