【发布时间】:2017-01-24 20:13:28
【问题描述】:
我有以下 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# 代码示例。 我只需要在其中添加 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);
}
}
【问题讨论】:
-
@SouvikGhosh对不起,不是。您建议的是在代码中使用 curl.exe 的 curl 示例。我希望将其转换为 c# web api 调用。