p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   .NET Web Services (http://p2p.wrox.com/forumdisplay.php?f=102)
-   -   The underlying connection was closed: (http://p2p.wrox.com/showthread.php?t=4586)

jhouse October 4th, 2003 05:22 PM

The underlying connection was closed:
occasionally I recieve this error from production enviroment when asp.net call web service, the next call to web service is usually succesful. Error
System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown.

---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.

   at System.Web.Services.Protocols.WebClientProtocol.Ge tWebResponse(WebRequest request)

   at System.Web.Services.Protocols.HttpWebClientProtoco l.GetWebResponse(WebRequest request)

   at System.Web.Services.Protocols.SoapHttpClientProtoc ol.Invoke(String methodName, Object[] parameters)

   at MySite.MyService.Service1.ProcessUpdates(String ApplicationName, Boolean bUpdateAllData)

   at MySite.MyAspxPage.Sub_CommitTransaction(Object sender, EventArgs e)

   at System.Web.UI.TemplateControl.OnCommitTransaction( EventArgs e)

   at System.Web.UI.Page.ProcessRequestTransacted()

   --- End of inner exception stack trace ---

   at System.Web.UI.Page.HandleError(Exception e)

   at System.Web.UI.Page.ProcessRequestTransacted()

Anyone else had this problem?

MichaelTJ October 9th, 2003 06:12 PM

Do you access any data from SQL? I've had a simmilar(but not same) problem when trying to access a SQL connection that has closed during page load. Quite by accident thou, the SQL server has shutdown just as my page has loaded..

misrasandeep October 13th, 2003 03:19 AM

I got the same problem when I tried to access the web service from .NET (C#) client which was behind a http proxy server.If I access the same web service without using HTTP Proxy ,the calls are successful.There is a remedy for this problem .Just override the method getWebRequest in your reference.cs file .
and add this code
protected override WebRequest GetWebRequest(Uri uri)
            HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);

            webRequest.KeepAlive = true;

            return webRequest;
This can solve the problem

misrasandeep October 30th, 2003 11:23 AM

My old post had an error please consider this,
and add this code to reference.cs
protected override WebRequest GetWebRequest(Uri uri)
            HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);

            webRequest.KeepAlive = false;
            return webRequest;
This can solve the problem

elxifara October 31st, 2003 02:28 PM

Hi all,

I have developed a WebApplication that sends messages to mobile phones by connecting to an integrator platform. I use VB.NET. Although my application seems to connect and authorise ok, right after it sends the message, the connection is broken and I get the System.Net.WebException. The first time I run the app, I get an error on a send, all the subsequent runs fail on the receive, the stack trace is the same, though:

[WebException: The underlying connection was closed: An unexpected error occurred on a receive.]
   System.Web.Services.Protocols.WebClientProtocol.Ge tWebResponse(WebRequest request) +58
   System.Web.Services.Protocols.HttpWebClientProtoco l.GetWebResponse(WebRequest request) +5
   System.Web.Services.Protocols.SoapHttpClientProtoc ol.Invoke(String methodName, Object[] parameters) +183
   WebApplication1.localhost.BrokerService.sendsimple message(simplemessageRequest Request, String& ErrorMessage, Object[]& Failed_MSISDN, Int32& RequestID) in C:\Inetpub\wwwroot\WebApplication1\Web References\localhost\Reference.vb:81
   WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WebApplication1\WebForm1.aspx.v b:61
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277

I tried the suggested solution, but I still get the same error. Any ideas?

xins January 11th, 2004 10:38 PM

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

i have the same problem as you. please see this page first for the main problem in .NET web.services :


well i added thos recommended code but it still has the same error in my program so i add some code and it's works, try this :

private string requestpost(String TheURL,string postdata, String TheProxy)
  Uri uri = new Uri(TheURL);
  HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
  string page;
    string postsourcedata=postdata;

    // start added codes
    request.KeepAlive = false;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = postsourcedata.Length;
    request.Proxy = System.Net.WebProxy.GetDefaultProxy();
    request.Timeout=(int) new TimeSpan(0,0,60).TotalMilliseconds;
    request.UserAgent="Mozilla/3.0 (compatible; My Browser/1.0)";
    // end added codes

    Stream writeStream = request.GetRequestStream();
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postsourcedata );
    writeStream.Write(bytes, 0, bytes.Length);

    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8);

    page = readStream.ReadToEnd();
  catch (Exception ee)
    page = "fail!";
  return page;

hopefully your troubled solved..



(by the way have u watch LORD OF THE RING : RETURN OF THE KING?)

See u in matrix

grhind January 12th, 2004 01:59 PM

How would I go about writing this in VB.NET. What class would it need to be in and what inheritance would I need.

protected override WebRequest GetWebRequest(Uri uri)
            HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);

            webRequest.KeepAlive = false;
            return webRequest;

okll May 16th, 2004 11:51 PM

I campareed two programs reslults-- one is in localhost with aspnet, one is consol program, same programs give different results, why? the firewall can distinguish them?

1st, I use http://localhost/quickstart/howto/sa...ebrequest.aspx
only those pages in local host can be received.
for those pages in internet: back webexception:
System.Net.WebException: The underlying connection was closed: Unable to connect to the remote server.
   at System.Net.HttpWebRequest.CheckFinalStatus()
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult asyncResult)
   at System.Net.HttpWebRequest.GetResponse()
   at ASP.webrequest_aspx.btnSubmit_Click(Object sender, EventArgs e)

another is almost same program but run in console mode

I can get right results!

Why? these programs are all .NET 's samples, why give different results?

Who can help me?

if I upload webrequest.aspx to my web site on internet, all OK!

Is it possible for my IIS or proxy server or firewall need some special setting?

test under VS.NET and C#.net and .NET Frame SDK 1.1, OS is Window 2000 server, IIS 5, and IE 6 as internet explore.

hansiola1 May 24th, 2004 08:51 AM


Must I use webRequest.KeepAlive = false?
I understand what Microsoft writes on theirs page.
But isn't that a simple solution for a complex situation.
Of course I can skip the use of keep alive but what does that do
for my performance?

I found when minimizing the use of static functions and variables makes the error less frequent.
Is there any more redesign one can do to make the error go away??
And keeping the KeepAlive?



quote:Originally posted by misrasandeep
 My old post had an error please consider this,
and add this code to reference.cs
protected override WebRequest GetWebRequest(Uri uri)
            HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);

            webRequest.KeepAlive = false;
            return webRequest;
This can solve the problem

amitkm July 28th, 2004 10:56 AM

I am also getting the same error, while connecting to SQL reporting service which is a web service by itself.

But if I am updating the reference file, and run the .net program which access the webservice, I get the error "HTTP request aborted. Access denied".

When I delete the reference and add a new one that works.
If anybody has a pointer plz help me.


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

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