如何在C#POST中为方法添加access-control-allow-methods

时间:2015-05-25 09:20:02

标签: c# silverlight post windows-phone-8.1

我正在尝试使用这个c#代码使用我的Windows Phone应用程序向一个java web服务发送POST:

 using (var client = new HttpClient())
 {
 client.DefaultRequestHeaders.Accept.Add(new 
                          MediaTypeWithQualityHeaderValue("application/json"));
var requestContent = new StringContent(json);
 requestContent.Headers.ContentType = new 
                      MediaTypeWithQualityHeaderValue("application/json");
var response = await client.PostAsync(requestUri, requestContent);
   //... 
 } 

但我收到400 Bad Request并发送此标题:

POST [myreq] HTTP/1.1
Content-Type: application/json
Content-Length: 340
Accept-Encoding: identity
Accept: application/json
User-Agent: NativeHost
Host: [myhost]
Connection: Keep-Alive
Pragma: no-cache

我从有效的类似(对于同一个Web服务)android java请求中看到的唯一区别是我的标题中的这一行:

访问控制 - 允许的方法= [POST]

如何在C#中包含此access-control-allow-methods?

1 个答案:

答案 0 :(得分:0)

以备将来帮助

client.DefaultRequestHeaders.Add("Access-Control-Allow-Methods", "POST");