[英]Create POST request to tumblr
我正在嘗試向tumblr api創建發布請求。 下面顯示的是上述api的摘錄:
The Write API is a very simple HTTP interface. To create a post, send a POST request to http://www.tumblr.com/api/write with the following parameters:
email - Your account's email address.
password - Your account's password.
type - The post type.
這些是必不可少的要素。 我想將照片發送到api。 根據API,這就是我構造請求的方式:
email: myEmail
password: myPassword
type: photo
data: "c:\\img.jpg"
多虧了dtb,我可以發送一個常規的帖子,該帖子僅使用字符串發送文本,不支持發送圖像。
var postData = new NameValueCollection
{
{ "email", email },
{ "password", password },
{ "type", regular },
{ "body", body }
};
using (var client = new WebClient())
{
client.UploadValues("http://www.tumblr.com/api/write", data: data);
}
這適用於發送常規數據,但是根據API,我應該以multipart/form-data
發送圖像,
或者,我可以通過Normal POST method
發送它,
但是,文件大小不如前者所允許。
client.UploadValues
支持數據:這使我可以將postData
傳遞給它。
client.UploadData
也可以,但是我無法弄清楚如何使用它,我已經參考了文檔。
另外,打開的文件不能在NameValueCollection中傳遞,這使我不知道如何發送請求。
請,如果有人知道答案,如果您能提供幫助,我將非常感謝。
您可以使用WebClient
類及其UploadValues
方法對application/x-www-form-urlencoded
有效負載執行POST請求:
var data = new NameValueCollection
{
{ "email", email },
{ "password", password },
{ "type", regular },
{ "body", body }
};
using (var client = new WebClient())
{
client.UploadValues("http://www.tumblr.com/api/write", data: data);
}
我能夠使用RestSharp
庫弄清楚這一點。
//Create a RestClient with the api's url
var restClient = new RestClient("http://tumblr.com/api/write");
//Tell it to send a POST request
var request = new RestRequest(Method.POST);
//Set format and add parameters and files
request.RequestFormat = DataFormat.Json; //I don't know if this line is necessary
request.AddParameter("email", "EMAIL");
request.AddParameter("password", "PASSWORD");
request.AddParameter("type", "photo");
request.AddFile("data", "C:\\Users\\Kevin\\Desktop\\Wallpapers\\1235698997718.jpg");
//Set RestResponse so you can see if you have an error
RestResponse response = restClient.Execute(request);
//MessageBox.Show(response) Perhaps I could wrap this in a try except?
它可以工作,但是我不確定這是否是最好的方法。
如果有人有其他建議,我會很樂意接受。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.