2009-08-07 65 views
1

我懷疑任何人都有與此特定任務相關的具體經驗,但也許你可以發現我的問題。我試圖使鋰通話(論壇軟件)放置一票投票通知,他們的文檔顯示此:在VB.Net做一些restapi和堅持的東西

實例網址: http://community.lithium.com/community-name/restapi/vc/polls/id/15/votes/place

查詢參數: poll.choice(必填): - 選擇投票。選擇由表單ID/choice_id的字符串指定的地方choice_id是投票選擇

HTTP方法的ID: POST

所以我的代碼看起來是這樣的:

Dim _Response As New XmlDocument  
Dim RestApiRoot As String = "http://example.com/community-name/restapi/vc/polls/id/6/votes/place" 

APIRequest = WebRequest.Create(RestApiRoot) 
APIRequest.Method = "POST"  
APIRequest.Headers.Add("poll.choice", HttpContext.Current.Server.UrlEncode("id/" & _choiceID.ToString)) 

APIResponse = APIRequest.GetResponse()  
APIReader = New StreamReader(APIResponse.GetResponseStream()) 

_Response.LoadXml(APIReader.ReadToEnd()) 

APIResponse.Close() 

我無法成功註冊投票,他們說這是因爲poll.choice參數沒有出現在頭文件中,但是如果我逐步調試,我可以在Header Keys/Items中看到它。

任何人有任何線索我可能做錯了什麼?

回答

1

我正是用RestSharp這個開源的REST框架來做到這一點。它適用於Lithium REST API。

你的代碼將看起來像這樣使用RestSharp:

您將創建一個類看起來像從鋰API的響應,在這種情況下「響應」。它看起來就像這樣(對不起,你必須這樣翻譯成VB.NET):

public class LithiumResponse 
{ 
    public string status { get; set; } 
    public string value { get; set; } 
    public string message { get; set; } 
} 

現在RestSharp將用它來拍攝,結果是這樣的:

// create the request 
var request = new RestRequest(); 
request.Verb = Method.POST; 
request.BaseUrl = "http://example.com/community-name"; 

// specify the action 
request.Action = "restapi/vc/polls/id/6/votes/place"; 

// add the parameters 
request.AddParameter("poll.choice", "id/" + _choiceID.ToString()); 

// now create a RestClient to execute the request, 
// telling it to put the results in your "reponse" class 
var client = new RestClient(); 
var lithiumresponse = client.Execute<LithiumResponse>(request); 

// now you can check the status property of your class to 
// see if it was successful 
if (lithiumresponse.status == "success") 
    // you successfully placed a vote 

我用RestSharp與鋰電池API進行大量互動,並使其簡單死腦筋。非常棒的圖書館。