WCF Rest发出http请求:由于身份验证错误,序列已关闭

时间:2017-12-01 06:32:20

标签: c# .net rest api wcf

我的WCF Rest向另一个Rest Api发出请求,但它返回以下错误:

  

System.Net.WebException:终止连接:意外发送   错误。 ---> System.IO.IOException:身份验证失败,因为   远程方关闭了传输流。 \ R \ n in   System.Net.Security.SslState.StartReadFrame(Byte []缓冲区,Int32   readBytes,AsyncProtocolRequest asyncRequest)\ r \ n in   System.Net.Security.SslState.StartReceiveBlob(Byte []缓冲区,   AsyncProtocolRequest asyncRequest)\ r \ n in   System.Net.Security.SslState.CheckCompletionBeforeNextReceive   (ProtocolToken消息,AsyncProtocolRequest asyncRequest)\ r \ n in   系统。 Net.Security.SslState.StartSendBlob(字节[]传入,Int32   count,AsyncProtocolRequest asyncRequest)\ r \ n in   System.Net.Security.SslState.ForceAuthentication(布尔值   receiveFirst,Byte [] buffer,AsyncProtocolRequest asyncRequest)\ r \   n System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult   lazyResult)\ r \ n在System.Net.TlsStream.CallProcessAuthentication中   (对象状态)\ r \ n in   System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,B oolean   在System.Threading.ExecutionContext.Run中保留了SyncCtx)\ r \ n   (ExecutionContext executionContext,ContextCallback回调,Object   state,Boolean preserveSyncCtx)\ r \ n in   System.Threading.ExecutionContext.Run(ExecutionContext   executionContext,ContextCallback回调,对象状态)\ r \ n in   System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)\   r \ n在System.Net.TlsStream.Write(Byte []缓冲区,Int32偏移量,   System32.PooledStream .Write(Byte []缓冲区中的Int32大小)\ r \ n,   Int32偏移,Int32大小)\ r \ n in   System.Net.ConnectStream.WriteHeaders(布尔异步)\ r \ n ---结束   内部异常的堆栈跟踪--- \ r \ n in   System.Net.WebClient.UploadDataInternal(Uri地址,String方法,   字节[]数据,WebRequest&请求)\ r \ n in   System.Net.WebClient.UploadString(Uri地址,String方法,字符串   数据)\ r \ n在BS.Control.Save.Send(String body,String to)

我所拥有的函数是在一个名为BS的库c#中,在“发送”类中,在这里我这样做:

json= {"user":"test","pass":"12345","message":"test 1","number":"12345678"}

using (var WC = new WebClient())
{
    JObject response = new JObject();
    ResponseD Result = new ResponseD();
    try
    {
        WC.Headers.Add(HttpRequestHeader.Authorization,"Bearer MyCode");
        WC.Headers.Add(HttpRequestHeader.ContentType,"application/json");
        string baseurl = "url-where receive the post";
        response = JObject.Parse(WC.UploadString(new Uri(baseurl), "POST", json));
    }
    catch(Exception ex)
    {
        Result.id = "Error";
        Result.result = ex.ToString();
    }  
} 

此异常存在于我的本地PC,我的IIS服务器甚至是Azure APP服务中......在我的IIS服务器中,我将SSL配置到我拥有WCF的位置,但是使用SSL我的WCF继续使用相同的问题并没有发送回复...

我做了一个控制台应用程序,我使用与我的WCF Rest相同的库,在这里没有问题,“response = JObject.Parse(WC.UploadString(new Uri(baseurl),”POST“,json ));”接收答案没有问题....收到一个美丽的Json与分配给请求的id的信息,时间发布等,但这是我唯一需要的是ID和从ConsoleApp一切正常....

响应的Json是:

{
"id": "Error",
    "result":
 the exception mentioned above and the location in Line where the problem is located and this is where the WC.uploadstring is
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

OP解决方案。

事实证明,托管Rest服务的服务器与TLS1.0不兼容。

所以我在uploadstring之前添加了这段代码,它在我的IIS服务器上完美运行。

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