In SOAP Request, "Underlying connection was closed" error

  • Thread starter Thread starter Brent
  • Start date Start date
B

Brent

For the life of me, I cannot understand why I'm getting this error
constantly with the ASP.Net 1.1 code below:

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

I've tried just about everything I know how to do. My best guess is
that the error is somehow related to an SSL problem, or perhaps a
certificate store policy, while working on a Windows Vista machine.
Could it be that Vista blocks connections differently than XP?
Whatever the case, I have tried various fixes for these problems with
no success.

The code here is complete and ready to paste into a .Net 1.1 console
app. The dummy username and password has no effect on the error I
receive.

Thanks for any help you can give.

--Brent

=======================================================
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;

namespace CallReportsConsole
{
class MainClass
{
public static void Main(string[] args)
{
try
{
string username = "username";
string password = "password";

//Create SOAP XML

StringBuilder sbSoap = new StringBuilder();

//1. Build SOAP string
sbSoap.Append("<? xml version=\"1.0\" encoding=\"utf-8\" ?>");
sbSoap.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/
XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sbSoap.Append("<soap:Body>");
sbSoap.Append("<TestUserAccess xmlns=\"http://cdr.ffiec.gov/public/
services\">\n");
sbSoap.Append("<user>" + username + "</user>\n");
sbSoap.Append("<password>" + password + "</password>\n");
sbSoap.Append("</TestUserAccess>\n");
sbSoap.Append("</soap:Body></soap:Envelope>");

string soapAction = "http://cdr.ffiec.gov/public/services/
TestUserAccess";

//2. Send XML to CDR server
string cdrURL = "https://cdr.ffiec.gov/pws/
retrievalservice.asmx";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cdrURL);

req.ContentType = "text/xml; charset=utf-8";
req.Accept = "text/xml";
req.Method = "POST";
req.ContentLength = sbSoap.Length;
req.Headers.Add("SOAPAction", soapAction);
req.KeepAlive = false;

Stream strm = req.GetRequestStream();
byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(sbSoap.ToString());
strm.Write(bytes, 0, bytes.Length);
strm.Close();

//3. Get response & send back filename
HttpWebResponse resp =
(HttpWebResponse)req.GetResponse();

StreamReader r = new
StreamReader(resp.GetResponseStream());

XmlTextReader xmlRead = new XmlTextReader(r);

while (xmlRead.Read())
{
Console.Write(xmlRead.Value.ToString());
}

r.Close();
strm.Close();

}
catch (WebException wex)
{
Console.Write(wex.ToString());
Console.Write(wex.Message.ToString());
}
catch (Exception ex)
{
Console.Write(ex.ToString());
Console.Write(ex.Message.ToString());
}
}
}
}
 
Brent,

Your XML declaration is invalid. You have:

<? xml version="1.0" encoding="utf-8" ?>

You are using whitespace that is not allowed in the XML preprocessor
directive. It should be:

<?xml version="1.0" encoding="utf-8"?>

Then it works.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brent said:
For the life of me, I cannot understand why I'm getting this error
constantly with the ASP.Net 1.1 code below:

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

I've tried just about everything I know how to do. My best guess is
that the error is somehow related to an SSL problem, or perhaps a
certificate store policy, while working on a Windows Vista machine.
Could it be that Vista blocks connections differently than XP?
Whatever the case, I have tried various fixes for these problems with
no success.

The code here is complete and ready to paste into a .Net 1.1 console
app. The dummy username and password has no effect on the error I
receive.

Thanks for any help you can give.

--Brent

=======================================================
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;

namespace CallReportsConsole
{
class MainClass
{
public static void Main(string[] args)
{
try
{
string username = "username";
string password = "password";

//Create SOAP XML

StringBuilder sbSoap = new StringBuilder();

//1. Build SOAP string
sbSoap.Append("<? xml version=\"1.0\" encoding=\"utf-8\" ?>");
sbSoap.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/
XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sbSoap.Append("<soap:Body>");
sbSoap.Append("<TestUserAccess xmlns=\"http://cdr.ffiec.gov/public/
services\">\n");
sbSoap.Append("<user>" + username + "</user>\n");
sbSoap.Append("<password>" + password + "</password>\n");
sbSoap.Append("</TestUserAccess>\n");
sbSoap.Append("</soap:Body></soap:Envelope>");

string soapAction = "http://cdr.ffiec.gov/public/services/
TestUserAccess";

//2. Send XML to CDR server
string cdrURL = "https://cdr.ffiec.gov/pws/
retrievalservice.asmx";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cdrURL);

req.ContentType = "text/xml; charset=utf-8";
req.Accept = "text/xml";
req.Method = "POST";
req.ContentLength = sbSoap.Length;
req.Headers.Add("SOAPAction", soapAction);
req.KeepAlive = false;

Stream strm = req.GetRequestStream();
byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(sbSoap.ToString());
strm.Write(bytes, 0, bytes.Length);
strm.Close();

//3. Get response & send back filename
HttpWebResponse resp =
(HttpWebResponse)req.GetResponse();

StreamReader r = new
StreamReader(resp.GetResponseStream());

XmlTextReader xmlRead = new XmlTextReader(r);

while (xmlRead.Read())
{
Console.Write(xmlRead.Value.ToString());
}

r.Close();
strm.Close();

}
catch (WebException wex)
{
Console.Write(wex.ToString());
Console.Write(wex.Message.ToString());
}
catch (Exception ex)
{
Console.Write(ex.ToString());
Console.Write(ex.Message.ToString());
}
}
}
}
 
Brent said:
For the life of me, I cannot understand why I'm getting this error
constantly with the ASP.Net 1.1 code below:

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

I've tried just about everything I know how to do. My best guess is
that the error is somehow related to an SSL problem, or perhaps a
certificate store policy, while working on a Windows Vista machine.
Could it be that Vista blocks connections differently than XP?
Whatever the case, I have tried various fixes for these problems with
no success.

The code here is complete and ready to paste into a .Net 1.1 console
app. The dummy username and password has no effect on the error I
receive.

Most likely, the SSL certificate for the site is invalid (expired or
something) or it's not on the machine period and is needed. That's the only
time I have seen the message you received when using SSL. I kind of doubt
that Vista is blocking anything.
 
Thanks for your replies. I changed the XML by removing the spaces, but
that did not correct the problem.

One possibly interesting side note: I started up Fiddler2 in hopes of
seeing the HTTPS traffic between my computer and the Web Service. When
using the "Decrypt HTTPS Traffic" option, the code works fine. It
seems somehow that Fidder2's HTTPS proxy helps prevent the "Connection
Closed" error. I find it all puzzling ... and frustrating.

As always, I appreciate any other solutions you may have.

--Brent
 
Brent said:
Thanks for your replies. I changed the XML by removing the spaces, but
that did not correct the problem.

One possibly interesting side note: I started up Fiddler2 in hopes of
seeing the HTTPS traffic between my computer and the Web Service. When
using the "Decrypt HTTPS Traffic" option, the code works fine. It
seems somehow that Fidder2's HTTPS proxy helps prevent the "Connection
Closed" error. I find it all puzzling ... and frustrating.

As always, I appreciate any other solutions you may have.

Well, use the WSDL pointing to the Web site and the Web service in a
browser. If it pops the certificate form, then you know there is a problem
with the certificate. If it's ok, the the XML for the Web service is ok

The other thing you can do is use the .Net WSDL.exe at the .Net Command
Prompt again pointing to the Web site and the Web Service, to create a proxy
C#.cs for the Web service . If the WSDL.exe aborts and closes the
connection, then again you know you have a problem with the certificate.
 
Back
Top