无法发送HTTPS请求

时间:2018-08-13 10:58:23

标签: c# .net https .net-core

我必须发送HTTPS POST请求。 我创建了一个库类来发送HTTPS POST请求。 我有2个程序调用此库类:

  • 我的主程序(IdentityServer)
  • 我的考试班

两个程序都只使用给定参数调用库类(将在POST正文上使用),然后库类执行请求。

问题是,当在我的测试类上运行它时,它可以正常工作。但是,对于IdentityServer,它会引发以下错误“ System.Net.Http.WinHttpException”。

我注意到测试类和IdentityServer之间的唯一区别是它们的“目标框架”:

  • IdentityServer(.NET Core 2.0)
  • 测试类(.NET Framework 4.6.1)
  • 类库(.NET Framework 4.6.1)

这是我发送请求的简单代码:

public static async Task<HttpStatusCode> Send(String destination, String message, String accountUsername, String accountPassword, String senderPhoneNumber)
    {

        var values = new Dictionary<string, string>
        {
            { "username", accountUsername },
            { "password", accountPassword },
            { "src", senderPhoneNumber },
            { "dst", destination },
            { "text", message },
            { "srr", "3" }
        };

        var content = new FormUrlEncodedContent(values);

        HttpStatusCode responseStatus = HttpStatusCode.NotAcceptable;

        try
        {
            HttpClient client = new HttpClient();

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

            ServicePointManager
                .ServerCertificateValidationCallback +=
                (sender, cert, chain, sslPolicyErrors) => true;

            var response = await client.PostAsync("TARGET_URL", content);
            var responseString = await response.Content.ReadAsStringAsync();
            responseStatus = response.StatusCode;
        }
        catch (WebException we)
        {
            string error = we.ToString();
        }
        catch (Exception e)
        {
            string error = e.ToString();
        }
        return responseStatus;
    }

我忽略了来自目标服务器的证书,因为它们的证书无效。

但是,正如我所说的,用我的测试类调用它就可以了。

从IdentityServer调用时引发的完整错误如下:

    System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.DiagnosticsHandler.<SendAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at VdfnHungSMSLibrary.VdfnHungSMSProvider.<Send>d__0.MoveNext() in D:\Projects\IdentityServer\Current\src\VdfnHungSMSLibrary\VdfnHungSMSProvider.cs:line 43

0 个答案:

没有答案