473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

HttpWebRequest Operation Has Timed Out After Numerous Requests

Hey Group,

I'm in the process of converting an ASP-based site to an ASP.NET site and
built a control that wraps around an ASP page. The control simply does a GET
to the same server to render the ASP content, and then just writes it to the
page.

In development and testing, there were no problems. Once deployed to
production, after the site runs for a few hours, it starts to slow down and
throw countless "Operation Has Timed Out" WebExceptions.

I'm not exactly sure what's wrong with my code. In the global.asax, I have
the ServicePointManager.DefaultConnectionLimit set to 1000. All this seems to
do is extend how long it takes for timeouts to occur.

Any idea what I'm doing wrong?

Thanks,
Alex

---- CODE ----
public class AspPage : WebControl
{
public AspPage() {}

private StringBuilder _text = new StringBuilder();

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RequestPage();
}

private void RequestPage()
{
//create a wrapper around the _text field
StringWriter writer = new StringWriter(_text);

//build the request
HttpWebRequest rqst = BuildRequest();

//get the response
HttpWebResponse resp = null;
try
{
//make the request
try
{
resp = (HttpWebResponse)rqst.GetResponse();
}
catch (WebException ex)
{
resp = (HttpWebResponse)ex.Response;
if (resp == null)
{
writer.Write(ex.ToString());
return;
}
}

//perhaps there was an error, a timeout
if (resp == null)
{
writer.Write("An unknown error occured.");
return;
}
//Pass Cookies back out
foreach (Cookie cook in resp.Cookies)
{
HttpCookie cookie = new HttpCookie(cook.Name, cook.Value);
cookie.Expires = cook.Expires;
Page.Response.Cookies.Add(cookie);
}

//Set Roles/Username from headers
if (GetMagHeaders)
{
HttpContext.Current.Session["MAGROLES"] = string.Format("{0}",
resp.Headers["MAGROLES"]);
HttpContext.Current.Session["MAGUSERNAME"] = string.Format("{0}",
resp.Headers["MAGUSERNAME"]);
}
//Redirect if appropriate
if ((resp.StatusCode == HttpStatusCode.Found) ||
(resp.StatusCode == HttpStatusCode.Redirect) ||
(resp.StatusCode == HttpStatusCode.Moved) ||
(resp.StatusCode == HttpStatusCode.MovedPermanently))
{
Page.Response.Redirect(resp.Headers["Location"]);
}

//write out the response
StreamReader reader = null;
try
{
reader = new StreamReader(resp.GetResponseStream());
writer.Write(reader.ReadToEnd());
}
finally
{
if (reader !=null) reader.Close();
}

}
finally
{
if (resp != null) resp.Close();
}
}

private HttpWebRequest BuildRequest()
{
//build the Uri to request
UriBuilder uri = new UriBuilder(Page.Request.Url);
uri.Path = Page.ResolveUrl(Url);

//URI Querystring
if (CopyQueryString) uri.Query = Page.Request.QueryString.ToString();
else if (QueryString != String.Empty) uri.Query = QueryString;

//Request to return
HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(uri.Uri);
rqst.AllowAutoRedirect = false;
rqst.KeepAlive = false;

//special header
rqst.Headers.Add("DOTNET-REQUEST", "yes");

//Misc Headers
rqst.Headers["HTTP_REFERER"] = Page.Request.Headers["HTTP_REFERER"];
rqst.UserAgent = Page.Request.UserAgent;

//copy cookies
if (Page.Request.Cookies.Count > 0)
{
rqst.CookieContainer = new CookieContainer(Page.Request.Cookies.Count);
foreach (string name in Page.Request.Cookies)
{
Cookie cookie = new Cookie(name, Page.Request.Cookies[name].Value);
rqst.CookieContainer.Add(rqst.RequestUri, cookie);
}
}
else
{
rqst.CookieContainer = new CookieContainer();
}

//body
if (CopyForm && Page.Request.RequestType.ToUpper() == "POST")
{
rqst.Method = "POST";
rqst.ContentType = Page.Request.ContentType;
rqst.ContentLength = Page.Request.ContentLength;

Stream input = Page.Request.InputStream;
Stream output = rqst.GetRequestStream();

byte[] buffer = new byte[2048];
int bytesRead = input.Read(buffer, 0, 2048);
while (bytesRead != 0)
{
output.Write(buffer, 0, bytesRead);
bytesRead = input.Read(buffer, 0, 2048);
}

output.Close();
}
return rqst;
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(_text.ToString());
}

/* Snip Properties */
}
Jan 29 '06 #1
0 1485

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker...
9
by: Mike Cronin via DotNetMonster.com | last post by:
Hi there, Can anyone tell me what level of encryption is used when making an HTTPS POST request through an instance of the System.Net.HttpWebRequest object? Thanks much in advance! Mike...
4
by: Mark Fletcher | last post by:
Hi, Im in the process of writing a program that crawls a website. Im using the HttpWebRequest and HttpWebResponse classed to get content. To make my application more scalable, my application is...
1
by: Jeff B | last post by:
I'm trying to create a simple screen scraping application and I kept getting a System.Net.WebException thrown back with a message of "The operation has timed-out." At first I thought it was some...
1
by: | last post by:
I have a strange behavior of a following code: HttpWebRequest request=(HttpWebRequest)WebRequest.Create(@"http://.../target.jsp"); request.ProtocolVersion=HttpVersion.Version11;...
1
by: Manso | last post by:
Hi, I have an ASP.NET 1.1 app running on Windows XP SP2 that is requesting information from a WebService which works just fine. Another part of the app is doing a server-side post to antoher...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
1
by: mroffey | last post by:
Hi I'm using the code below to post to a web form, but when I run it, i get System.Net.WebException: The operation has timed-out. error Obviously something is wrong, can anyone give me a clue?...
1
by: Eric Cadwell | last post by:
Are there two distinct timeouts on HttpWebRequest.GetResponse()? If I recall correctly, I've seen an "operation timed out" that was not the same as request time out. Thanks, Eric
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.