Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
  #2 (permalink)  
Old January 19th, 2009, 04:53 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
  #3 (permalink)  
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
  #4 (permalink)  
Old January 20th, 2009, 03:49 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
  #5 (permalink)  
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:58 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.