连接到json-rpc接口

本文关键字:接口 json-rpc 连接 | 更新日期: 2023-09-27 18:28:58

我正试图通过C#/Java连接到传输rpc接口,以获取一些信息。https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt不幸的是,我在获取正确的HTTPPost以访问接口方面遇到了问题。例如,如果我在C#中尝试这个:

    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.dll中发生类型为"System.Net.WebException"的未处理异常附加信息:远程服务器返回异常:(409)冲突

连接到json-rpc接口

您必须保存409响应中提供的X-Transmission-Session-Id,并在请求标头中添加X-Transmission-Ssession-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);