curl - ASP.NET MVC curl 等价物

标签 curl asp.net-mvc-4

我很难找到这方面的很多细节。我需要使用处理信用卡的 api,它使用 curl。所有文档都在 php 中,虽然我可以使用 php,但我的主站点完全在使用 razor View 引擎的 MVC4 中。我需要将它从 php 转换为 .net 中可用的东西

$ curl https://api.stripe.com/v1/customers -u *private key here*: 
       -d "description=Customer for test@example.com" -d "card[number]=4242424242424242" 
       -d "card[exp_month]=12" -d "card[exp_year]=2013"

在此先感谢您的时间

最佳答案

来自 curl manual

  • -u, --user <user:password>是用户凭据
  • -d, --data <data>是POST数据

  • 所以你可以将其“解码”为:
    using (var wc = new System.Net.WebClient())
    {
        // data
        string parameters = string.Concat("description=", description, "&amp;card[number]=" , cardNumber, "&amp;card[exp_month]=", cardExpirationMonth, "&amp;card[exp_year]=", cardExpirationYear),
               url = "https://api.stripe.com/v1/customers;
    
        // let's fake it and make it was a browser requesting the data
        wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    
        // credentials
        wc.Credentials = new System.Net.NetworkCredential("*private key here*", "");
    
        // make it a POST instead of a GET
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    
        // send and get answer in a string
        string result = wc.UploadString(url, parameters);
    }
    

    更新了来自 existing answer 的 POST 调整.

    关于curl - ASP.NET MVC curl 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014623/

    相关文章:

    php - 在 php 脚本仍在执行时回显消息

    PHP CURL 到 CodeIgniter Controller

    node.js - 乱码而不是 html 响应作为 node.request 中的正文

    c# - ASP.NET MVC 4 - 以 HTML、JSON 或 XML 响应的正确方式

    jquery - 如何在 mvc razor 中显示/隐藏网格行

    c# - 仅适用于 ie8 的复选框样式表

    javascript - Javascript 中的 C# Razor foreach 循环语法

    asp.net-mvc-4 - 使用信号器时的 session 超时

    linux - wget 打破内容处置

    php - 使用 curl 下载大文件