C#web api请求的Curl命令

C#web api请求的Curl命令,c#,rest,curl,C#,Rest,Curl,我有以下CURL命令,我想将其转换为c#webapi调用 curl -H "Content-Type: application/x-www-form-urlencoded" \ -H "client_id: YOUR-CLIENT-ID" \ -H "client_secret: YOUR-CLIENT-SECRET" \ -d "mailbox_id=YOUR-MAILBOX-ID" \ --data-urlencode email@/path/to/email/contents.eml \

我有以下CURL命令,我想将其转换为c#webapi调用

curl -H "Content-Type: application/x-www-form-urlencoded" \
-H "client_id: YOUR-CLIENT-ID" \
-H "client_secret: YOUR-CLIENT-SECRET" \
-d "mailbox_id=YOUR-MAILBOX-ID" \
--data-urlencode email@/path/to/email/contents.eml \
"https://api.abc.com/v2/add"
我需要帮助的部分是如何将邮箱id和数据添加为url编码。它还从磁盘上接收电子邮件。我可以添加字节数组吗

下面是我的c代码示例。 我只需要添加邮件ID和电子邮件内容在那里

  public static string WebRequestWithByte(byte[] postData)
    {
        var url = @"https://api.abv.com/v2/add";
        var clientId = "wewew";
        var clientSecret = "df58ffed4bc0bc41";


        string ret = string.Empty;

        StreamWriter requestWriter;

        var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.Headers.Add("client_id", clientId);
            webRequest.Headers.Add("client_secret", clientSecret);
            webRequest.Method = "POST";
            webRequest.ServicePoint.Expect100Continue = false;
            webRequest.Timeout = 20000;
            webRequest.ContentType = "application/x-www-form-urlencoded";
            //POST the data.
            using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                requestWriter.Write(payloadStr);
            }
        }

您可以使用下面的代码。我想你想打个电话。您可以将数据添加到
字典中
,然后将其转换为字节数组

-d
--data
是相同的

此代码仅供参考,您可以进行相应的修改

private static String Post(String url,
            int timeout)
    {

        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
        req.Method = "POST";
        if (timeout != 0)
            req.Timeout = timeout;


        req.Headers.Set("client_id", "client_id");
        req.Headers.Set("client_secret", "client_secret");

        Dictionary<String, String> data = new Dictionary<String, String>();
        data.Add("mailbox_id", "mailbox_id");

        byte[] rawData = Encoding.UTF8.GetBytes(Encode(data));
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = rawData.Length;

        String ret = null;
        using (Stream s = req.GetRequestStream())
        {
            s.Write(rawData, 0, rawData.Length);
            using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
            {
                using (Stream s2 = res.GetResponseStream())
                {
                    using (StreamReader r = new StreamReader(s2, Encoding.UTF8))
                    {
                        ret = r.ReadToEnd();
                    }
                }
            }
        }
        return ret;
    }
    public static String Encode(Dictionary<String, String> data)
    {
        StringBuilder s = new StringBuilder();
        foreach (KeyValuePair<String, String> o in data)
        {
            s.AppendFormat("{0}={1}&", o.Key, HttpUtility.UrlEncode(o.Value));
        }

        char[] trim = { '&' };
        String ret = s.ToString().TrimEnd(trim);
        return ret;
    }
私有静态字符串Post(字符串url,
整数超时)
{
HttpWebRequest req=WebRequest.Create(url)为HttpWebRequest;
请求方法=“POST”;
如果(超时!=0)
请求超时=超时;
请求标题集(“客户端id”、“客户端id”);
请求头集合(“客户机秘密”、“客户机秘密”);
字典数据=新字典();
添加(“邮箱id”、“邮箱id”);
byte[]rawData=Encoding.UTF8.GetBytes(Encode(data));
req.ContentType=“应用程序/x-www-form-urlencoded”;
req.ContentLength=rawData.Length;
字符串ret=null;
使用(streams=req.GetRequestStream())
{
s、 写入(rawData,0,rawData.Length);
使用(HttpWebResponse res=req.GetResponse()作为HttpWebResponse)
{
使用(流s2=res.GetResponseStream())
{
使用(StreamReader r=新的StreamReader(s2,Encoding.UTF8))
{
ret=r.ReadToEnd();
}
}
}
}
返回ret;
}
公共静态字符串编码(字典数据)
{
StringBuilder s=新的StringBuilder();
foreach(数据中的KeyValuePair o)
{
s、 AppendFormat(“{0}={1}&”,o.Key,HttpUtility.UrlEncode(o.Value));
}
char[]trim={'&'};
String ret=s.ToString().TrimEnd(trim);
返回ret;
}

可能重复-@souvikghosh抱歉没有。您建议的是在代码中使用curl.exe的curl示例。我希望将其转换为c#web api调用。谢谢。但是我们在哪里添加了数据urlencode email@/path/to/email/contents.eml\请求中的这一部分?