c# - 使用 Web 客户端的 HTTP 请求。随机错误(验证失败,因为对方已关闭传输流。)

标签 c# angularjs error-handling umbraco

嘿,你好,我正在使用带有 Umbraco 的 MVC 使用 atm,并且我正在尝试使用 webclient http 协议(protocol)进行 API 调用。

这是我正在做的请求。

[HttpPost]
public JObject CheckAuth()
{

    string URI = "https://nedfinity.api.accelo.com/oauth2/v0/token";
    string myParameters = "grant_type=client_credentials&scope=read(all)";

    var clientId = "this is a secret";
    var clientSecret = "this is a secret";

    var byteArray = new UTF8Encoding().GetBytes(clientId + ":" + clientSecret);

    using (WebClient wc = new WebClient())
    {
        wc.UseDefaultCredentials = true;
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        wc.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(byteArray);
        string HtmlResult = wc.UploadString(URI, myParameters);
        JObject json = JObject.Parse(HtmlResult);
        return json;
    }
}

它过去一直工作得很好,但是当我编辑 CSS 时它突然停止工作。我试图扭转我在 bitbucket 中的所有提交,但没有修复。
此外,当尝试从 accelo 调用 api 时,它的工作正常。
这是我从服务器获得的唯一反馈:
data
:
Object
ExceptionMessage
:
"Verification failed because the other party has closed the transport stream." 
ExceptionType
:
"System.IO.IOException"
Message
:
"An error has occurred."
StackTrace
:
"   bij System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
↵   bij System.Net.TlsStream.CallProcessAuthentication(Object state)
↵   bij System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
↵   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
↵   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
↵   bij System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
↵   bij System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
↵   bij System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
↵   bij System.Net.ConnectStream.WriteHeaders(Boolean async)"

任何人都知道什么会导致这个问题以及如何解决它或在哪里寻找?

我在想我自己这与一些配置设置或网络权限有关,但它在本地主机上,我在这段时间没有更改任何可能导致这种情况的东西。

附言我希望这种提出问题的正确方法是否做错了,请随时指出我。

最佳答案

您可以尝试将安全协议(protocol)设置为以下内容:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

大多数 API 现在使用 TLS12 作为默认协议(protocol)。

让我知道事情的后续。

PS。您可以在现有的方法 block 中添加它。

问候

克雷格

关于c# - 使用 Web 客户端的 HTTP 请求。随机错误(验证失败,因为对方已关闭传输流。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44196591/

相关文章:

javascript - angular ui-router 多个嵌套和 Controller 数据

c# - 在 C# 中访问函数返回类型作为元组

c# - IsPostBack Condition 的作用是什么?

c# - 将字典键设置为对象类型

css - 无法将 Bower 组件 css 添加到 Rails 4.2 中的 Assets 管道中

javascript - 如何让 Angular 打开一个按钮并关闭另一个按钮?

php - 如何使用骨架项目 'Application' 模块中的错误处理程序来捕获事件回调中的错误?

java - 我不明白为什么这段代码对int错误无效

magento - 在Magento中过滤网格集合时,左联接引发错误

c# - 并发文件.移动同一文件