我正在尝试将文件发布到 PushBullet API 服务。
API 的信息站点显示了一个示例:
curl -i https://api.pushbullet.com/api/pushes \
-u API_KEY:
-F device_iden=u1qSJddxeKwOGuGW
-F type=file
-F <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0167686d643c41756472752f757975" rel="noreferrer noopener nofollow">[email protected]</a>
我正在尝试使用 C# 中的 HttpClient
来完成此操作
我知道如何使用 Httpclient
、使用 MultipartFormDataContent
发布文件,但是如何将 device_iden 和类型信息添加到客户端?
我正在用这个
using (var content = new MultipartFormDataContent())
{
try
{
content.Add(new StreamContent(new FileStream(pathFile, FileMode.Open, FileAccess.Read)));
var resp = wc.PostAsync(new Uri(baseUri, "api/pushes"), content);
}
catch (Exception)
{
throw;
}
}
[[新增]]
使用 cURL 和 fiddler 这将是 POST
POST http://\/ HTTP/1.1
Authorization: Basic d45YQkxueWswSmxQRTYFjc1deUzNo8UtueVZpaktIZm34anVqdU9NZerWYmFlOp==
User-Agent: curl/7.33.0
Host: \
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 787
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------886f3981539a91b3
--------------------------886f3981539a91b3
Content-Disposition: form-data; name="device_iden"
ujuOMfjVbaedjz7O3P0Jl6
--------------------------886f3981539a91b3
Content-Disposition: form-data; name="type"
file
--------------------------886f3981539a91b3
Content-Disposition: form-data; name="file"; filename="img.gif"
Content-Type: application/octet-stream
GIF89a������@�;A�;A�;A�;B�<B�<������!�Created with GIMP�!�
��,����������x�����I��8KȻ�`(�$��h��l{�p�tm߮��x����P,���҃l:q���I���u�Mf����
��d�y}LG��{���J�>���W};������^�1�����������
--------------------------886f3981539a91b3--
最佳答案
尝试使用 fiddler 后终于可以发布该帖子:
第一:
public static void PushFile(string pathFile, string iddev)
{
string name = Path.GetFileName(pathFile);
using (var wc = GetClient())
{
using (var multiPartCont = new MultipartFormDataContent())
{
multiPartCont.Add(addStringContent("device_iden", iddev));
multiPartCont.Add(addStringContent("type","file"));
multiPartCont.Add(addStreamContent(new FileStream(pathFile,FileMode.Open),name ));
try
{
var resp = wc.PostAsync(new Uri(baseUri, "api/pushes"), multiPartCont);
Task<string> result = resp.Result.Content.ReadAsStringAsync();
//string resultado = result.Result;
}
catch (Exception)
{
throw;
}
}
}
}
然后是创建字符串内容和流内容的方法
private static StreamContent addStreamContent(Stream stream, string filename )
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"file\"",
FileName = "\""+filename+"\""
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return fileContent;
}
private static StringContent addStringContent(string name, string content)
{
var fileContent = new StringContent(content);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"" + name + "\""
};
return fileContent;
}
关于c# - cURL 发布到 HTTPCLient C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352886/