locked
How to send HttpWebRequest RRS feed

  • Question

  • I am trying to redirect to a different location by adding some value in HttpWebRequest Header
    But i don't no how to submit it so that it can redirect me to the Uri Location
    some one can suggest me Please

    The related code is here

    HttpWebRequest httpWebRequest = WebRequest.Create("http://localhost:3440/Call/default.aspx") as HttpWebRequest;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                //PaymentReutrnUrl
                httpWebRequest.Headers.Add("returnURL", "http://localhost:3440/Call/default2.aspx");
                httpWebRequest.Headers.Add("amt", "3242.002");
                httpWebRequest.Headers.Add("pname", "PNM");
                httpWebRequest.Headers.Add("pcode", "PCC");
                httpWebRequest.Headers.Add("sid", HttpContext.Current.Session.SessionID);
                httpWebRequest.Headers.Add("ip", Function.GetClientIPAddress(HttpContext.Current.Request));
                httpWebRequest.Headers.Add("Curr", ConfigurationManager.AppSettings["Currncy"].ToString());
                httpWebRequest.Headers.Add("accno", ConfigurationManager.AppSettings["Currncy"].ToString());
                httpWebRequest.Headers.Add("mobno", ConfigurationManager.AppSettings["Currncy"].ToString());

                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.Referer = ConfigurationManager.AppSettings["PaymentUrl"].ToString();
                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

     

    I Simply Want to redirect this   to http://localhost:3440/Call/default.aspx with header value

          


    Tushar Tyagi
    • Moved by Dummy yoyo Tuesday, December 13, 2011 7:47 AM (From:Visual C# Language)
    Saturday, December 10, 2011 8:06 AM

All replies

  • Your code appears to try and post the data in the headers to default.aspx as you seem to ask.  The server handles redirection while the client only states whether the redirection is allowed, via the AllowAutoRedirect property.  It is quite likely I am misunderstanding your goal.

    --
    Mike
    Saturday, December 10, 2011 12:59 PM
  • Hi Tushar,
    Welcome to the MSDN forum!

    You said "redirect this to ... with header value" . What is this which you are refering?

    I noticed that the returnURL is "http://localhost:3440/Call/default2.aspx" in the headers, which is quite confusing.

    Please refer to the following code snippets:

                HttpWebRequest httpWebRequest = WebRequest.Create("http://localhost:3440/Call/default.aspx") as HttpWebRequest;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Headers.Add("Location", "http://localhost:3440/Call/default2.aspx");
                httpWebRequest.Headers.Add("amt", "3242.002");
                httpWebRequest.Headers.Add("pname", "PNM");
                httpWebRequest.Headers.Add("pcode", "PCC");
                httpWebRequest.Headers.Add("sid", HttpContext.Current.Session.SessionID);
                httpWebRequest.Headers.Add("ip", Function.GetClientIPAddress(HttpContext.Current.Request));
                httpWebRequest.Headers.Add("Curr", ConfigurationManager.AppSettings["Currncy"].ToString());
                httpWebRequest.Headers.Add("accno", ConfigurationManager.AppSettings["Currncy"].ToString());
                httpWebRequest.Headers.Add("mobno", ConfigurationManager.AppSettings["Currncy"].ToString());
    
                httpWebRequest.AllowAutoRedirect = false;
                httpWebRequest.Referer = ConfigurationManager.AppSettings["PaymentUrl"].ToString();
    
                // Get the redirection URL location and then go there
                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                String reUrl = httpWebResponse.Headers("Location");
                Server.Transfer(reUrl);       
            
    

    You may also look at the following links:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredirect.aspx

    How to use HttpWebRequest and HttpWebResponse in .NET
    http://www.codeproject.com/KB/IP/httpwebrequest_response.aspx

    Why the location is not listed on the response Headers
    http://stackoverflow.com/questions/6195256 

     

    Meanwhile, I will move the thread to the following forum for better support:
    Network Class Library (System.Net) 
    http://social.msdn.microsoft.com/Forums/en-US/ncl/threads

    Have a nice day!

    Yoyo. 


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us





    Tuesday, December 13, 2011 5:47 AM
  • I'm suspecting the returnURL is also a parameter for the request that's used to specify where the browser should be redirected to when transaction is successful (something like payment gateway or shopcart), so it could be ignored for the purpose of solving the question.

    Btw, there's no way from web server's side to set header for a web browser to be sent to another page. Image if this were possible, all forums will have convenient way to fool those photo album hosting website who check access through referrer header.

    For this to work as you want, you probably want to rewrite the code in Javascript with jQuery.post() , where it also allow you to specify the header values in beforeSend handler.

    Because there could be requirement to to redirect to another page when transaction is success, Yoyo's solution may not work as desired depending on how the default.aspx is implemented.

    EDIT: Of course, if both pages are within your control, I highly recommand you to rewrite it to accept parameters in proper POST way (so you could embed a form with many hidden control to store the parameters) instead of inside headers.


    • Edited by cheong00 Tuesday, December 13, 2011 9:34 AM
    Tuesday, December 13, 2011 9:21 AM