c# - 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 之后被调用。
解决方案
推荐阅读
- postgresql - Prometheus postgres 导出器未显示带有单引号的间隔时间的指标
- r - 通过ggplot中连续变量的中位数重新排序因子水平
- angular - 在 Angular 中正确重置模板驱动的表单
- laravel-8 - 如何通过使用 tinymce wiris 插件获取图像 url?
- r - 如何在R中对直方图中的条形重新排序?
- amazon-web-services - 从 Internet 访问 lxc Kubernetes 集群
- jsp - 如何为在servlet中返回数字并将其传递给jsp文件的方法设置参数?
- c# - 在应用程序中侦听保存文件对话框窗口,然后自动保存它?
- angular8 - 角度 - 不能总结点
- amazon-web-services - 我的域在 AWS Certificate Manager 中等待验证