- Create a Console app project.
- Make Sure that the code compiles and runs.
- If you are within a proxy, you can do it programmatically like this:
System.Net.WebRequest.DefaultWebProxy = new WebProxy(ip,port);
| using System; | |
| using System.Net; | |
| using System.Threading; | |
| class Request : IDisposable { | |
| private string data; | |
| private Exception exception; | |
| private readonly string url; | |
| private readonly ReaderWriterLockSlim rwlockSlim; | |
| public Request(string url) { | |
| rwlockSlim = new ReaderWriterLockSlim(); | |
| this.url = url; | |
| } | |
| public void Dispose() { | |
| rwlockSlim.Dispose(); | |
| } | |
| private void BypassAllCertificates() { | |
| ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain,error) => true; | |
| } | |
| private string Send(string url) { | |
| using (WebClient wc = new WebClient()) { | |
| BypassAllCertificates(); | |
| return wc.DownloadString(url); | |
| } | |
| } | |
| public void Run() { | |
| rwlockSlim.EnterWriteLock(); | |
| try { | |
| Console.WriteLine($"Running({url}) on => {Thread.CurrentThread}({Thread.CurrentThread.Name})"); | |
| data = Send(url); | |
| } catch(Exception e) { | |
| this.exception = e; | |
| } finally { | |
| rwlockSlim.ExitWriteLock(); | |
| } | |
| } | |
| public string Get() { | |
| rwlockSlim.EnterReadLock(); | |
| try { | |
| if (exception == null) | |
| return data; | |
| throw exception; | |
| } finally { | |
| rwlockSlim.ExitReadLock(); | |
| } | |
| } | |
| } |
| using System; | |
| using System.Threading; | |
| using System.Diagnostics; | |
| public class Runner { | |
| public static void Main(string[] args) { | |
| var placesNearbyUrl = "https://geographic-services.herokuapp.com/places/nearby?lat=19.01&lon=72.8&radius=25&unit=km"; | |
| var weatherUrl = "https://geographic-services.herokuapp.com/weather?lat=19.01&lon=72.8"; | |
| using (var placesNearbyRequest = new Request(placesNearbyUrl)) { | |
| using(var weatherRequest = new Request(weatherUrl)) { | |
| var placesNearbyThread = new Thread(() => placesNearbyRequest.Run()); | |
| placesNearbyThread.Name = "placesNearby"; | |
| var weatherThread = new Thread(() => weatherRequest.Run()); | |
| weatherThread.Name = "weather"; | |
| var watch = Stopwatch.StartNew(); | |
| placesNearbyThread.Start(); | |
| weatherThread.Start(); | |
| do { | |
| Console.WriteLine("==========> Waiting For Results.... <=========="); | |
| Thread.Sleep(100); | |
| } while (placesNearbyRequest.Get() == null && weatherRequest.Get() == null); | |
| watch.Stop(); | |
| Console.WriteLine($"Time Taken {watch.ElapsedMilliseconds}(ms)"); | |
| string placesNearbyAndWeatherData = $@"{{ ""weather"" : {weatherRequest.Get()}, ""placesNearby"": {placesNearbyRequest.Get()} }}"; | |
| Console.WriteLine(placesNearbyAndWeatherData); | |
| } | |
| } | |
| } | |
| } |