C# 将数据发布到特定URL&;显示返回页面

C# 将数据发布到特定URL&;显示返回页面,c#,asp.net,asp.net-mvc-4,asp.net-mvc-3,model-view-controller,C#,Asp.net,Asp.net Mvc 4,Asp.net Mvc 3,Model View Controller,在下面的HTML中,代码运行良好 <html> <head> <body> <h1>[http://www.smmotors.org][1]</h1> <form name="Redirect_to_NetConnect" method="post" action="http://www.smmotors.org/NetConnect"> <input type="hidden" name="Me

在下面的HTML中,代码运行良好

<html>
<head>
<body>
<h1>[http://www.smmotors.org][1]</h1>       

<form name="Redirect_to_NetConnect" method="post"  action="http://www.smmotors.org/NetConnect"> 

<input type="hidden" name="Merchant_ID" value="170922010433235"/>
<input type="hidden" name="Order_No" value="1321"/>
<input type="hidden" name="Order_Amount" value="900.00"/> 
<input type="hidden" name="Date" value="11/09/2017"/>
<input type="hidden" name="Time" value="21:00:00"/>
<input type="hidden" name="CheckSum" value="6217bd46945786a8ab864943e615f2aa"/>
<input type="hidden" name="Transaction_Desc" value="Shop From SM Motors"/>

<input value="try now" type="submit">


</form>
</body>
</head>
</html>

[http://www.smmotors.org][1]       
我想在支付插件(Controllers>HomeControllers.cs)中显示返回页面

public void NetConnect()
{
尝试
{
HttpClient=新的HttpClient();
var值=新列表();
添加(新的KeyValuePair(“商户ID”,“1709022104222235”);
添加(新的KeyValuePair(“订单号”、“1321”);
添加(新的KeyValuePair(“订单金额”,“900.00”);
//Add(新的KeyValuePair(“Date”,DateTime.Now.ToString(“dd/MM/yyyy”));
//Add(新的KeyValuePair(“Time”,DateTime.Now.ToString(“HH:mm:ss”));
添加(新的KeyValuePair(“日期”,“11/09/2017”);
添加(新的KeyValuePair(“时间”,“21:00:00”));
添加(新的KeyValuePair(“校验和”,校验和));
添加(新的KeyValuePair(“交易描述”、“SM汽车店”);
var内容=新的FormUrlEncodedContent(值);
var response=client.PostAsync(“http://http://www.smmotors.org/NetConnect/NetConnect“,内容)。结果;
var responseString=response.Content.ReadAsStringAsync();
var responseString1=client.GetStringAsync(“http://103.25.136.125/KPALServer/NetConnect.aspx");
}
捕获(例外)
{
抛出新异常();
}
}
我将数据发布到特定的URL&希望显示返回页面


返回时,上面的代码显示空白页。请指导如何显示返回页。

好的,您可以尝试这段简短的代码,它不太复杂,并且完全符合您的目标 添加以下静态类

public static class Http
{
    public static string Post(string uri, NameValueCollection pairs)
    {
        byte[] response = null;
        using (WebClient client = new WebClient())
        {
            response = client.UploadValues(uri, pairs);
        }
        return System.Text.Encoding.UTF8.GetString(response);
    }
}
然后简单地说:

var response = Http.Post("http://103.25.136.125/KPALServer/NetConnect.aspx", new NameValueCollection() {
    { "Merchant_ID", "170900010400235" },
    { "Order_NO", "1321" },
    { "Order_Amount", "900.00" } //similarly include all your post parameters
});
public void后处理付款(后处理付款请求后处理付款请求)
{
string orderNo=postprocessaymentrequest.Order.Id.ToString();
字符串金额=postProcessPaymentRequest.Order.OrderTotal.ToString(“0.00”,CultureInfo.InvariantCulture);
字符串merchantId=\u NetConnectPaymentSettings.CustomerId;
字符串日期=DateTime.Now.ToString(“dd/MM/yyyy”);
字符串时间=DateTime.Now.ToString(“HH:mm:ss”);
字符串urlNetConnect=\u NetConnectPaymentSettings.PaymentPage;
字符串校验和=生成商品请求校验和(“7C12B6AECC51AF3189799098AB1981”,商品、订单号、金额、日期、时间);
var client=新的HttpClient();
var值=新列表();
添加(新的KeyValuePair(“商户ID”,merchantId));
添加(新的KeyValuePair(“订单号”,订单号));
添加(新的KeyValuePair(“订单金额”,金额));
添加(新的KeyValuePair(“日期”),日期);
添加(新的KeyValuePair(“Time”,Time));
添加(新的KeyValuePair(“校验和”,校验和));
添加(新的KeyValuePair(“交易描述”、“SM汽车店”);
var内容=新的FormUrlEncodedContent(值);
//HttpResponseMessage响应=client.PostAsync(“http://www.smmotors.org“,内容)。结果;
HttpResponseMessage response=client.PostAsync(urlNetConnect,content).Result;
if(响应。IsSuccessStatusCode)
{
var responseString=response.Content.ReadAsStringAsync();
HttpContext.Current.Response.Write(responseString.Result);
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.End();
_webHelper.IsPostBeingDone=true;
}
其他的
{
抛出新的NopException();
}
返回;
}

首先,您的
catch
块是完全无用的。您应该完全删除它,以便在出现异常时可以实际看到异常情况。除此之外,实际的问题是什么?
responseString1
是否不包含您要查找的内容?里面有什么?到底是什么不起作用?无法将类型字符串转换为字节[]哪一行?你能理解吗?返回System.Text.Encoding.UTF8.GetString(响应);我已经编辑了答案,将函数post的返回类型从字节[]更改为字符串,它现在可以工作了。再次面对直播中的问题,没有显示返回页面。
var response = Http.Post("http://103.25.136.125/KPALServer/NetConnect.aspx", new NameValueCollection() {
    { "Merchant_ID", "170900010400235" },
    { "Order_NO", "1321" },
    { "Order_Amount", "900.00" } //similarly include all your post parameters
});
  public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
    {

    string orderNo = postProcessPaymentRequest.Order.Id.ToString();
    string amount = postProcessPaymentRequest.Order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture);
    string merchantId = _NetConnectPaymentSettings.CustomerId;  
    string date = DateTime.Now.ToString("dd/MM/yyyy");
    string time = DateTime.Now.ToString("HH:mm:ss");
    string urlNetConnect = _NetConnectPaymentSettings.PaymentPage;


    string checksum = Generate_MerchantRequest_Check_Sum("7C12B6AECC51A3F3189799098AB1981", merchantId, orderNo, amount, date, time);

    var client = new HttpClient();

    var values = new List<KeyValuePair<string, string>>();
    values.Add(new KeyValuePair<string, string>("Merchant_ID", merchantId));
    values.Add(new KeyValuePair<string, string>("Order_NO", orderNo));
    values.Add(new KeyValuePair<string, string>("Order_Amount", amount));

    values.Add(new KeyValuePair<string, string>("Date", date));
    values.Add(new KeyValuePair<string, string>("Time", time));

    values.Add(new KeyValuePair<string, string>("CheckSum", checksum));
    values.Add(new KeyValuePair<string, string>("Transaction_Desc", "Shop From SM Motors"));


    var content = new FormUrlEncodedContent(values);



    //HttpResponseMessage response = client.PostAsync("http://www.smmotors.org", content).Result;

HttpResponseMessage response = client.PostAsync(urlNetConnect, content).Result;

    if (response.IsSuccessStatusCode)
    {
        var responseString = response.Content.ReadAsStringAsync();


        HttpContext.Current.Response.Write(responseString.Result);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        HttpContext.Current.Response.End();
        _webHelper.IsPostBeingDone = true;

    }
    else
    {
        throw new NopException();
    }


       return;
}