/*Session handling*/ /// <summary> /// Send a LogIn request, this must be called before anything else in this class. /// </summary> /// <param name="userName">The user name of OpenSubtitles</param> /// <param name="password">The password</param> /// <param name="language">The language, usally en</param> /// <returns>Status of the login operation</returns> public static IMethodResponse LogIn(string userName, string password, string language) { // Method call .. List<IXmlRpcValue> parms = new List<IXmlRpcValue>(); parms.Add(new XmlRpcValueBasic(userName)); parms.Add(new XmlRpcValueBasic(password)); parms.Add(new XmlRpcValueBasic(language)); parms.Add(new XmlRpcValueBasic(XML_PRC_USERAGENT)); XmlRpcMethodCall call = new XmlRpcMethodCall("LogIn", parms); OSHConsole.WriteLine("Sending LogIn request to the server ...", DebugCode.Good); //File.WriteAllText(".\\request.txt", Encoding.UTF8.GetString(XmlRpcGenerator.Generate(call))); // Send the request to the server string response = Utilities.GetStreamString(Utilities.SendRequest(XmlRpcGenerator.Generate(call), XML_PRC_USERAGENT)); if (!response.Contains("ERROR:")) { // No error occur, get and decode the response. We expect Struct here. XmlRpcMethodCall[] calls = XmlRpcGenerator.DecodeMethodResponse(response); if (calls.Length > 0) { if (calls[0].Parameters.Count > 0) { XmlRpcValueStruct mainStruct = (XmlRpcValueStruct)calls[0].Parameters[0]; MethodResponseLogIn re = new MethodResponseLogIn("Success", "Log in successful."); foreach (XmlRpcStructMember MEMBER in mainStruct.Members) { switch (MEMBER.Name) { case "token": re.Token = TOKEN = MEMBER.Data.Data.ToString(); OSHConsole.WriteLine(MEMBER.Name + "= " + MEMBER.Data.Data.ToString()); break; case "seconds": re.Seconds = (double)MEMBER.Data.Data; OSHConsole.WriteLine(MEMBER.Name + "= " + MEMBER.Data.Data.ToString()); break; case "status": re.Status = MEMBER.Data.Data.ToString(); OSHConsole.WriteLine(MEMBER.Name + "= " + MEMBER.Data.Data.ToString()); break; } } return re; } } } else { OSHConsole.WriteLine(response, DebugCode.Error); return new MethodResponseError("Fail", response); } return new MethodResponseError("Fail", "Log in failed !"); }
/// <summary> /// Log out from the server. Call this to terminate the session. /// </summary> /// <returns></returns> public static IMethodResponse LogOut() { if (TOKEN == "") { OSHConsole.WriteLine("Can't do this call, 'token' value not set. Please use Log In method first.", DebugCode.Error); return new MethodResponseError("Fail", "Can't do this call, 'token' value not set. Please use Log In method first."); } // Method call .. List<IXmlRpcValue> parms = new List<IXmlRpcValue>(); parms.Add(new XmlRpcValueBasic(TOKEN, XmlRpcBasicValueType.String)); XmlRpcMethodCall call = new XmlRpcMethodCall("LogOut", parms); OSHConsole.WriteLine("Sending LogOut request to the server ...", DebugCode.Good); // Send the request to the server string response = Utilities.GetStreamString(Utilities.SendRequest(XmlRpcGenerator.Generate(call), XML_PRC_USERAGENT)); if (!response.Contains("ERROR:")) { // No error occur, get and decode the response. We expect Struct here. XmlRpcMethodCall[] calls = XmlRpcGenerator.DecodeMethodResponse(response); if (calls.Length > 0) { if (calls[0].Parameters.Count > 0) { XmlRpcValueStruct strct = (XmlRpcValueStruct)calls[0].Parameters[0]; OSHConsole.WriteLine("STATUS=" + ((XmlRpcValueBasic)strct.Members[0].Data).Data.ToString()); OSHConsole.WriteLine("SECONDS=" + ((XmlRpcValueBasic)strct.Members[1].Data).Data.ToString()); MethodResponseLogIn re = new MethodResponseLogIn("Success", "Log out successful."); re.Status = ((XmlRpcValueBasic)strct.Members[0].Data).Data.ToString(); re.Seconds = (double)((XmlRpcValueBasic)strct.Members[1].Data).Data; return re; } } } else { OSHConsole.WriteLine(response, DebugCode.Error); return new MethodResponseError("Fail", response); } return new MethodResponseError("Fail", "Log out failed !"); }