English 中文(简体)
电子邮件:www.un.org
原标题:Generate http post request from controller
  • 时间:2009-11-10 03:20:35
  •  标签:

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

http://somepaymentservice.com/pay.do?MerchantID=xxx&Price=xxx&otherparameters

我知道,通过增加一种观点来做到这一点是容易的。 然而,我不想谈谈第三方参数。 我想向我的控制者提交信息,然后,控制者产生所需的ur,然后发出请求。 下面是伪装法。

[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.

最佳回答
问题回答

图书馆当然有一个建筑,可以提出网上要求。 下面是我很快从VB改划的两个有益职能。 NET to C#. 第一种方法是履行第二任职务。 我希望你看到这些内容有用。

你希望确保进口该系统。 网上名称空间。

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();
}

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.

在很多情况下,用户(客户)向供应商提供一些价值,在供应商网站上输入一些数据,然后再次转往贵网站。 您无法在服务器上进行这一应用。





相关问题
热门标签