WebResponse 到 HttpResponseBase。是否可以?

发布于 2024-08-11 14:08:33 字数 568 浏览 3 评论 0原文

在控制器操作中,我使用 WebRequest 手动将表单发送到远程 URL。我成功收到包含要显示的 html 页面的 WebResponse。我想将此响应“粘贴”为操作的响应(类型为 HttpResponseBase)。操作通常会返回 ActionResult,那么如何结束我的控制器操作以便 WebResponse 将成为结果?

注意:浏览器中的Url也必须成为响应的url。

更新:这是目标。这是贝宝结帐页面上的。我不想在我的视图中使用包含所有购物车隐藏字段和结账提交按钮的表单,而是想要一个链接到我的操作之一的简单结账按钮。在此操作中,我将使用表单准备 WebRequest 并将其发送到 paypal。在操作中执行此操作还允许我将未激活的订单存储在数据库表中,以便当订单确认到来时我可以将其与存储的订单进行比较并激活它。

解决方案:感谢那些回答的人指出这不可能通过 POST 重定向。看来我没有义务通过 POST 重定向到 paypal。相反,我可以使用查询字符串中的所有购物车数据构建一个 URL 并重定向到它。从控制器操作方法执行此操作仍然允许我将挂单存储在数据库中。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅冇旳回忆 2024-08-18 14:08:33

如果您只想将 WebRequest 响应的内容在控制器操作的响应中发回,您可以在操作方法中执行类似的操作:

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;

这更加简洁:

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

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

return fsr;

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;
无悔心 2024-08-18 14:08:33

没有直接的方法可以做到这一点。我对 ASp.NET 不太了解。您是说您的 ASP.net 页面正在向远程站点发出 HTTP 请求并返回响应,并且您希望该响应成为 HTTPResponseBase?之后你想用它做什么?

请参阅以下有关类似主题的 SO 线程...
如何模拟 HttpResponseBase.End()?

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...
How do I mock HttpResponseBase.End()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文