简体   繁体   中英

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. System.Net.Http.HttpRequestMessage.Content.get returned null.

I don't know why is this happening.

Can anyone help me to correct my code?

HttpRequestMessage.Content.Headers. ContentType throws null reference exception in c#?

System.Net.Http api is used to cross-platform. And it could not support UWP completely. In UWP platform we suggest you use Windows.Web.Http namespace to replace.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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