无法设置Content-Type windows手机应用程序的标题

时间:2014-09-25 15:23:53

标签: c# httprequest httpclient windows-phone-8.1 win-universal-app

我正在使用HttpClient创建HTTP请求,该客户端来自程序集Windows.Web.Http

在没有Content-Type Header的情况下发布请求时,一切都很好但是服务器没有返回我需要的东西,因为它需要那个头,因此在找到需要发送的正确头文后我面临另一个问题......我无法设置Content-Type标头

这是我的代码(其中try块是错误所在的位置)

using (var wp = new Windows.Web.Http.HttpClient())
            {

                HttpRequestMessage mSent = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
                //mSent.Headers.Add("Host", "academicos.ubi.pt");
                //mSent.Headers.Add("Connection", "keep-alive");
                //mSent.Headers.Add("Content-Length", "18532");
                //mSent.Headers.Add("Origin", "https://academicos.ubi.pt");
                //mSent.Headers.Add("X-Requested-With", "XMLHttpRequest");
                //mSent.Headers.Add("Cache-Control", "no-cache");
                //mSent.Headers.Add("X-MicrosoftAjax", "Delta=True");
                mSent.Headers.Add("User-Agent", " Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
                try
                {
                    mSent.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
                }
                catch (Exception ex) { ex.ToString(); }
                //mSent.Headers.Add("Accept", "*/*");
                //mSent.Headers.Add("Referer", "https://academicos.ubi.pt/online/horarios.aspx");
                //mSent.Headers.Add("Accept-Encoding", "gzip,deflate");
                //mSent.Headers.Add("Accept-Language", "pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4");
                mSent.Headers.Add("Cookie", "the cookie string is big, so I will not post it here");
                mSent.Content = new HttpStringContent("the content is well defined, but I will not post it here it's huge"), Windows.Storage.Streams.UnicodeEncoding.Utf8);

                HttpResponseMessage mReceived = await wp.SendRequestAsync(mSent, HttpCompletionOption.ResponseContentRead);

                if (mReceived.IsSuccessStatusCode)
                {
                    htmlPage = await mReceived.Content.ReadAsStringAsync();
                }
            }

我收到的错误是对象引用未设置为对象的实例。 我尝试设置这个标题,就像我设置了用户代理一样,它给了我另一个例外,即设置我需要在内容标题下设置它的内容类型......

任何想法?我试着寻找这个问题的答案,到目前为止我空手而归

2 个答案:

答案 0 :(得分:2)

佩德罗,你需要将Content属性设置为某种东西,例如:

HttpRequestMessage mSent = new HttpRequestMessage(
    HttpMethod.Post,
    new Uri(url));
mSent.Content = new HttpStringContent(
    "Name=Jonathan+Doe&Age=23",
    UnicodeEncoding.Utf8,
    "application/x-www-form-urlencoded");

还有其他类型的IHttpContent,例如HttpBufferContent, HttpFormUrlEncodedContent, HttpMultipartContent, HttpMultipartFormDataContent and HttpStreamContent

答案 1 :(得分:0)

您没有在.Content成员上设置标头。您可以改为使用Multi-Part Content对象并设置与此类似的内容:   HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();                 form.Add(new HttpStringContent(RequestBodyField.Text)," data");

            HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, form).AsTask(cts.Token); 

参考:http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664/sourcecode?fileId=98924&pathId=1116044733