C# HttpWebRequest returns 错误 403
C# HttpWebRequest returns error 403
我的应用程序的以下部分存在问题,returns 错误 403 作为 HttpWebRequest 的响应。你能告诉我为什么会出现这个错误吗?
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
抱歉,如果这没有提示答案;所以不让我评论。
您确定 webServiceServerName + uri
的计算结果为 a.k.a。 "APIURL.Domanin.com"?也许该代码恰好最终尝试与不是您所追求的服务器通信,该服务器具有另一组 authentication/authorization 规则(HTTP-403 表示 "forbidden")。
我的应用程序的以下部分存在问题,returns 错误 403 作为 HttpWebRequest 的响应。你能告诉我为什么会出现这个错误吗?
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
抱歉,如果这没有提示答案;所以不让我评论。
您确定 webServiceServerName + uri
的计算结果为 a.k.a。 "APIURL.Domanin.com"?也许该代码恰好最终尝试与不是您所追求的服务器通信,该服务器具有另一组 authentication/authorization 规则(HTTP-403 表示 "forbidden")。