我正在尝试将文件发布到PushBullet API服务。
API的信息网站显示了一个示例:
curl -i https://api.pushbullet.com/api/pushes \
-u API_KEY:
-F device_iden=u1qSJddxeKwOGuGW
-F type=file
-F file=@test.txt
我正在尝试使用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--
答案 0 :(得分:5)
在与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;
}
答案 1 :(得分:0)
curl中的-u是http身份验证。看起来pushbullet希望您使用API密钥登录。使用httpclient,你会使用;
httpClient.DefaultRequestHeaders.Authorization = CreateBasicHeader("username", "password");
其余的将是GET或POST变量(我不确定pushbullet的预期)。在您的代码中,您可以通过POST设置它。因此,您可以使用;
附加到“content”变量 "device_iden=u1qSJddxeKwOGuGW&type=file";
答案 2 :(得分:0)
创建-u
数据的另一种方法是使用HttpClientHandler
NetworkCredential
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential(API_KEY, "")
};
var client = new HttpClient(handler);
这将创建一个基本的http身份验证标头