簡體   English   中英

創建對tumblr的POST請求

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM