У меня возникает проблема:
System.Net.WebException: базовое соединение было закрыто: при получении произошла непредвиденная ошибка.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;
}