views:

143

answers:

2

Hi,

In a controller action, I am manually sending a form to a remote URL using WebRequest. I successfully receive a WebResponse containing an html page to display. I would like to "paste" this response as the Response (of type HttpResponseBase) of the action. An action normally returns an ActionResult, so how do I conclude my controller action so that the WebResponse will be the result?

Note: the Url in the browser must also become the url of the response.

Update: Here is the goal. This is on a paypal checkout page. Instead of having a form with all cart hidden fields and a checkout submit button in my view, I would like a simple checkout button linked to one of my actions. In this action, I will prepare the WebRequest with the form and send it to paypal. Doing this in an action also allows me to store the inactivated order in a DB table so that when the order confirmation comes I can compare it with the stored order and activate it.

Solution: thanks to those who answered for pointing out that this would not be possible to redirect with a POST. It appears that I am not obliged to redirect to paypal with a POST. Instead I can construct a URL with all the cart data in the query string and redirect to it. Doing it from a controller action method still allows me to store the pending order in a database.

Thanks

A: 

If you just want the content of the WebRequest response to be sent back in the response from your controller action, you could do something like this in your action method:

WebRequest req = WebRequest.Create("http://www.google.com");
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());

ContentResult cr = new ContentResult();
cr.Content = sr.ReadToEnd();

return cr;

this is even more concise:

WebRequest req = WebRequest.Create("http://www.google.com");
WebResponse res = req.GetResponse();

FileStreamResult fsr = new FileStreamResult(res.GetResponseStream(),res.ContentType);

return fsr;
Matthew
Thank you. The problem is that the content is not enough (see my update above). The result also contains header and maybe cookies. If I just set the content, all those data won't be used. The net result is that the Url in the browser is not correct (it should be something like https://...paypal.../....). Also right now I'm using the paypal sandbox for my tests. Even if I'm logged in to the sandbox in the same browser, the paypal response is that I'm not logged in to the sandbox.
Nicolas Cadilhac
I'm not familiar with PayPal but why are you using a WebRequest to do that? I would assume that they have an API or something. You should look into that. As for my solution, you can just as easily copy cookies and other headers from the WebResponse into the Response of your action.
Matthew
A: 

THere is no direct way to do this. I dont know much about ASp.NET. ARe you saying that your ASP.net page is doing a HTTP request to a remote site, and getting back a response, and you want that response to become a HTTPResponseBase? What do you want to do with it after that?

See the following SO thread on a similar topic... http://stackoverflow.com/questions/1183124/how-do-i-mock-httpresponsebase-end

feroze