C# 从控制器生成 http post 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1705442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate http post request from controller
提问by Wei Ma
Forgive me if this is a stupid question. I am not very experienced with Web programming. I am implementing the payment component of my .net mvc application. The component interacts with an external payment service. The payment service accepts http post request in the following form
如果这是一个愚蠢的问题,请原谅我。我对 Web 编程不是很有经验。我正在实现我的 .net mvc 应用程序的支付组件。该组件与外部支付服务交互。支付服务接受以下形式的 http post 请求
http://somepaymentservice.com/pay.do?MerchantID=xxx&Price=xxx&otherparameters
I know this is dead easy to do by adding a form in View. However, I do not want my views to deal with third party parameters. I would like my view to submit information to my controller, then controller generates the required url and then send out the request. Following is the pseudo code.
我知道这很容易通过在 View 中添加一个表单来完成。但是,我不希望我的视图处理第三方参数。我希望我的视图向我的控制器提交信息,然后控制器生成所需的 url,然后发出请求。以下是伪代码。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PayForOrder(OrderForm order)
{
var url = _paymentService.GetUrlFromOrder(order);
SendPostRequest(url);
return View("FinishedPayment");
}
Is it possible to do so? Does c# have built-in library to generate http request? Thanks in advance.
有可能这样做吗?c# 是否有内置库来生成 http 请求?提前致谢。
采纳答案by Andy Gaskell
You'll want to use the HttpWebRequestclass. Be sure to set the Method property to post - here's an example.
您将需要使用HttpWebRequest类。请务必将 Method 属性设置为 post - 这是一个示例。
回答by Andy Gaskell
There certainly is a built in library to generate http requests. Below are two helpful functions that I quickly converted from VB.NET to C#. The first method performs a post the second performs a get. I hope you find them useful.
当然有一个内置的库来生成 http 请求。下面是我从 VB.NET 快速转换为 C# 的两个有用的函数。第一种方法执行post,第二种方法执行get。我希望你觉得它们有用。
You'll want to make sure to import the System.Net namespace.
您需要确保导入 System.Net 命名空间。
public static HttpWebResponse SendPostRequest(string data, string url)
{
//Data parameter Example
//string data = "name=" + value
HttpWebRequest httpRequest = HttpWebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
var streamWriter = new StreamWriter(httpRequest.GetRequestStream());
streamWriter.Write(data);
streamWriter.Close();
return httpRequest.GetResponse();
}
public static HttpWebResponse SendGetRequest(string url)
{
HttpWebRequest httpRequest = HttpWebRequest.Create(url);
httpRequest.Method = "GET";
return httpRequest.GetResponse();
}
回答by Mathias F
It realy makes a difference if ASP.NET makes a request or the the client makes a request. If the documentation of the the provider says that you should use a form with the given action that has to be submited by the client browser then this might be necessary.
如果 ASP.NET 发出请求或客户端发出请求,这确实有所不同。如果提供商的文档说您应该使用具有必须由客户端浏览器提交的给定操作的表单,那么这可能是必要的。
In lots of cases the user (the client) posts some values to the provider, enters some data at the providers site and then gets redirected to your site again. You can not do this applicationflow on the serverside.
在很多情况下,用户(客户端)向提供者发布一些值,在提供者站点上输入一些数据,然后再次重定向到您的站点。您不能在服务器端执行此应用程序流程。