By Bill


2009-10-22 16:42:24 8 Comments

How do access a secure website within a sharepoint webpart? The following code works fine as a console application but if you run it in a webpart, you will get a access violation

WebRequest request = WebRequest.Create("https://somesecuresite.com");
WebResponse firstResponse = null;
try 
{
    firstResponse = request.GetResponse();  
}  
catch (WebException ex)  
{ 

    writer.WriteLine("Error: " + ex.ToString());
    return;  
}

if you access a non secure site, it also works. Any ideas?

Error: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> 
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
 at System.Net.UnsafeNclNativeMethods.NativePKI.CertVerifyCertificateChainPolicy(IntPtr policy, SafeFreeCertChain chainContext, ChainPolicyParameter& cpp, ChainPolicyStatus& ps)
 at System.Net.PolicyWrapper.VerifyChainPolicy(SafeFreeCertChain chainContext, ChainPolicyParameter& cpp)
 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.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
 at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
 at System.Net.TlsStream.CallProcessAuthentication(Object state)
 at System.Threading.ExecutionContext.runTryCode(Object userData)
 at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
 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.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
 at System.Net.ConnectStream.WriteHeaders(Boolean async) --- End of inner exception stack trace ---
 at System.Net.HttpWebRequest.GetResponse()

1 comments

@Chris Jones 2009-10-22 23:37:11

i once had this problem and solved it by overiding the GetWebRequest method: see here:

http://p2p.wrox.com/net-web-services/4586-underlying-connection-closed.html

Related Questions

Sponsored Content

1 Answered Questions

WCF Rest making a http request: sequence closed due to authentication error

  • 2017-12-01 06:32:20
  • samuel Sai
  • 47 View
  • 0 Score
  • 1 Answer
  • Tags:   c# .net rest api wcf

3 Answered Questions

[SOLVED] System.IO.IOException: The handshake failed due to an unexpected > packet format?

  • 2011-03-03 09:21:56
  • TheLearner
  • 35334 View
  • 18 Score
  • 3 Answer
  • Tags:   c# wcf

0 Answered Questions

2 Answered Questions

0 Answered Questions

PowerShell + HttpWebRequest throws exception

2 Answered Questions

[SOLVED] Accessing Webservice which is hosted on https server

2 Answered Questions

[SOLVED] Error in Sharepoint web part

Sponsored Content