HttpWebRequest Operation Has Timed Out After Numerous Requests

G

Guest

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 */
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top