У меня проблема со следующим разделом моего приложения, которое возвращает ошибку 403 в качестве ответа HttpWebRequest. Не могли бы вы сообщить мне, почему эта ошибка?C# HttpWebRequest возвращает ошибку 403
string url = "http://" + webServiceServerName + uri + "?extendedInfo=2";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/json";
//Failed tries to fix this error 403, non of these fix the issue
request.UserAgent = "[AnyWordThatIsMoreThan5Char]";
request.UseDefaultCredentials = true;
request.Accept = "*/*";
//Get the headers associated with the request.
WebHeaderCollection myWebHeaderCollection = request.Headers;
//Add Custom header fields
myWebHeaderCollection.Add("api-version","1.0");
myWebHeaderCollection.Add("auth-key","XYZ");
// Gets the stream associated with the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// response contains error 403
Однако, когда я посылаю такой же запрос в Powershell, используя следующие команды, я получаю ответ успеха:
> $headers = @{"api-version"="1.0"; "auth-key"="XYZ"}
> Invoke-RestMethod -Uri "http://APIURL.Domanin.com?extendedinfo=2" -Headers $header -ContentType "application/json" -Method Get
Service : Configured
Version : 6.1.0.1
Transfer Database : Available
Logging Database : Available
Client Database : Configured
Fileshare : Available
Возможный дубликат [httpwebrequest-error-403] (http://stackoverflow.com/questions/3231674/httpwebrequest-error-403) – esiprogrammer