private static void DoQueryWithProxy(string proxyIP, int proxyPort, string mobileNumber, CancellationToken ct, WPExists wPExists) { if (ct.IsCancellationRequested) { try { wPExists.SetError = "Operation Canceled."; wPExists.ProcessCompleted = wPExists.StopRequested = true; ct.ThrowIfCancellationRequested(); } catch (Exception ex) { wPExists.SetError = ex.Message; } } WAResponse wa = new WAResponse(); wa.Number = mobileNumber; try { string url = SettingConfig.API_BASE_URL + mobileNumber; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); WebProxy myproxy = new WebProxy(proxyIP, proxyPort); myproxy.BypassProxyOnLocal = false; request.Proxy = myproxy; request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Thread ts = new Thread(delegate() { Stream newStream = response.GetResponseStream(); StreamReader sr = new StreamReader(newStream); wa.Exists = sr.ReadToEnd(); wa.StatusCode = Convert.ToInt32(response.StatusCode); wa.StatusDescription = response.StatusDescription; }); ts.IsBackground = true; ts.Start(); } catch (Exception ex) { wPExists.SetError = ex.Message + Environment.NewLine + ex.InnerException; wa.Exception = ex.Message; wa.InnerException = ex.InnerException.Message; } finally { wPExists.WAResponse = wa; } }
private static void DoQuery(string mobileNumber, CancellationToken ct, WPExists wPExists) { if (ct.IsCancellationRequested) { try { wPExists.SetError = "Operation Canceled."; wPExists.ProcessCompleted = wPExists.StopRequested = true; ct.ThrowIfCancellationRequested(); } catch (Exception ex) { wPExists.SetError = ex.Message; } } WAResponse wa = new WAResponse(); wa.Number = mobileNumber; try { string url = SettingConfig.API_BASE_URL + mobileNumber; var client = new RestClient(url); client.Timeout = -1; var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(request); var data = response.Content; Thread ts = new Thread(delegate() { wa.Exists = response.Content; wa.StatusCode = Convert.ToInt32(response.StatusCode); wa.StatusDescription = response.StatusDescription; }); ts.IsBackground = true; ts.Start(); } catch (Exception ex) { wPExists.SetError = ex.Message + Environment.NewLine + ex.InnerException; wa.Exception = ex.Message; wa.InnerException = ex.InnerException.Message; } finally { wPExists.WAResponse = wa; } }