我正在使用 c# 对网址进行 Web 请求
这是原始的 curl 请求
curl 'http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=808593890516' -H 'Cookie:shippingCountry=US;' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.8' -H 'Accept: application/json, text/javascript, */*; q=0.01' --compressed
我为此编写了一个 c# 代码
public string variations_curl ()
{
var request = (HttpWebRequest)WebRequest.Create("http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=5045493369123");
//request.Referer = "http://www.youtube.com/"; // optional
request.Method = WebRequestMethods.Http.Post;
//request.Headers["origin"] = "http://www1.bloomingdales.com";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers["Accept-Language"] = "en-US,en;q=0.8";
request.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36";
//request.ContentType = "application/x-www-form-urlencoded";
request.Headers ["Cookie"] = "shippingCountry=US;";
request.Accept = "application/json";
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var html = reader.ReadToEnd();
return html;
}
}
但是上面的代码抛出了一些错误
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (411) Length Required.
如何修复此错误并使程序正常工作?
只需添加到请求方法e
request.ContentLength = 0;
这对我有用