C# 如何执行HTTP POST并从POST结果重定向到外部站点?
因此,我可以做以下的后提交,并得到重定向到支付网关网站C# 如何执行HTTP POST并从POST结果重定向到外部站点?,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,因此,我可以做以下的后提交,并得到重定向到支付网关网站 @Html.BeginForm(null, null, FormMethod.Post, new { @action = "https://l33tpaymentgateway.com" }) { <input id="RefNo" name="RefNo" type="hidden" value="ABCDE" /> <input id="Amount" name="Amount" type="hidde
@Html.BeginForm(null, null, FormMethod.Post, new { @action = "https://l33tpaymentgateway.com" })
{
<input id="RefNo" name="RefNo" type="hidden" value="ABCDE" />
<input id="Amount" name="Amount" type="hidden" value="300" />
<input id="UserEmail" name="UserEmail" type="hidden" value="warheat1990@warheat1990.com" />
<input id="Signature" name="Signature" type="hidden" value="1234567890" />
<input id="ResponseURL" name="ResponseURL" type="hidden" value="http://warheat1990.com" />
<input type="submit" value="submit"/>
}
从安全角度来看,我认为文档中的HTTP POST示例是一个很大的否定
<HTML>
<BODY>
<FORM method="post" name="ePayment" action="https://l33tpaymentgateway.com">
<INPUT type="hidden" name="MerchantCode" value="ID00001">
<INPUT type="hidden" name="PaymentId" value="1">
<INPUT type="hidden" name="RefNo" value="A00000001">
<INPUT type="hidden" name="Amount" value="300">
<INPUT type="hidden" name="Currency" value="USD">
<INPUT type="hidden" name="ProdDesc" value="Photo Print">
<INPUT type="hidden" name="UserName" value="John Tan">
<INPUT type="hidden" name="UserEmail" value="john@hotmail.com">
<INPUT type="hidden" name="UserContact" value="0126500100">
<INPUT type="hidden" name="Remark" value="">
<INPUT type="hidden" name="Lang" value="UTF-8">
<INPUT type="hidden" name="Signature" value="Q/iIMzpjZCrhJ2Yt2dor1PaFEFI=">
<INPUT type="hidden" name="ResponseURL" value="http://www.test.com/payment/response.asp">
<INPUT type="hidden" name="BackendURL" value="http://www.test.com/payment/backend_response.asp">
<INPUT type="submit" value="Proceed with Payment" name="Submit">
</FORM>
</BODY>
</HTML>
其中密钥是MerchantKey(类似于私钥)+商户代码+参考号+金额的组合更新了您的代码如下:
public ActionResult SubmitPayment()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://l33tpaymentgateway.com");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("RefNo", "ABCDE"),
new KeyValuePair<string, string>("Amount", "300"),
new KeyValuePair<string, string>("UserEmail", "warheat1990@warheat1990.com"),
new KeyValuePair<string, string>("Signature", "1234567890"),
new KeyValuePair<string, string>("ResponseURL", "http://warheat1990.com")
});
var result = await client.PostAsync("", content).Result;
if(result.IsSuccessStatusCode)
{
return Redirect(result.url);
}
}
}
public ActionResult SubmitPayment()
{
使用(var client=new HttpClient())
{
client.BaseAddress=新Uri(“https://l33tpaymentgateway.com");
var content=newformurlencodedcontent(new[]
{
新的KeyValuePair(“参考号”、“ABCDE”),
新的KeyValuePair(“金额”、“300”),
新的KeyValuePair(“UserEmail”warheat1990@warheat1990.com"),
新的KeyValuePair(“签名”,“1234567890”),
新的KeyValuePair(“ResponseURL”http://warheat1990.com")
});
var result=wait client.PostAsync(“,content).result;
if(结果。IsSuccessStatusCode)
{
返回重定向(result.url);
}
}
}
我不确定结果对象是什么。但是将要重定向到的url放入重定向方法的参数中。我不太明白您要做什么。你想重定向到哪里?您只需使用returnnewredriect(redirectUrl);以目前的形式,这确实是不可解释的。这完全取决于“l33t支付网关”是如何设计的。如果它返回用户需要交互的页面,则必须在用户会话中存储任何cookies服务器端,将返回的HTML数据返回给用户,并处理下一篇文章。不过,您不想这样做,因为用户不必向您的站点提供他们的付款详细信息。如果站点响应重定向,其中URL包含用于此支付的某种令牌,那么它是微不足道的.server.redirect(URL)可能与您不需要重定向的内容重复。只需使用HttpClient呼叫他们的站点并发布数据。HttpClient将自动执行重定向,并且
HttpResponseMessage
没有url
property。url只是一个例子
<HTML>
<BODY>
<FORM method="post" name="ePayment" action="https://l33tpaymentgateway.com">
<INPUT type="hidden" name="MerchantCode" value="ID00001">
<INPUT type="hidden" name="PaymentId" value="1">
<INPUT type="hidden" name="RefNo" value="A00000001">
<INPUT type="hidden" name="Amount" value="300">
<INPUT type="hidden" name="Currency" value="USD">
<INPUT type="hidden" name="ProdDesc" value="Photo Print">
<INPUT type="hidden" name="UserName" value="John Tan">
<INPUT type="hidden" name="UserEmail" value="john@hotmail.com">
<INPUT type="hidden" name="UserContact" value="0126500100">
<INPUT type="hidden" name="Remark" value="">
<INPUT type="hidden" name="Lang" value="UTF-8">
<INPUT type="hidden" name="Signature" value="Q/iIMzpjZCrhJ2Yt2dor1PaFEFI=">
<INPUT type="hidden" name="ResponseURL" value="http://www.test.com/payment/response.asp">
<INPUT type="hidden" name="BackendURL" value="http://www.test.com/payment/backend_response.asp">
<INPUT type="submit" value="Proceed with Payment" name="Submit">
</FORM>
</BODY>
</HTML>
private string ComputeHash(string Key)
{
SHA1CryptoServiceProvider objSHA1 = new SHA1CryptoServiceProvider();
objSHA1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Key.ToCharArray));
byte[] buffer = objSHA1.Hash;
string HashValue = System.Convert.ToBase64String(buffer);
return HashValue;
}
public ActionResult SubmitPayment()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://l33tpaymentgateway.com");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("RefNo", "ABCDE"),
new KeyValuePair<string, string>("Amount", "300"),
new KeyValuePair<string, string>("UserEmail", "warheat1990@warheat1990.com"),
new KeyValuePair<string, string>("Signature", "1234567890"),
new KeyValuePair<string, string>("ResponseURL", "http://warheat1990.com")
});
var result = await client.PostAsync("", content).Result;
if(result.IsSuccessStatusCode)
{
return Redirect(result.url);
}
}
}