2014-11-04 51 views
0

我試圖通過C#/ Java連接到傳輸rpc接口來獲取一些信息。 https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt 不幸的是我有問題要獲取正確的HTTP-Post來訪問接口。 例如,如果我嘗試在C#:連接到json-rpc接口

using (var client = new WebClient()) 
    { 
     var values = new NameValueCollection(); 
     values["torrent-get"] = "id"; 

     var response = client.UploadValues("http://ip:9091/transmission/rpc", values); 

     var responseString = Encoding.Default.GetString(response); 
     Console.WriteLine("" + responseString); 
    } 

或者,如果我使用:

using (var webClient = new WebClient()) 
     { 
      String json = "{\"arguments\": {\"fields\": [ \"id\", \"name\", \"totalSize\" ],\"ids\": [ 7, 10 ]},\"method\": \"torrent-get\",\"tag\": 39693}"; 
      var response = webClient.UploadString("http://192.168.240.171:9091/transmission/rpc", "POST", json); 
      Console.WriteLine(""+response); 
     } 

我得到以下錯誤:

型System.Net的」未處理的異常。 WebException'發生在System.dll中 附加信息:遠程服務器返回了一個異常:(409)衝突。

回答

1

您必須保存X-傳輸會話Id在409響應中提供並重新提交了X-傳輸會話Id屬性的請求添加到您的請求頭。

實施例在Java:

int index = responseString.indexOf("X-Transmission-Session-Id:"); 
String sessionId = responseString.substring(index + 27, index + 75); 
connection.setRequestProperty("X-Transmission-Session-Id", sessionId); 
相關問題