0

Anybody tried to add customized Http Header in MVC application, and I will set the form action to a third party URL, the third party URL is expecting some certain custom Http Headers. And after user submits the form from MVC application, the context must be switch to third party URL as well.

I need to build the MVC application and read value from server side and eventually compose them in header and submit the form.

Thanks

Hardy

hardywang
  • 4,864
  • 11
  • 65
  • 101

2 Answers2

1

You cannot add custom headers when using an HTML <form> element. There's just nothing that the HTML specification has to offer in this regard.

The only way to add custom headers is to perform the POST request to the third party site from your ASP.NET MVC application using either a WebClient or an HttpWebRequest. Both allow you to set custom HTTP headers when performing an HTTP request to a given url. Obviously the drawback is that you are performing the request on the behalf of your server application and not the client, so switching contexts might be challenging.

Depending on your exact specific scenario (which you haven't detailed) there might be different ways to try to tackle the problem.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • yes, switching context is my concern. After user submits the form from MVC application, user must see third party web site in browser. I did moresearch (e.g. http://stackoverflow.com/questions/581383/adding-custom-http-headers-using-javascript) I feel I might play with client side javaScript to submit the form. – hardywang Nov 28 '11 at 22:06
  • @hardywang, no you cannot use javascript due to the [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) restriction which will pretty quickly let you down. You cannot send cross domain AJAX requests. I would recommend you not wasting your time with javascript. – Darin Dimitrov Nov 28 '11 at 22:07
0

From the sound of your question you should post this information to the third party website using a customized HTTP request.

You can do this inside an action directly by processing the form post using HttpWebRequest and using an action result to share the confirmation with the user.

Such as:

public ActionResult PostTest()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();

            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();


            return View(responseFromServer);
        }

See MSDN on how to use HttpWebRequest to send HTTP Post forms.

Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21
  • HttpWebRequest will not switch the context to third party URL. And I see your solution here, your idea basiacally is to use server request (with custom header) and pull the content as string to display on UI. What worries me a bit is that all the links, image path from third party URL will be translated into local links and images, which don't exist at all. – hardywang Nov 28 '11 at 22:07
  • @hardywang yea that was just a quick idea as I'm not sure what you're trying to accomplish directly. Is there any reason you would need to show the user the entire website? You could just show some sort of confirmation page and embed only those items? Otherwise, yes the links would be off if you did a raw dump of the page as I showed in the answer. – Frazell Thomas Nov 28 '11 at 22:13
  • once user submits the form to third party URL, I am done on my side, user should work directly with other side. – hardywang Nov 28 '11 at 22:15