[英]EXE program calling aspx page gets: 401 - Unauthorized: Access is denied due to invalid credentials
I'm creating an executable program that uses HttpClient
to call a URL. 我正在创建一个使用HttpClient
调用URL的可执行程序。 I try using the DefaultNetworkCredentials
as I do not know the user id and password of the server. 我尝试使用DefaultNetworkCredentials
因为我不知道服务器的用户ID和密码。 The following code runs successfully on my local machine, but it will get error when deployed to the server. 以下代码在我的本地计算机上成功运行,但是在部署到服务器时会出错。
My code: 我的代码:
HttpClientHandler authtHandler = new HttpClientHandler()
{
Credentials = CredentialCache.DefaultNetworkCredentials
};
// Call the URL
using (HttpClient httpClient = new HttpClient(authtHandler))
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("testingkey", "testingvalue")
});
// Make it sync instead of async by using .Result
var result = httpClient.PostAsync(url, formContent).Result;
// Get the content, for verification
var content = result.Content;
}
DefaultNetworkCredentials is the authentication credentials for the current security context in which the application is running. DefaultNetworkCredentials是运行应用程序的当前安全上下文的身份验证凭据。 For client side application, these are usually the Windows credentials of the user who is running the application. 对于客户端应用程序,这些通常是运行该应用程序的用户的Windows凭据。
If the user does not have rights over the URL over what you are using the HttpClient then error will come. 如果用户没有使用HttpClient的URL的权限,则将出现错误。 To make it work you 为了让你工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.