private T PerformRequestInternal <T>(Dictionary <string, string> queryparams) { queryparams["format"] = "json"; string query = EncodeQueryString(queryparams); byte[] data = ENCODING.GetBytes(query); System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_controlUri); req.Method = "POST"; req.ContentLength = data.Length; req.ContentType = "application/x-www-form-urlencoded ; charset=" + ENCODING.BodyName; req.Headers.Add("Accept-Charset", ENCODING.BodyName); if (m_xsrftoken != null) { req.Headers.Add(XSRF_HEADER, m_xsrftoken); } req.UserAgent = "Duplicati TrayIcon Monitor, v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); if (req.CookieContainer == null) { req.CookieContainer = new System.Net.CookieContainer(); } if (m_authtoken != null) { req.CookieContainer.Add(new System.Net.Cookie(AUTH_COOKIE, m_authtoken, "/", req.RequestUri.Host)); } if (m_xsrftoken != null) { req.CookieContainer.Add(new System.Net.Cookie(XSRF_COOKIE, m_xsrftoken, "/", req.RequestUri.Host)); } //Wrap it all in async stuff Duplicati.Library.Utility.AsyncHttpRequest areq = new Library.Utility.AsyncHttpRequest(req); using (System.IO.Stream s = areq.GetRequestStream()) s.Write(data, 0, data.Length); //Assign the timeout, and add a little processing time as well if (queryparams["action"] == "get-current-state" && queryparams.ContainsKey("duration")) { areq.Timeout = (int)(Duplicati.Library.Utility.Timeparser.ParseTimeSpan(queryparams["duration"]) + TimeSpan.FromSeconds(5)).TotalMilliseconds; } using (System.Net.HttpWebResponse r = (System.Net.HttpWebResponse)areq.GetResponse()) using (System.IO.Stream s = areq.GetResponseStream()) if (typeof(T) == typeof(string)) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { s.CopyTo(ms); return((T)(object)ENCODING.GetString(ms.ToArray())); } } else { using (var sr = new System.IO.StreamReader(s, ENCODING, true)) return(Serializer.Deserialize <T>(sr)); } }
private SaltAndNonce GetSaltAndNonce() { var httpOptions = new Duplicati.Library.Modules.Builtin.HttpOptions(); httpOptions.Configure(m_options); using (httpOptions) { var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_baseUri + LOGIN_SCRIPT); req.Method = "POST"; req.UserAgent = "Duplicati TrayIcon Monitor, v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; req.Headers.Add(TRAYICONPASSWORDSOURCE_HEADER, m_TrayIconHeaderValue); req.ContentType = "application/x-www-form-urlencoded"; Duplicati.Library.Utility.AsyncHttpRequest areq = new Library.Utility.AsyncHttpRequest(req); var body = System.Text.Encoding.ASCII.GetBytes("get-nonce=1"); using (var f = areq.GetRequestStream(body.Length)) f.Write(body, 0, body.Length); using (var r = (System.Net.HttpWebResponse)areq.GetResponse()) using (var s = areq.GetResponseStream()) using (var sr = new System.IO.StreamReader(s, ENCODING, true)) return(Serializer.Deserialize <SaltAndNonce>(sr)); } }
private T PerformRequestInternal <T>(string method, string endpoint, Dictionary <string, string> queryparams) { queryparams["format"] = "json"; string query = EncodeQueryString(queryparams); var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(m_apiUri + endpoint + '?' + query)); req.Method = method; req.Headers.Add("Accept-Charset", ENCODING.BodyName); if (m_xsrftoken != null) { req.Headers.Add(XSRF_HEADER, m_xsrftoken); } req.UserAgent = "Duplicati TrayIcon Monitor, v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); req.Headers.Add(TRAYICON_HEADER, "true"); if (req.CookieContainer == null) { req.CookieContainer = new System.Net.CookieContainer(); } if (m_authtoken != null) { req.CookieContainer.Add(new System.Net.Cookie(AUTH_COOKIE, m_authtoken, "/", req.RequestUri.Host)); } if (m_xsrftoken != null) { req.CookieContainer.Add(new System.Net.Cookie(XSRF_COOKIE, m_xsrftoken, "/", req.RequestUri.Host)); } //Wrap it all in async stuff var areq = new Library.Utility.AsyncHttpRequest(req); req.AllowWriteStreamBuffering = true; //Assign the timeout, and add a little processing time as well if (endpoint.Equals("/serverstate", StringComparison.InvariantCultureIgnoreCase) && queryparams.ContainsKey("duration")) { areq.Timeout = (int)(Duplicati.Library.Utility.Timeparser.ParseTimeSpan(queryparams["duration"]) + TimeSpan.FromSeconds(5)).TotalMilliseconds; } using (var r = (System.Net.HttpWebResponse)areq.GetResponse()) using (var s = areq.GetResponseStream()) if (typeof(T) == typeof(string)) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { s.CopyTo(ms); return((T)(object)ENCODING.GetString(ms.ToArray())); } } else { using (var sr = new System.IO.StreamReader(s, ENCODING, true)) return(Serializer.Deserialize <T>(sr)); } }
private SaltAndNonce GetSaltAndNonce() { var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_baseUri + LOGIN_SCRIPT + "?get-nonce=1"); req.Method = "GET"; req.UserAgent = "Duplicati TrayIcon Monitor, v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); Duplicati.Library.Utility.AsyncHttpRequest areq = new Library.Utility.AsyncHttpRequest(req); using (var r = (System.Net.HttpWebResponse)areq.GetResponse()) using (var s = areq.GetResponseStream()) using (var sr = new System.IO.StreamReader(s, ENCODING, true)) return(Serializer.Deserialize <SaltAndNonce>(sr)); }