【问题标题】:how to change dotnet core outgoing http request hostname from the default localhost如何从默认 localhost 更改 dotnet core 传出 http 请求主机名
【发布时间】:2022-09-29 09:53:23
【问题描述】:

我能够通过邮递员成功地将请求发送到沙箱,由提供商按照他们的规格给出(见下图)

请求成功(见下文)

为了做到这一点,除了各自的标头和参数(见图 2)之外,我必须添加一个 ssl/Tls 证书(.pfx),因为服务器需要 2 次握手,所以它需要 Ssl 客户端证书:

授权(见下文)。

标题(见下文)

身体(见下文)

现在,我正在尝试使用 dotnet core 6 以编程方式执行 ir,但我一直遇到同样的问题:

这是我的代码:

public static string GetAccessToken(IConfiguration _config)
    {
        string UserName = Environment.GetEnvironmentVariable(\"USER_NAME\");
        string Password = Environment.GetEnvironmentVariable(\"PASSWORD\");
        

        var client = new RestClient(\"https://connect2.xyz.com/auth/token\");
       
        var request = new RestRequest();
        X509Certificate2 FullChainCertificate = new X509Certificate2(\"Path/to/Cert/cert.pfx\", \"test\");
        client.Options.ClientCertificates = new X509CertificateCollection() { FullChainCertificate };
        client.Options.Proxy = new WebProxy(\"connect2.xyz.com\");
        var restrequest = new RestRequest();
        restrequest.Method = Method.Get;
        restrequest.AddHeader(\"Accept\", \"*/*\");
        restrequest.AddHeader(\"Cache-Control\", \"no-cache\");
        restrequest.AddHeader(\"Content-Type\", \"application/x-www-form-urlencoded\");
        restrequest.AddHeader(\"Authorization\", \"Basic \" + Convert.ToBase64String(Encoding.Default.GetBytes($\"{UserName}:{Password}\")));
        restrequest.AddParameter(\"grant_type\", \"client_credentials\");
        RestResponse response = client.Execute(restrequest);


        AccessTokenPointClickCare accessToken = JsonConvert.DeserializeObject<AccessTokenPointClickCare>(response.Content);
        string strToken = accessToken.access_token;

        return strToken;
    }

现在,正如错误似乎显示的那样,它与证书有关(显然),但我不知道代码中是否有问题,或者我是否遗漏了什么等等......

值得注意的是,这段代码确实在其他人的电脑上运行过同样的设置,但当然是用那个人自己的 pfx,但对于其余的,它本质上是一样的,更不用说那它做为我的邮递员工作。

最后,作为这个问题的标题,我认为它可能也会影响请求的唯一一件事是主机。如果我引用邮递员,有一个字段我必须放置服务器的主机名https://connect2.xyz.com/auth/token

    标签: .net-core ssl-certificate httprequest restsharp


    【解决方案1】:

    因此,通过更改为新的 Windows 10 使其工作。研究其他 Stackoverflow 线程找到了答案:.NET CORE 5 '''HandshakeFailure'" when making HTTPS request

    所以我得出结论,这与密码有关

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多