0

I am working on this project, where my Application need to connect to a third party application. My steps

  1. Create form to submit data.
  2. Get Form data in MY controller Action. Save mine required variables to database.
  3. Then send the data to Third Party Application.

Now the problem here is that, i need to send the data as Submit Form. The Third party app takes the values as

Request.form("FormVal")

I can make no changes to the Third party Application.

So how do i send the Form data through my Controller action to the Third Party application?

Also

return view("action", model); // the model is needed to be sent.

it works for the view in my own Application. However I need to send it to an external view(third party) with the model so they can get value as

Request.form("FormVal")

I see the Redirect commands can go the external view but i cannot send the form Data with it.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ruchan
  • 3,124
  • 7
  • 36
  • 72

2 Answers2

1

You can use HttpClient to send data to the third party application.

like this:

  var formValues = new Dictionary<string,string>();
  formValues.Add("Key", "Value");
  HttpResponseMessage response = await httpClient.PostAsync(thirdPartyUrl, new FormUrlEncodedContent(formValues));
  response.EnsureSuccessStatusCode();
  string responseBody = await response.Content.ReadAsStringAsync();

Edited:

If you are not using .NET Framework 4.5 you can use the WebClient.UploadValues instead.

 byte[] responseArray = myWebClient.UploadValues(thirdPartyUrl,myNameValueCollection);
Kambiz Shahim
  • 2,560
  • 14
  • 21
  • Thankyou I will check it now and reply you. – Ruchan May 05 '13 at 08:21
  • The HttpClient Doesn't seem to be supported in .net 4.0 framework. It's for 4.5. http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx – Ruchan May 05 '13 at 08:31
  • OK, you can also use WebClient http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.100).aspx – Kambiz Shahim May 05 '13 at 08:36
  • Sorry your method is not much to my use, because it doesn't do `redirect`. – Ruchan May 05 '13 at 09:06
  • Sorry, I think you want send data to the third party application in background without redirect the user to that application. – Kambiz Shahim May 05 '13 at 09:17
  • 1
    Nature of an HTTP redirection is a GET request not a POST. Most browsers even convert an original POST request to GET. For more information see this [post](http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get). – Kambiz Shahim May 05 '13 at 09:39
  • I think using Javascript is the best method for now. Thanks Kambiz – Ruchan May 05 '13 at 10:00
0

the HttpResponseMessage was not what i actually wanted. I also needed to redirect. So I used javascript/jquery for this.

I created two views

  1. 1st is the user input view.
  2. 2nd is the Redirect View

From the 1st view I post to my controller then, call my redirect view from the controller action

return view("redirectView", model);

In the Redirect view I created a form with hidden inputs.

@HiddenFor(m=>m.SomeValue)

Then on page load i used javascript to Submit the form.

$("form#formID").submit();

This works for me right now. Also thanks to Kambiz for making me understand this issue.

Ruchan
  • 3,124
  • 7
  • 36
  • 72