public static async Task <string> PostRequest(string postdata, DataUpdate _type) { try { string url = Settings.PublishDatabaseServerIp; CloudStreamCore.print("PUBLISHIP: " + url); int waitTime = 400; HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); if (CloudStreamCore.GetRequireCert(url)) { webRequest.ServerCertificateValidationCallback = delegate { return(true); }; } webRequest.Method = "POST"; webRequest.UserAgent = "CLOUDSTREAM APP v" + App.GetBuildNumber(); webRequest.Timeout = waitTime * 10; webRequest.ReadWriteTimeout = waitTime * 10; webRequest.ContinueTimeout = waitTime * 10; webRequest.Headers.Add("TYPE", ((int)_type).ToString()); try { HttpWebRequest _webRequest = webRequest; Stream postStream = await _webRequest.GetRequestStreamAsync(); string requestBody = postdata; // --- RequestHeaders --- byte[] byteArray = Encoding.UTF8.GetBytes(requestBody); postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); // BEGIN RESPONSE try { HttpWebRequest request = webRequest; HttpWebResponse response = (HttpWebResponse)(await webRequest.GetResponseAsync()); using StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()); try { string s = await httpWebStreamReader.ReadToEndAsync(); CloudStreamCore.print("RESPONSEF FROM POST::: " + s); } catch (Exception) { return(""); } } catch (Exception _ex) { CloudStreamCore.error("FATAL EX IN POST2: " + _ex); } } catch (Exception _ex) { CloudStreamCore.error("FATAL EX IN POSTREQUEST" + _ex); } return(""); } catch (Exception _ex) { CloudStreamCore.error(_ex); return(""); } }
public static async Task <string> _PostRequest(string postdata, DataUpdate _type) { try { string url = Settings.PublishDatabaseServerIp; CloudStreamCore.print("PUBLISHIP: " + url); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(postdata); request.ContentType = "application/json"; request.ContentLength = byteArray.Length; request.Headers.Add("TYPE", ((int)_type).ToString()); Stream dataStream = await request.GetRequestStreamAsync(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = await request.GetResponseAsync(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = await reader.ReadToEndAsync(); reader.Close(); dataStream.Close(); response.Close(); return(responseFromServer); } catch (WebException e) { CloudStreamCore.error(e); try { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); using Stream data = response.GetResponseStream(); using var reader = new StreamReader(data); string text = reader.ReadToEnd(); Console.WriteLine(text); } return(""); } catch (Exception _ex) { CloudStreamCore.error(_ex); return(""); } } catch (Exception _ex) { CloudStreamCore.error(_ex); return(""); } }
public static async Task <string> PostRequest(string url, string postdata, string rtype = "POST", string auth = null, string content = "application/x-www-form-urlencoded") { try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = rtype; if (auth.IsClean()) { request.Headers["Authorization"] = auth; } byte[] byteArray = Encoding.UTF8.GetBytes(postdata); request.ContentType = content; request.ContentLength = byteArray.Length; Stream dataStream = await request.GetRequestStreamAsync(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = await request.GetResponseAsync(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = await reader.ReadToEndAsync(); reader.Close(); dataStream.Close(); response.Close(); return(responseFromServer); } catch (WebException e) { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } } return(""); } catch (Exception _ex) { CloudStreamCore.error(_ex); return(""); } }
public static async Task <string> GetRequest(string url, string auth = null) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; request.Method = "GET"; if (auth.IsClean()) { request.Headers["Authorization"] = auth; } request.ContentType = "text/html; charset=UTF-8"; request.UserAgent = CloudStreamCore.USERAGENT; request.Headers.Add("Accept-Language", "en-US,en;q=0.5"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.Headers.Add("TE", "Trailers"); using HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync(); using Stream stream = response.GetResponseStream(); using StreamReader reader = new StreamReader(stream, Encoding.UTF8); string result = await reader.ReadToEndAsync(); return(result); } catch (WebException e) { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } } return(""); } catch (Exception _ex) { CloudStreamCore.error(_ex); return(""); } }
public static async Task <AniListUser?> GetUser(bool setSettings = true) { try { const string q = @" { Viewer { id name avatar { large } } }" ; var data = await PostApi("https://graphql.anilist.co", $"&query={q}"); if (!data.IsClean()) { return(null); } AniListRoot userData = JsonConvert.DeserializeObject <AniListRoot>(data); var u = userData.data.Viewer; var user = new AniListUser() { id = u.id, name = u.name, picture = u.avatar.large, }; if (setSettings) { Settings.CurrentAniListUser = user; } App.SaveData(); return(user); } catch (Exception _ex) { CloudStreamCore.error(_ex); return(null); } }