简体   繁体   English

为什么 HttpRequestMessage.Content.Headers.ContentType 会抛出 null 引用异常?

[英]Why does HttpRequestMessage.Content.Headers.ContentType throw null reference exception?

Here is my code:这是我的代码:

 string bound = "----------------------------" +DateTime.Now.Ticks.ToString("x");
 HttpRequestMessage httpreq = new HttpRequestMessage();
 httpreq.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data; boundary=" + bound);

which throws the following exception:引发以下异常:

Object reference not set to an instance of an object. Object 引用未设置为 object 的实例。 System.Net.Http.HttpRequestMessage.Content.get returned null. System.Net.Http.HttpRequestMessage.Content.get 返回 null。

I don't know why is this happening.我不知道为什么会这样。

Can anyone help me to correct my code?谁能帮我更正我的代码?

HttpRequestMessage.Content.Headers. HttpRequestMessage.Content.Headers。 ContentType throws null reference exception in c#? ContentType在 c# 中抛出 null 引用异常?

System.Net.Http api is used to cross-platform. System.Net.Http api用于跨平台。 And it could not support UWP completely.而且它不能完全支持 UWP。 In UWP platform we suggest you use Windows.Web.Http namespace to replace.在 UWP 平台中,我们建议您使用Windows.Web.Http命名空间来替换。

using Windows.Web.Http;


string bound = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpRequestMessage httpreq = new HttpRequestMessage();
httpreq.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("multipart/form-data; boundary=" + bound);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM