Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2009, 01:56 PM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default HttpWebRequest - The connection was closed unexpectedly

Hi,

I'm trying to POST an xml message to a web server by using HttpWebRequest.

If I post a correct xml the response from the web server is

HTTP/1.0 200 OK

and I can get the web server's response with

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

But if the xml message is faulty in any way, the web server returns

HTTP/1.0 516 Error Message(CR)(LF)

and a WebException is thrown when I try to get the HttpWebResponse. The WebException only says that the connection was closed unexpectedly. But if I sniff the traffic I can se the response message from the web server as stated above.

Does anyone know why I can't read the response from the web server and why the connection is closed when I try to get the HttpWebResponse object?

Thanks in advance.

Regards,
Simon
 
Old January 19th, 2009, 04:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Have you looked at the InnerException property of the generic WebException? The inner exception may reveal the real problem if it's not null....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 20th, 2009, 03:17 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I've checked the InnerException property and it is NULL unfortunately. So no luck there .

The message

HTTP/1.0 516 Error Message(CR)(LF)

is returned from the web server the moment i close the stream that I use to write data to the request stream. I'm a bit concerned that the web server closes the connection after this message has been sent.

Shouldn't I be able to read the above message even if the connection is closed? For all I know the message has already been sent from the server and should be available on the "wire".

Simon
 
Old January 20th, 2009, 03:49 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, I agree. If you can sniff the network and see the exception details, the info must be available somewhere. I just don't know how to get at that information without seeing more of your code, the web page you're posting to and a reproducible scenario. Sorry, may be someone else on this board does?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 20th, 2009, 03:59 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

This is the code I use to post to/read from the web server:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = message.Length;
request.ContentType = "text/xml";
request.ProtocolVersion = HttpVersion.Version10;

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(message);
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}

The web server is hosted within a Java application so I can't provide any information about it's implementation.

The exception is being trown on the followin line:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

But the response message from the web service is available at the moment the StreamWriter object is closed (or disposed whatever comes first).

The strange thing is that I've already written a client in Java to communicate with this web server using Jakarta Commons HttpClient without any problems. The error message and status code (516) are returned without any exception being thrown in my client application.

Simon





Similar Threads
Thread Thread Starter Forum Replies Last Post
The underlying connection was closed: jhouse .NET Web Services 33 September 21st, 2011 04:26 PM
The underlying connection was closed: An unexpecte ksumijain .NET Web Services 0 April 4th, 2008 12:42 PM
The underlying connection was closed: unable to co badyalsk .NET Web Services 0 January 23rd, 2007 04:44 AM
Connection being closed? Amateur BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 January 18th, 2007 04:18 AM
Underlying connection was closed r_ganesh76 .NET Web Services 0 May 4th, 2006 04:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.