2017-01-24 69 views
0

我有以下CURL命令,我想将其转换为C#webapi调用。对C#web api请求的curl命令

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 protected]/path/to/email/contents.eml \ 
"https://api.abc.com/v2/add" 

我需要帮助的部分是如何添加邮箱ID和数据作为URL编码。它也从磁盘中获取电子邮件。我可以只添加字节数组吗?

这是我的C#代码的例子。 我只需要在那里添加mailid和电子邮件内容。

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); 
      } 
     } 
+0

可能式两份http://stackoverflow.com/questions/35463199/how-to-call-a-rest-web-api-for-access-token-with-these-curl-commands-in -c-shar –

+0

@SouvikGhoshSorry它不是。你所建议的是在代码中使用curl.exe的curl例子。我正在寻找转换成C#web API调用。 –

回答

0

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

-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; 
    } 
+0

感谢您的支持。但我们要在哪里添加--data-urlencode [email protected]/path/to/email/contents.eml \这部分请求? –