I have the following code
CookieContainer container = new CookieContainer();
HttpCookieCollection oCookies = HttpContext.Current.Request.Cookies;
for (int j = 0; j < oCookies.Count; j++)
{
HttpCookie oCookie = oCookies.Get(j);
Cookie oC = new Cookie();
// Convert between the System.Net.Cookie to a System.Web.HttpCookie...
oC.Domain = HttpContext.Current.Request.Url.Host;
oC.Expires = oCookie.Expires;
oC.Name = oCookie.Name;
oC.Path = oCookie.Path;
oC.Secure = oCookie.Secure;
oC.Value = oCookie.Value;
container.Add(oC);
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.ashx");
request.ServicePoint.ConnectionLimit = 100;
request.Timeout = 20000;
//request.Credentials = CredentialCache.DefaultCredentials;
request.ServicePoint.Expect100Continue = false;
request.CookieContainer = container;
request.Method = "POST";
string formContent = "requestName=update&objectId=1&parentId=1";
byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Flush();
dataStream.Close();
}
try
{
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
{
if (stream == null)
{
throw new Exception("no response");
}
using (var sr = new StreamReader(stream))
{
var content = sr.ReadToEnd();
}
}
}
catch (WebException wex)
{
var pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
throw new Exception(pageContent);
}
}
I am attempting to POST the data to a page on behalf of the user, this is a process that happens programatically and the site uses forms authentication, so the code copies the current cookies, and adds the container to the HttpWebRequest
. When the code runs it gets to the line where it calls request.GetResponse()
but at this point the code stops, and eventually times out. However, i have a break point on the PageLoad of the page it is calling, once the timeout has occurred the code hits the breakpoint at the start of this page with the correct information from the POST and the session cookies as would be expected. Does anybody know why there is a call that leads to a timeout first or what is happening at this point?