Web请求-基础连接已关闭:连接意外关闭

时间:2020-07-15 16:37:20

标签: c# curl postman httpclient keycloak

当我在Curl中运行以下命令时,我得到了正确的预期结果:

curl -d "client_secret=VALUE" -d "client_id=VALUE" -d "username=VALUE" -d "password=VALUE" -d "grant_type=VALUE" "https://<host>/auth/realms/test-rai/protocol/openid-connect/token"

(当邮递员触发相同的请求时,结果也是正确的。)

但是,当我从C#触发(相同)请求时,我遇到了一个异常:

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace HttpClientPost
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var url = "https://<host>/auth/realms/test-rai/protocol/openid-connect/token";

            var values = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("client_secret", "VALUE"),
                new KeyValuePair<string, string>("client_id", "VALUE"),
                new KeyValuePair<string, string>("username", "VALUE"),
                new KeyValuePair<string, string>("password", "VALUE"),
                new KeyValuePair<string, string>("grant_type", "VALUE")
            };

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
                                                   SecurityProtocolType.Tls11 |
                                                   SecurityProtocolType.Tls12 |
                                                   SecurityProtocolType.Ssl3;

            using (var httpClient = new HttpClient())
            {
                using (var content = new FormUrlEncodedContent(values))
                {
                    content.Headers.Clear();
                    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                    // Throws exception: An error occurred while sending the request. The underlying connection was closed: The connection was closed unexpectedly.
                    HttpResponseMessage response = await httpClient.PostAsync(url, content);

                    var result = await response.Content.ReadAsStringAsync();
                }
            }
        }
    }
}

抛出的异常是:

发送请求时发生错误。基础连接已关闭:连接意外关闭。

在网上搜索时,我发现了很多关于设置“ ServicePointManager.SecurityProtocol”的参考。 (测试了所有选项。)

由于Curl和Postman都给出了正确/预期的结果:我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

我自己找到了答案:

由于我的请求在 Postman Curl 中按预期工作,因此我注意到Postman中有一个选项可以将Postman请求转换为特定语言:在“保存”按钮,您会找到一个“代码”按钮来实现此目的。

我生成的-并且运行良好-C#代码是:

static void Main(string[] args)
{
    var client = new RestClient("https://<host>/auth/realms/test-rai/protocol/openid-connect/token");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("grant_type", "password");
    request.AddParameter("client_id", "VALUE");
    request.AddParameter("username", "VALUE");
    request.AddParameter("password", "VALUE");
    request.AddParameter("client_secret", "VALUE");
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
}