mscorlib.dll中发生"System.Net.Http.HttpRequestException",但未在用户代码中处理 [英] 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll but was not handled in user code

查看:157
本文介绍了mscorlib.dll中发生"System.Net.Http.HttpRequestException",但未在用户代码中处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发Windows窗体应用程序,以向Azure AD验证Windows窗体应用程序.

We are developing the windows forms application to authenticate a windows forms application to a Azure AD.

我遇到了两个问题,在线资源似乎都已过时.

I have a couple of issues and the resources online are all seems to be outdated.

正如下面的链接所建议的,我创建了一个webapi和本机客户端api和Windows窗体应用程序.

As suggested by the below link I have created a webapi and native client api and windows forms application.

在Web api中,我更新了清单文件,如下所示

In web api I have updated the manifest files as follows

"oauth2Permissions":[

"oauth2Permissions": [

{

  "adminConsentDescription": "Allow the application to access ftpwebapi on behalf of the signed-in user.",

  "adminConsentDisplayName": "Access ftpwebapi",

  "id": "4eebeb18-aee6-408a-bea1-c090766dce23",

  "isEnabled": true,

  "origin": "Application",

  "type": "User",

  "userConsentDescription": "Allow the application to access ftpwebapi on your behalf.",

  "userConsentDisplayName": "Access ftpwebapi",

  "value": "user_impersonation"

}

],

并在密钥下添加了我添加了2年的条目,并将webapi添加到了本机客户端应用程序.

And added under keys I have added an entry for 2 years and added the webapi to the native client application.

在Windows窗体中,我在按钮单击事件中添加了以下代码.

In the windows forms I have added the below code in the button click event.

private async void button1_Click(对象发送者,EventArgs e)

private async void button1_Click(object sender, EventArgs e)

    {

        string authority = "https://login.windows.net/****";

        string resourceURI = "https://***.com/WebApplication3";

        string clientID = "********-5815-4358-****-*********";

        Uri returnURI = new Uri("http://********.com");

        AuthenticationContext authContext =

            new AuthenticationContext(authority);

        AuthenticationResult authResult =

            authContext.AcquireToken(resourceURI, clientID, returnURI);

        string authHeader = authResult.CreateAuthorizationHeader();

        // don't do this in prod

        System.Net.ServicePointManager.ServerCertificateValidationCallback =

            ((s, c, c2, se) => true);

        HttpClient client = new HttpClient();

        HttpRequestMessage request =

            new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/");

        request.Headers.TryAddWithoutValidation("Authorization", authHeader);

        var response = await client.SendAsync(request);

        string responseString = await response.Content.ReadAsStringAsync();

        MessageBox.Show(responseString);

}

到目前为止是否正确?

我正在夸大一个错误

var response =等待客户端.SendAsync(request);

var response = await client.SendAsync(request);

mscorlib.dll中发生类型'System.Net.Http.HttpRequestException'的异常,但未在用户代码中处理

An exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll but was not handled in user code

其他信息:发送请求时发生错误.

Additional information: An error occurred while sending the request.

推荐答案

我在正确设置这些变量后运行您的代码,起初我遇到相同的错误.然后在httpRequestMessage request =new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/")中更改我的requesturl.

I run your code after setting these variables properly, at first I get the same error. Then I change my requesturl in httpRequestMessage request =new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/").

现在它可以成功运行,请确保您的URL https://localhost:***/task/api/是正确的,再次检查localhost:***中的端口.

Now it runs successfully, make sure your url https://localhost:***/task/api/ is right, double check your Port in localhost:***.

请阅读一些教程,以从

Please read some tutorials to get more information from here.

这篇关于mscorlib.dll中发生"System.Net.Http.HttpRequestException",但未在用户代码中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆