2017-01-24 13 views
0

私はC#webapi呼び出しに変換したいCURLコマンドを以下に示します。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#コードの例があります。 メール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); 
      } 
     } 
+0

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

+0

@SouvikGhosh申し訳ありません。あなたが示唆したのは、curl.exeをコードで使用したカールの例です。私はそれを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 \この部分をリクエストして追加していますか? –