14 December 2011

The underlying connection was closed: An unexpected error occurred on a receive

When I tried to access an external server on the server side (not the organization) through the code (in my case web-part of sharepoint) I got an error:

The code is simple code:

public XmlDocument GetXmlData()
{
    XmlDocument document = new XmlDocument();
    //Now load the XML Document
    document.Load(this.url);// fall
    return document;
}

The error I got it:

   The underlying connection was closed: An unexpected error occurred on a receive.

   Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

   at System.Net.HttpWebRequest.GetResponse()
   at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials)
   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
   at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
   at System.Threading.CompressedStack.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
   at System.Xml.XmlTextReaderImpl.OpenUrl()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at System.Xml.XmlDocument.Load(String filename)
   ...

In Visual Studio it looks like this:
The underlying connection was closed: An unexpected error occurred on a receive

I tried to execute the code in another request.
Then in another Thread.
And almost before I gave up, I tried to do impersonation with another user.
Nothing works....

Then I realized the problem - is the problem of information security.
I defined proxy in web.config - and cheers .... It works

Setting in web.config

  <system.net>
    <!--proxyaddress is the url address of the Company with the port-->

     <defaultProxy>
       <proxy usesystemdefault="True" proxyaddress="http://myporxy:port" bypassonlocal="True" />
     </defaultProxy>
   </system.net>

Hope I helped,
Roi

No comments:

Post a Comment