问题:System.Net.WebException:基础连接已关闭:接收时发生意外错误。 System.NullReferenceException

时间:2018-05-24 13:54:31

标签: c# httpwebrequest webrequest

我遇到了一个问题:

  

System.Net.WebException:底层连接已关闭:An   接收时发生意外错误。 System.NullReferenceException:   对象引用未设置为对象的实例。

我在某些Windows 10环境中遇到此问题。相同的代码适用于其他环境。

我从下面的行收到此错误 response =(HttpWebResponse)request.GetResponse();

下面是详细的堆栈跟踪:

{System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at Microsoft.SharePoint.Administration.SPServer.get_RegistryEncodedServerId()
       at Microsoft.SharePoint.Utilities.SPMonitoredScope.PushParent()
       at Microsoft.SharePoint.Utilities.SPMonitoredScope..ctor(Boolean clearCorrelationId, String name, TraceSeverity traceLevel, ISPScopedPerformanceMonitor[] monitors)
       at Microsoft.SharePoint.SPCertificateValidator.SPImmutableCertificateValidator.Validate(X509Certificate2 certificate)
       at Microsoft.SharePoint.SPCertificateValidator.Validate(X509Certificate2 certificate)
       at Microsoft.SharePoint.SPCertificateValidator.IsValid(X509Certificate2 certificate)
       at System.Net.Security.RemoteCertificateValidationCallback.Invoke(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
       at System.Net.ServerCertValidationCallback.Callback(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.ServerCertValidationCallback.Invoke(Object request, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
       at System.Net.Security.SecureChannel.VerifyRemoteCertificate(RemoteCertValidationCallback remoteCertValidationCallback)
       at System.Net.Security.SslState.CompleteHandshake()
       at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
       at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.ConnectStream.WriteHeaders(Boolean async)
       --- End of inner exception stack trace ---

代码是:

     public static string SendMessage(SharePointAdapter adr, string uri, Stream postStream, string contentType)
        {
        string responseText = null;
        Stream requestStream = null;
        System.Net.WebRequest request = null;
        byte[] buffer = null;
        HttpWebResponse response = null;
        Stream receiveStream = null;
        StreamReader readStream = null;

        try
        {      
            //Create a request
            request = WebRequest.Create(uri);
            request.Timeout = 180000;
            request.Method = "POST";
            request.ContentType = contentType;
            request.Headers.Add("X-Vermeer-Content-Type", "application/x-www-form-urlencoded");
            request.Credentials = adr.Credentials.NetworkCredentials;
            HttpWebRequest httprequest = request as HttpWebRequest;
            httprequest.KeepAlive = false;
            adr.certificates.CopyCertificatesToCollection(httprequest.ClientCertificates);

            if (adr.AdapterProxy != null)
            {
                request.Proxy = adr.AdapterProxy;
            }       
            httprequest.CookieContainer = new CookieContainer();
            adr.CookieManager.AddCookiesTo(httprequest.CookieContainer);

            ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072 | (SecurityProtocolType)48;

            requestStream = request.GetRequestStream();
            int iRead = 1;
            int iBufferSize = 4096;
            buffer = new byte[iBufferSize];

            //Go to the beginning of the stream
            postStream.Seek(0, SeekOrigin.Begin);

            //Read from the post stream to the request stream
            while (iRead > 0)
            {
                iRead = postStream.Read(buffer, 0, iBufferSize);
                requestStream.Write(buffer, 0, iRead);
            }
            requestStream.Close();
            response = (HttpWebResponse)request.GetResponse();
            receiveStream = response.GetResponseStream();
            readStream = new StreamReader(receiveStream, Encoding.UTF8);
            responseText = readStream.ReadToEnd();
            response.Close();
            readStream.Close();

        }           
        finally
        {
           requestStream = null;
            request = null;
            buffer = null;
            response = null;
            receiveStream = null;
            readStream = null;
            System.GC.Collect();
        }
        return responseText;
        }

1 个答案:

答案 0 :(得分:0)

System.NullReferenceException不是很具体。很难找到问题,特别是因为你的方法中的所有内容都在一个大的try / catch块中。

仅在需要的块上使用try/catch块,避免使用只有一个通用try / catch的大方法。有关详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions

我强烈建议您使用HttpClient代替WebRequestHttpClient类可以在.Net Framework 4.5.2+上使用,以下是一些详细的说明以及使用它的原因:Deciding between HttpClient and WebClient

此外,不是手动操作流,而是使用StreamContent

发送
client.PostAsync (url, new StreamContent(stream));

在您的代码上,您应该使用IDisposable功能(使用using)来确保关闭和处理流和其他非托管资源。例如:

using (var client = new HttpClient())
{
}

最后,避免直接操作GC,因为它会引起副作用,特别是对性能的影响。 GC只应在非常具体的情况下进行操作。有关https://msdn.microsoft.com/en-us/library/ms973837.aspx

的更多信息,请参阅

这些提示可能有助于减少未来的问题,提高代码的可维护性和可读性(甚至可能会发现System.NullReferenceException问题)。