0

Im trying to redirect my ASP.NET page on it load to redirect to another page, whose URL i would get from the response of an HTTP post.

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

StringBuilder postData = new StringBuilder();
postData.Append("xmldata=" + HttpUtility.UrlEncode(xdoc));
postData.Append("&signature=" +HttpUtility.UrlEncode(signature));
httpWebRequest.Method = "POST";
httpWebRequest.Accept = "*/*";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (BinaryWriter bw = new BinaryWriter(ms))
        {
            bw.Write(Encoding.UTF8.GetBytes(postData.ToString()));
            ms.WriteTo(requestStream);
        }
    }
}
httpWebRequest.AllowAutoRedirect = false;
var returnURL="";
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
    httpStatusCode = httpWebResponse.StatusCode;
     if (httpStatusCode == HttpStatusCode.Found)
     {
        returnURL= httpWebResponse.Headers["Location"].ToString();
     }
}

Response.Redirect(returnURL);

This response redirect ends in 404 error. Please help

Raju
  • 207
  • 4
  • 12

2 Answers2

1

In the below lines of code:

var returnURL="";
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
    httpStatusCode = httpWebResponse.StatusCode;
     if (httpStatusCode == HttpStatusCode.Found)
     {
        returnURL= httpWebResponse.Headers["Location"].ToString();
     }
}

Response.Redirect(returnURL);

The returnURL variable may be empty if the httpStatusCode is not equal to HttpStatusCode.Found. You might want to pass a different valid URL for other status codes.

d.i.joe
  • 606
  • 9
  • 22
0

Use

System.Threading.Thread.Sleep(2000);

before

Response.Redirect(returnURL);
Pang
  • 9,564
  • 146
  • 81
  • 122