private async Task<string> GetResponseAsync(PostDataWriter args) { //Data request HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; handler.AllowAutoRedirect = args.method == SupportedMethods.GetPage; handler.UseCookies = true; HttpClient client = new HttpClient(handler); HttpResponseMessage response = null; HttpRequestMessage message = new HttpRequestMessage(args.method == SupportedMethods.GetPage ? HttpMethod.Get : HttpMethod.Post, args.GetUrl()); #if DEBUG Debug.WriteLine(args.GetUrl() + " / " + args.GetPostDataString()); #endif if (args.method != SupportedMethods.GetPage) { StringContent content = new StringContent(args.GetPostDataString()); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); message.Content = content; } message.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("gzip")); response = await client.SendAsync(message); string resp; if (response.StatusCode == HttpStatusCode.Redirect) { resp = String.Empty; } else { resp = await response.Content.ReadAsStringAsync(); } return resp; }
public async Task<string> MakeOperationAsync(SupportedModules module, SupportedMethods method, string syscode = "", string resStr = "", string genMod = "", bool forAuth = false) { if (IsNetworkAvailable() && (forAuth || await IsValidAccountAsync(forAuth))) { PostDataWriter args = new PostDataWriter() { module = module, method = method, cidReq = syscode, resStr = resStr, GenMod = genMod }; String strContent = ""; try { strContent = await GetResponseAsync(args); #if DEBUG Debug.WriteLine("Call for :" + module + "/" + method + "\nResponse :" + strContent + "\n"); #endif if (args.method != SupportedMethods.GetPage && strContent.StartsWith("<")) { strContent = ""; } } catch (Exception ex) { LastException = ex; } return strContent; } else { LastException = new NetworkException("Network Unavailable"); return null; } }