GetByteArrayAsync() public method

public GetByteArrayAsync ( Uri requestUri ) : Task
requestUri System.Uri
return Task
Beispiel #1
2
        async private void loadJsonInfo() 
        {
            try
            {
                //--------- Having content from website--------------
                HttpClient http = new HttpClient();
                var response = await http.GetByteArrayAsync("http://emresevinc.github.io/exrepo.html");
                String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
                source = WebUtility.HtmlDecode(source);
                HtmlDocument resultat = new HtmlDocument();
                resultat.LoadHtml(source);

                List<HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
                    (x => (x.Name == "div" && x.Attributes["class"] != null &&
                    x.Attributes["class"].Value.Contains("container"))).ToList();
                String text = toftitle[0].InnerText;

                selectedMovie = new Movie();
                //----------------------------------------------------
                _jObject = JsonObject.Parse(text);  // Burada json dosyası parse ediliyor.
                jArr = _jObject.GetNamedArray("movies"); // Json verileri içindeki movies array'i
                fillToMovieList();
            }
            catch (Exception)
            {
               new MessageDialog("Bir hata gerceklesti").ShowAsync();
            }
        }
Beispiel #2
1
        protected Downloads.File GetFileStream(Downloads.File file)
        {
            using (var client = new HttpClient()) {

            try {
                var data = client.GetByteArrayAsync(file.DownloadUrl).Result;

                    if (data.Length > 0) {

                    var result = data.ToArray();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(data, 0, data.Length);
                    ms.Position = 0;

                    file.FileStream = ms;
                    file.IsValidFile = true;
                    ms.Flush();

                    } else {
                        throw new Exception("Error: Unable to download file");
                    }
                } catch (AggregateException ex) {
                    //log error
                    ex.ToString();
              }
            }
              return file;
        }
        private async Task Download(string url,StorageFile file,bool cover)
        {
            var http = new HttpClient();
            byte[] response = { };
            string betterUrl = url;
            if(cover)
            {
                var pos = betterUrl.IndexOf(".jpg");
                if (pos != -1)
                    betterUrl = betterUrl.Insert(pos, "l");
            }

            //get bytes
            try
            {
                await Task.Run(async () => response = await http.GetByteArrayAsync(betterUrl));
            }
            catch (Exception)
            {
                await Task.Run(async () => response = await http.GetByteArrayAsync(url));
            }

            var fs = await file.OpenStreamForWriteAsync(); //get stream
            var writer = new DataWriter(fs.AsOutputStream());

            writer.WriteBytes(response); //write
            await writer.StoreAsync();
            await writer.FlushAsync();

            writer.Dispose();
        }
Beispiel #4
0
        public async Task StreamsReadFromSamePositionEachRequest()
        {
            var bytes = new byte[]
            {
                121, 111, 117, 116, 117, 98, 101, 46, 99, 111, 109, 47, 112, 108, 97, 121, 108, 105, 115, 116, 63, 108,
                105, 115, 116, 61, 80, 76, 89, 111, 111, 69, 65, 70, 85, 102, 104, 68, 102, 101, 118, 87, 70, 75, 76,
                97, 55, 103, 104, 51, 66, 111, 103, 66, 85, 65, 101, 98, 89, 79
            };

            int offsetStreamPosition = 39;

            byte[] expectedOffsetBytes = bytes.Skip(offsetStreamPosition).ToArray();

            using (MemoryStream stream = new MemoryStream(bytes))
                using (MemoryStream offsetStream = new MemoryStream(bytes))
                {
                    handler.SetupRequest(HttpMethod.Get, "https://example.com/normal")
                    .ReturnsResponse(stream);

                    // Multiple setups can share the same stream as well
                    handler.SetupRequest(HttpMethod.Get, "https://example.com/normal2")
                    .ReturnsResponse(stream);

                    // This stream is the same but seeked forward; each request should read from this position rather than
                    // seeking back to the beginning
                    offsetStream.Seek(offsetStreamPosition, SeekOrigin.Begin);
                    handler.SetupRequest(HttpMethod.Get, "https://example.com/offset")
                    .ReturnsResponse(offsetStream);

                    var responseBytes1 = await client.GetByteArrayAsync("normal");

                    var responseBytes2 = await client.GetByteArrayAsync("normal");

                    var responseBytes3 = await client.GetByteArrayAsync("normal2");

                    var offsetResponseBytes1 = await client.GetByteArrayAsync("offset");

                    var offsetResponseBytes2 = await client.GetByteArrayAsync("offset");

                    responseBytes1.Should().BeEquivalentTo(bytes);
                    responseBytes2.Should().BeEquivalentTo(bytes,
                                                           "the stream should be returned to its original position after being read");
                    responseBytes3.Should().BeEquivalentTo(bytes,
                                                           "the stream should be reusable not just between requests to one setup but also between setups");

                    offsetResponseBytes1.Should().BeEquivalentTo(expectedOffsetBytes,
                                                                 "the stream should read from its initial (offset) position, not necessarily the beginning");
                    offsetResponseBytes2.Should().BeEquivalentTo(expectedOffsetBytes,
                                                                 "the stream should be returned to its original (offset, not zero) position after being read");
                }
        }
Beispiel #5
0
        /// <summary>
        /// Parser article from URL.
        /// </summary>
        /// <param name="url">URL</param>
        /// <returns></returns>
        public static String Parse(String url)
        {
            String source;
            byte[] response;
            HtmlNode content;
            HttpClient client;
            HtmlDocument document;
            List<String> sentences;

            sentences = new List<String>();
            client = new HttpClient();
            response = client.GetByteArrayAsync(url).Result;
            source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
            source = WebUtility.HtmlDecode(source);
            document = new HtmlDocument();
            document.LoadHtml(source);

            content = document.DocumentNode.Descendants().Where(
                x => x.Attributes["id"] != null && x.Attributes["id"].Value.Equals("mw-content-text")).FirstOrDefault();

            if (content == null)
                return "Unable to parse the source.";

            foreach (HtmlNode childNoteLoop in content.ChildNodes)
            {
                if (childNoteLoop.Name == "div" && childNoteLoop.Id == "toc")
                    break;

                if (childNoteLoop.Name == "p")
                    sentences.Add(childNoteLoop.InnerText.Trim());
            }

            return String.Join(NotenizerConstants.WordDelimeter, sentences);
        }
Beispiel #6
0
		/// <summary>
		/// Concurrently requests the given <paramref name="target"/> <paramref name="requestCount"/> times.
		/// </summary>
		/// <param name="target">Uri to torture.</param>
		/// <param name="requestCount">Number of requests.</param>
		/// <param name="concurrentRequestLimit">Maximum number of concurrent requests.</param>
		public static async Task<TortureResult> TortureAsync(Uri target, Int64 requestCount, Int64 concurrentRequestLimit = 22)
		{
			Int64 bytesRead = 0;
			Stopwatch stopwatch = Stopwatch.StartNew();

			var pendingTasks = new List<Task<Byte[]>>();

			using (HttpClient client = new HttpClient())
			{
				for (Int64 index = 0; index < requestCount; index++)
				{
					Task<Byte[]> fetchTask = client.GetByteArrayAsync(target);
					pendingTasks.Add(fetchTask);

					if (pendingTasks.Count > concurrentRequestLimit)
					{
						foreach (var task in pendingTasks.ConsumeWhere(null))
						{
							Byte[] result = await task.ConfigureAwait(false);
							bytesRead += result.LongLength;
						}
					}
				}

				foreach (var task in pendingTasks.ConsumeWhere(null))
				{
					Byte[] result = await task.ConfigureAwait(false);
					bytesRead += result.LongLength;
				}
			}

			return new TortureResult { Target = target, RequestCount = requestCount, BytesRead = bytesRead, TimeElapsed = stopwatch.Elapsed };
		}
Beispiel #7
0
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button AsyncButton = FindViewById<Button> (Resource.Id.btnAsync);
			Button ParallelButton = FindViewById<Button> (Resource.Id.btnParallel);
			TextView LabelSize = FindViewById<TextView> (Resource.Id.lblSize);
			ImageView ImageDonwloaded = FindViewById<ImageView> (Resource.Id.imageD);
			CheckBox ckbHUD = FindViewById<CheckBox> (Resource.Id.checkBoxHUD);
			var showHUD = false;

			ParallelButton.Click += (sender, e) => {
				showHUD = ckbHUD.Checked;
				Task.Factory.StartNew(()=>{
					if(showHUD)
						AndHUD.Shared.Show(this, "Downloading Image via Parallel", -1, MaskType.Clear);
					var httpClient = new HttpClient();
					byte[] imageBytes  = httpClient.GetByteArrayAsync("http://upload.wikimedia.org/wikipedia/commons/6/66/Big_size_chess_6759_CRI_08_2009_Langosta_Beach.jpg").Result;
					return imageBytes; 
				}).ContinueWith(res=>{
					string documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
					string localPath = System.IO.Path.Combine (documents, "image.png");
					File.WriteAllBytes (localPath, res.Result);
					var localImage = new Java.IO.File (localPath);
					if (localImage.Exists ()) {
						var teamBitmap = BitmapFactory.DecodeFile (localImage.AbsolutePath);
						RunOnUiThread(()=>{
							LabelSize.Text = string.Format("Size: {0} MB", ConvertBytesToMegabytes(res.Result.Length));
							ImageDonwloaded.SetImageBitmap (teamBitmap);
						});
					}
					if(showHUD)
						AndHUD.Shared.Dismiss(this);
				});
			};

			AsyncButton.Click += async (sender, e) => {
				showHUD = ckbHUD.Checked;
				if(showHUD)
					AndHUD.Shared.Show(this, "Downloading Image via Async/Await", -1, MaskType.Clear);
				var httpClient = new HttpClient();
				byte[] imageBytes  = await httpClient.GetByteArrayAsync("http://upload.wikimedia.org/wikipedia/commons/6/66/Big_size_chess_6759_CRI_08_2009_Langosta_Beach.jpg");
				string documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
				string localPath = System.IO.Path.Combine (documents, "image.png");
				File.WriteAllBytes (localPath, imageBytes);
				var localImage = new Java.IO.File (localPath);
				if (localImage.Exists ()) {
					var teamBitmap = BitmapFactory.DecodeFile (localImage.AbsolutePath);
					LabelSize.Text = string.Format("Size: {0} MB", ConvertBytesToMegabytes(imageBytes.Length));
					ImageDonwloaded.SetImageBitmap (teamBitmap);
				}
				if(showHUD)
					AndHUD.Shared.Dismiss(this);
			};
		}
Beispiel #8
0
       async  void BeginReadCCTV()
        {
            
            IsBeginRead = true;
            ISExit = false;
            tblCCTV cctvinfo=null;
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                   () =>
                   {
                       cctvinfo = this.DataContext as tblCCTV;
                   });

             
           // client=new HttpClient();
            while (!ISExit)
            {
                try
                {
                    Uri uri = new Uri("http://192.192.161.3/" + cctvinfo.REF_CCTV_ID.Trim() + ".jpg?" + rnd.Next(), UriKind.Absolute);

                    using (httpClient = new HttpClient())
                    {
                        httpClient.Timeout = TimeSpan.FromSeconds(0.5);
                        var contentBytes = await httpClient.GetByteArrayAsync(uri);

                   
                        var ims =  new InMemoryRandomAccessStream();

                        var dataWriter = new DataWriter(ims);
                        dataWriter.WriteBytes(contentBytes);
                        await dataWriter.StoreAsync();
                       //ims.seak 0
                     ims.Seek(0 );
                       
                       await Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal,()=>
                            {
                        BitmapImage bitmap = new BitmapImage();
                        bitmap.SetSource(ims );
                       

                        this.imgCCTV.Source = bitmap;
                        imginx = (imginx + 1) % 90000;
                        this.RunFrameRate.Text = imginx.ToString();
                            });
                    }
                    
                }
                catch (Exception ex)
                {
                 //   this.textBlock1.Text = ex.Message;
                }
                // BitmapImage img = new BitmapImage();
                // img.SetSource(stream);

              

               
            }
        
        }
 private async Task<List<string>> GetCsvData(Uri csvLink)
 {
     HttpClient request = new HttpClient();
     var csv = Encoding.GetEncoding("latin1").GetString(await request.GetByteArrayAsync(csvLink));            
     var lines = csv.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
     return lines.Skip(1).ToList();
 }
 public async static Task<byte[]> GetByteArrayAsync(string url)
 {
     using (var httpClient = new HttpClient(new ModernHttpClient.NativeMessageHandler()))
     {
         return await httpClient.GetByteArrayAsync(url);
     }
 }
        public Task<byte[]> DownloadFromUri(string uri)
        {
            //HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

            //using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
            //{
            //    using (Stream stream = httpWebReponse.GetResponseStream())
            //    {
            //        var memoryStream = new MemoryStream();
            //        stream.CopyTo(memoryStream);
            //        return memoryStream.ToArray();
            //    }
            //}

            return Task.Run(async () =>
             {
                 using (var client = new HttpClient())
                 {
                      // var stream = client.GetStreamAsync("https://scontent-frt3-1.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/12289642_1183804578301916_8282521644435051935_n.jpg?oh=3f6d4458591beda5ca286d4269ecb865&oe=5724CC7F");
                      var rawresponse = await  client.GetByteArrayAsync(uri);

                     return rawresponse;// TODO:
                  }

             });
        }
 private static async Task DownloadFromOfficial(string fileName, string imageUrl, string relevantPart)
 {
     using (var client = new HttpClient())
     {
         var imgData = await client.GetByteArrayAsync(imageUrl).ConfigureAwait(false);
         CreateDirectories(fileName);
         using (var ms = new MemoryStream(imgData))
         using (var image = Image.FromStream(ms))
         {
             try
             {
                 using (var outputStream = File.Create(fileName, 65536, FileOptions.Asynchronous))
                 {
                     image.Save(outputStream, ImageFormat.Png);
                 }
                 Log.InfoFormat("Downloaded item image {0} to the file system.", relevantPart);
                 return;
             }
             catch (IOException e)
             {
                 Log.Info("File " + relevantPart +
                          " could not be created. Most likely because it was created from another thread.", e);
                 Log.Info("Waiting 1 sec for the other thread to finish.");
             }
             // No await in catch
             await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
         }
     }
 }
		private async Task saveImageToDisk(Articolo art) // async
		{
			HttpClient c = new HttpClient();
			c.BaseAddress =  new Uri(art.UrlImg);
			c.Timeout = new TimeSpan(0,0, 3);

			Byte[] imgByteArray = await c.GetByteArrayAsync ("");

			CancellationToken a = new CancellationToken();

			IFolder rootFolder = FileSystem.Current.LocalStorage;
  
			IFolder folder = await rootFolder.CreateFolderAsync("Immagini",
				CreationCollisionOption.OpenIfExists);

			var listaFilePerVerifica = await folder.GetFilesAsync (new CancellationToken ());

			var listafile = await folder.GetFilesAsync ();
			
			IFile file = await folder.CreateFileAsync(art.ImageFileName ,
				CreationCollisionOption.ReplaceExisting);
  
			// http://www.mallibone.com/post/storing-files-from-the-portable-class-library-(pcl)

			using (var fileHandler = await file.OpenAsync (FileAccess.ReadAndWrite)) {
				fileHandler.WriteAsync(imgByteArray, 0, imgByteArray.Length); // await
			}   
		}
Beispiel #14
0
        private async void DownLoadImage(Uri _ImageUri)
        {
            HttpClient client = new HttpClient();
            byte[] buffer = await client.GetByteArrayAsync(_ImageUri);

            // 注意需要把数据流重新复制一份,否则会出现跨线程错误
            // 网络下载到的图片数据流,属于后台线程的对象,不能在UI上使用
            Stream streamForUI = new MemoryStream();
            streamForUI.Write(buffer, 0, buffer.Length);

            streamForUI.Seek(0, SeekOrigin.Begin);

            //触发UI更新界面
            await Page.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {

                BitmapImage bm = new BitmapImage();
                bm.SetSource(streamForUI.AsRandomAccessStream());
                //把图像放到弱引用里面
                if (null == bitmapImage)
                {
                    bitmapImage = new WeakReference(bm);
                }
                else
                {
                    bitmapImage.Target = bm;
                }
                //触发UI绑定属性的改变
                OnPropertyChanged("ImageSource");

            });
        }
Beispiel #15
0
        public async Task<FileContentResult> GetResizePicture(string file, int width, int height)
        {
            if (file == "")
                return new FileContentResult(new byte[0], "image/png");

            var client = new HttpClient();
            var response = await client.GetByteArrayAsync(file);

            using (var rms = new MemoryStream(response))
            {
                using (var source = new Bitmap(rms))
                {
                    using (var ms = new MemoryStream())
                    {
                        source.Save(ms, ImageFormat.Png);
                    }

                    var scale = _resize.DetermineResizeScale(source.Width, source.Height, width, height);
                    var bytes = _resize.CreateResizedPicture(rms, scale);

                    return new FileContentResult(bytes, "image/png");
                }
            }

            
        }
Beispiel #16
0
        private async Task SumPageSizesAsync()
        {
            // Declare an HttpClient object and increase the buffer size. The
            // default buffer size is 65,536.
            HttpClient client =
            new HttpClient() { MaxResponseContentBufferSize = 1000000 };

            // Make a list of web addresses.
            List<string> urlList = SetUpURLList();

            var total = 0;
            foreach (var url in urlList)
            {
                // GetURLContents returns the contents of url as a byte array.
                //byte[] urlContents = await GetURLContentsAsync(url);

                byte[] urlContents = await client.GetByteArrayAsync(url);   

                DisplayResults(url, urlContents);

                // Update the total.
                total += urlContents.Length;
            }

            // Display the total count for all of the web addresses.
            resultsTextBox.Text +=
                string.Format("\r\n\r\nTotal bytes returned:  {0}\r\n", total);
        }
 public void UploadMessageAsync()
 {
     var uri = this.UploadMessageHelperAsync().Result;
     var client = new HttpClient();
     var downloadedBody = client.GetByteArrayAsync(uri).Result;
     Assert.Equal(Valid.MessageContent, downloadedBody);
 }
Beispiel #18
0
        /// <summary>
        /// Image用 失敗時NULL
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public virtual async Task <byte[]> HttpGetByte(string url, string referer = default(string))
        {
            await Sleep();

            if (ReportUrl)
            {
                ReportManage.Report(this, "GET " + url, true, true);
            }

            byte[] data;
            try
            {
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler, false);
                client.DefaultRequestHeaders.UserAgent.ParseAdd(this.UserAgent);
                client.DefaultRequestHeaders.Referrer = new Uri(referer);
                data = await client.GetByteArrayAsync(url);
            }
            catch (Exception e)
            {
                if (visbleErr)
                {
                    ReportManage.ErrReport(this, "Url:" + url + " " + e.Message);
                }
                data = null;
            }
            if (data != null)
            {
                return(data);
            }
            else
            {
                return(null);
            }
        }
		public async Task<int> DownloadHomepageAsync()
		{
			try
			{
				var httpClient = new HttpClient();

				Task<string> contentsTask = httpClient.GetStringAsync("http://xamarin.com"); 

				string contents = await contentsTask;

				int length = contents.Length;
				ResultTextView.Text += "Downloaded the html and found out the length.\n\n";

				byte[] imageBytes = await httpClient.GetByteArrayAsync("http://xamarin.com/images/about/team.jpg"); 
				await SaveBytesToFileAsync(imageBytes, "team.jpg");
				ResultTextView.Text += "Downloaded the image.\n";
				ResultTextView.Text += "Save the image to a file." + Environment.NewLine;
				DownloadedImageView.Image = UIImage.FromFile(localPath);

				ALAssetsLibrary library = new ALAssetsLibrary();     
				var dict = new NSDictionary();
				var assetUrl = await library.WriteImageToSavedPhotosAlbumAsync 
													(DownloadedImageView.Image.CGImage, dict);
				ResultTextView.Text += "Saved to album assetUrl = " + assetUrl + "\n";

				ResultTextView.Text += "\n\n\n" + contents; // just dump the entire HTML
				return length; 
			}
			catch
			{
				// do something with error
				return -1;
			}
		}
        protected override async void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == "Renderer")
            {
                if (loaded)
                    return;
                
                var uriImageSource = Element.Source as UriImageSource;
                if (uriImageSource == null || uriImageSource.Uri == null)
                    return;

                try
                {
                    loaded = true;
                    using (var client = new HttpClient())
                    {
                        var bytes = await client.GetByteArrayAsync(uriImageSource.Uri);
                        var sourceRef = CGImageSource.FromData(NSData.FromArray(bytes));
                        CreateAnimatedImageView (sourceRef, this.Control);
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Unable to load gif: " + ex.Message);
                }

            }
        }
Beispiel #21
0
		public async Task<int> DownloadHomepageAsync()
		{
			var httpClient = new HttpClient(); // Xamarin supports HttpClient!

			Task<string> contentsTask = httpClient.GetStringAsync("http://xamarin.com"); // async method!

			ResultEditText.Text += "DownloadHomepage method continues after async call. . . . .\n";

			// await! control returns to the caller and the task continues to run on another thread
			string contents = await contentsTask;

			// After contentTask completes, you can calculate the length of the string.
			int exampleInt = contents.Length;

			ResultEditText.Text += "Downloaded the html and found out the length.\n\n\n";




			byte[] imageBytes  = await httpClient.GetByteArrayAsync("http://xamarin.com/images/about/team.jpg"); // async method!

			string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
			string localFilename = "team.jpg";
			string localPath = Path.Combine (documentsPath, localFilename);
			File.WriteAllBytes (localPath, imageBytes); // writes to local storage   

			ResultTextView.Text += "Downloaded the image.\n";

			// ImageView stuff goes here

			ResultEditText.Text += contents; // just dump the entire HTML
			return exampleInt; // Task<TResult> returns an object of type TResult, in this case int
		}
Beispiel #22
0
		public async Task<string> GetSpeakerImagePath (Conference conference, Speaker speaker)
		{

			string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			string localFilename = conference.Slug + "-" + speaker.Slug + ".png";
			string localPath = Path.Combine (documentsPath, localFilename);
			byte[] bytes = null;

			if (!File.Exists (localPath)) {
				using (var httpClient = new HttpClient (new NativeMessageHandler ())) {

					try {
						bytes = await httpClient.GetByteArrayAsync (speaker.ImageUrl);
					} catch (OperationCanceledException opEx) {
						Insights.Report (opEx);
						return null;
					} catch (Exception e) {
						Insights.Report (e);
						return null;
					}

					//Save the image using writeAsync
					FileStream fs = new FileStream (localPath, FileMode.OpenOrCreate);
					await fs.WriteAsync (bytes, 0, bytes.Length);
				}
			} 

			return localPath;

		}
Beispiel #23
0
        public static async Task<byte[]> Download(string url)
        {
            Uri uri = new Uri(url);
            HttpClient httpClient = new HttpClient();

            return await httpClient.GetByteArrayAsync(uri);
        }
 public async static Task DownloadAndExtractAll(string uri, string dest)
 {
     var tmpFile = Path.GetTempPath() + "pomelo_" + Guid.NewGuid().ToString() + ".zip";
     Console.WriteLine("Downloading from " + uri);
     using (var webClient = new HttpClient() { Timeout = new TimeSpan(1, 0, 0), MaxResponseContentBufferSize = 1024 * 1024 * 50 })
     {
         var bytes = await webClient.GetByteArrayAsync(uri);
         File.WriteAllBytes(tmpFile, bytes);
         Console.WriteLine("Downloaded");
     }
     using (var fileStream = new FileStream(tmpFile, FileMode.Open))
     using (var archive = new ZipArchive(fileStream))
     {
         foreach (var x in archive.Entries)
         {
             if (!Directory.Exists(Path.GetDirectoryName(dest + x.FullName)))
                 Directory.CreateDirectory(Path.GetDirectoryName(dest + x.FullName));
             if (x.Length == 0 && string.IsNullOrEmpty(Path.GetExtension(x.FullName)))
                 continue;
             using (var entryStream = x.Open())
             using (var destStream = File.OpenWrite(dest + x.FullName))
             {
                 entryStream.CopyTo(destStream);
             }
         }
     }
     File.Delete(tmpFile);
 }
        public async Task<BitmapImage> BitmapImageAsync(string url)
        {
            Stream stream = null;
            HttpClient WebClient = new HttpClient();
            BitmapImage image = new BitmapImage();

            try
            {
                stream = new MemoryStream(await WebClient.GetByteArrayAsync(url));
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad; // here
                image.StreamSource = stream;
                image.EndInit();
                image.Freeze();
            }
            catch { }

            if (stream != null)
            {
                stream.Close(); stream.Dispose(); stream = null;
            }

            url = null; WebClient.CancelPendingRequests(); WebClient.Dispose(); WebClient = null;
            return image;
        }
 static async Task<Bitmap> GetRandomKitty() {
     var httpClient = new HttpClient(new OkHttpNetworkHandler());
     httpClient.Timeout = TimeSpan.FromSeconds(10);
     var imageBytes = await httpClient.GetByteArrayAsync(kittyJpgUrl);
     Bitmap imageBitmap = await BitmapFactory.DecodeByteArrayAsync(imageBytes, 0, imageBytes.Length);
     return imageBitmap;
 }
        static void Main(string[] args)
        {
            HttpClient http = new HttpClient();
            string webSiteUrl = "http://www.posta.md/ro/tracking?id=RH263108004CN";
            var response =  http.GetByteArrayAsync(webSiteUrl);
            string source = Encoding.GetEncoding("utf-8").GetString(response.Result, 0, response.Result.Length - 1);
            source = WebUtility.HtmlDecode(source);
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(source);

            List<HtmlNode> nodes = htmlDoc.DocumentNode.Descendants().Where(w => (w.Name == "div" && w.Attributes["class"] != null) && w.Attributes["class"].Value.Contains("row clearfix")).ToList();

            OrderTracking orderTracking = new OrderTracking();

            foreach (HtmlNode node in nodes)
            {
                orderTracking.AddItem(node);
            } 

            SmppServer smppServer = new SmppServer();

            smppServer.ConnectToSmppServerSmsCarrier();
            smppServer.SendSms();

            Console.ReadLine();
        }
 /// <summary>
 /// 指定したURLから画像データをByte配列で取得します
 /// </summary>
 /// <param name="url">画像URL</param>
 /// <returns>画像データ</returns>
 public static async Task<byte[]> DownLoadImageBytesAsync(string url)
 {
     using (var web = new HttpClient())
     {
         return await web.GetByteArrayAsync(url).ConfigureAwait(false);
     }
 }
Beispiel #29
0
        private async void parsing(string Url)
        {
            try {
                chargement.Visibility = Visibility.Visible;
                HttpClient http    = new System.Net.Http.HttpClient();
                var        reponse = await http.GetByteArrayAsync(Url);

                str = System.Text.Encoding.GetEncoding("UTF-8").GetString(reponse, 0, reponse.Length - 1);
                var          strdecode = System.Net.WebUtility.HtmlDecode(str);
                HtmlDocument htmldoc   = new HtmlDocument();
                htmldoc.LoadHtml(strdecode);

                var l0  = htmldoc.DocumentNode.Descendants("div").Where(x => (x.Attributes.Contains("id") && String.Equals((String)x.GetAttributeValue("id", "null"), "print_dmname"))).ToList();
                var mal = htmldoc.DocumentNode.Descendants("p").ToList();

                for (int i = 4; i < 9; i++)
                {
                    mar = mar + mal[i].InnerText;
                    //+ mal[i].InnerText;
                }
                Listt.Add(new TextArticle
                {
                    Titre  = article.Title,
                    Text   = mar,
                    Writer = mal[9].InnerText
                });
                Post.DataContext      = Listt;
                chargement.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageDialog mesg = new MessageDialog("Please verify your Internet connection");
                mesg.ShowAsync();
            }
        }
        private async void parsing(string Url)
        {
            try
            {
                chargement.Visibility = Visibility.Visible;
                HttpClient http    = new System.Net.Http.HttpClient();
                var        reponse = await http.GetByteArrayAsync(new Uri(Url));

                str = System.Text.Encoding.GetEncoding("UTF-8").GetString(reponse, 0, reponse.Length - 1).ToString();
                var          strdecode = System.Net.WebUtility.HtmlDecode(str);
                HtmlDocument htmldoc   = new HtmlDocument();
                htmldoc.LoadHtml(strdecode);
                var kk = htmldoc.DocumentNode.Descendants("a").ToList();
                for (int i = 84; i < 297; i++)
                {
                    ArticleList.Add(new Article
                    {
                        Title = kk[i].InnerText,
                        Url   = kk[i].GetAttributeValue("href", null)
                    });
                }
                Titles.DataContext    = ArticleList;
                chargement.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageDialog mesg = new MessageDialog("Please verify your Internet connection");
                mesg.ShowAsync();
            }

            int j = 0;
        }
        public async static Task DownloadAndExtract(
            string uri,
            params Tuple<string, string>[] files)
        {
            var tmpFile = Path.GetTempPath() + "pomelo_" + Guid.NewGuid().ToString() + ".zip";
            Console.WriteLine("Downloading from " + uri);
            using (var webClient = new HttpClient() { Timeout = new TimeSpan(1, 0, 0), MaxResponseContentBufferSize = 1024 * 1024 * 50 })
            {
                var bytes = await webClient.GetByteArrayAsync(uri);
                File.WriteAllBytes(tmpFile, bytes);
                Console.WriteLine("Downloaded");
            }
            using (var fileStream = new FileStream(tmpFile, FileMode.Open))
            using (var archive = new ZipArchive(fileStream))
            {
                foreach (var file in files)
                {
                    var entry = archive.GetEntry(file.Item1);
                    if (entry == null)
                        throw new Exception("Could not find file '" + file.Item1 + "'.");

                    Directory.CreateDirectory(Path.GetDirectoryName(file.Item2));

                    using (var entryStream = entry.Open())
                    using (var dllStream = File.OpenWrite(file.Item2))
                    {
                        entryStream.CopyTo(dllStream);
                    }
                }
            }
            File.Delete(tmpFile);
        }
        /// <summary>
        /// Get extracted template path from the given templatepath(url) in request body
        /// </summary>
        /// <param name="TemplateUrl"></param>
        /// <param name="ExtractedTemplate"></param>
        public string GetTemplateFromPath(string TemplateUrl, string ExtractedTemplate, string GithubToken, string UserID = "", string Password = "")
        {
            string templatePath = string.Empty;

            try
            {
                Uri    uri          = new Uri(TemplateUrl);
                string fileName     = Path.GetFileName(TemplateUrl);
                string extension    = Path.GetExtension(fileName);
                string templateName = ExtractedTemplate.ToLower().Replace(".zip", "").Trim();
                if (!Directory.Exists(HostingEnvironment.MapPath("~") + @"\ExtractedZipFile"))
                {
                    Directory.CreateDirectory(HostingEnvironment.MapPath("~") + @"\ExtractedZipFile");
                }
                var path = HostingEnvironment.MapPath("~") + @"\ExtractedZipFile\" + ExtractedTemplate;

                //Downloading template from source of type github
                if (uri.Host == "raw.githubusercontent.com")
                {
                    var githubToken = GithubToken;
                    //var url = TemplateUrl.Replace("github.com/", "raw.githubusercontent.com/").Replace("/blob/master/", "/master/");

                    using (var client = new System.Net.Http.HttpClient())
                    {
                        var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", githubToken);
                        credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
                        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
                        var contents = client.GetByteArrayAsync(TemplateUrl).Result;
                        System.IO.File.WriteAllBytes(path, contents);
                    }
                }
                //Downloading file from other source type (ftp or https)
                else
                {
                    WebClient webClient = new WebClient();
                    if (UserID != null && Password != null)
                    {
                        webClient.Credentials = new NetworkCredential(UserID, Password);
                    }
                    webClient.DownloadFile(TemplateUrl, path);
                    webClient.Dispose();
                }

                templatePath = ExtractZipFile(path, templateName);
            }
            catch (Exception ex)
            {
                ProjectService.logger.Info(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n");
            }
            finally
            {
                var zippath = HostingEnvironment.MapPath("~") + @"\ExtractedZipFile\" + ExtractedTemplate;
                if (File.Exists(zippath))
                {
                    File.Delete(zippath);
                }
            }
            return(templatePath);
        }
        async private Task<BitmapImage> GetImageFromUrlAsync(string url)
        {
            var client = new HttpClient();

            byte[] imageBytes = await client.GetByteArrayAsync(url); // async method!  

            return ToImage(imageBytes);
        }
Beispiel #34
0
 static string DownloadHtml_Remote(string url)
 {
     HttpClient http = new HttpClient();
     var response = http.GetByteArrayAsync(url);
     Task.WaitAll(response);
     var source = Encoding.GetEncoding("utf-8").GetString(response.Result, 0, response.Result.Length);
     return source;
 }
Beispiel #35
0
    private static byte[] FetchGoogleTile(Uri arg)
    {
        var httpClient = new System.Net.Http.HttpClient();

        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Referer", "http://maps.google.com/");
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", @"Mozilla / 5.0(Windows; U; Windows NT 6.0; en - US; rv: 1.9.1.7) Gecko / 20091221 Firefox / 3.5.7");

        return(httpClient.GetByteArrayAsync(arg).ConfigureAwait(false).GetAwaiter().GetResult());
    }
        public Image GetImageFromUrl(string url)
        {
            Uri uri = new Uri(Uri.EscapeUriString(url));

            byte[]     urlContents = _httpClient.GetByteArrayAsync(uri).Result;
            string     fullName    = Path.Combine(CacheImagePath, Guid.NewGuid().ToString());
            FileStream fs          = new FileStream(fullName, FileMode.OpenOrCreate);

            fs.Write(urlContents, 0, urlContents.Length);
            return(Image.FromStream(fs));
        }
Beispiel #37
0
        public override byte[] HttpGetByte(string url)
        {
            this.Sleep();
            System.Net.Http.HttpClient client = GetHttpClient();
            if (!string.IsNullOrEmpty(this.Referer))
            {
                client.DefaultRequestHeaders.Referrer = new Uri(this.Referer);
            }
            var result = client.GetByteArrayAsync(url).ConfigureAwait(false);

            return(result.GetAwaiter().GetResult());
        }
Beispiel #38
0
        private static async Task <SolvedCaptcha> CrackCaptcha(DeathByCaptcha.Client dbc_client, HttpClient client, string content)
        {
            var recaptcha_id = content.Split(new string[] { "Recaptcha.create(\"" }, StringSplitOptions.None)[1]
                               .Split('\"')[0]
                               .Trim();

            var challenge_response =
                await client.GetStringAsync("https://www.google.com/recaptcha/api/challenge?k=" + recaptcha_id);

            Regex regexChallenge = new Regex("challenge : '(.*)'");
            var   match          = regexChallenge.Match(challenge_response);

            if (!match.Success)
            {
                return(null);
            }

            var recaptcha_challenge = match.Groups[1].Value;

            var image_response =
                await client.GetByteArrayAsync("https://www.google.com/recaptcha/api/image?c=" + recaptcha_challenge);

            if (image_response == null)
            {
                return(null);
            }

            Captcha captcha = dbc_client.Decode(image_response);

            if (captcha.Solved && captcha.Correct)
            {
                SolvedCaptcha solved = new SolvedCaptcha();
                solved.captcha   = captcha;
                solved.challenge = recaptcha_challenge;

                return(solved);
            }
            else
            {
                return(null);
            }
        }
        private async void AddSnapshotsAsync(string DeviceId, string clientIP, IEnumerable <XElement> events)
        {
            if (events.Count() == 0)
            {
                return;
            }

            using (var db = new ICMDBContext())
            {
                foreach (var e in events)
                {
                    if (e.Attribute("time").Value != null)
                    {
                        photograph snapshot = new photograph
                        {
                            C_srcaddr = DeviceId,
                            C_time    = DateTime.Parse(e.Attribute("time").Value)
                        };

                        string imagePath = string.Format("http://{0}/photo?ro={1}&time={2}",
                                                         clientIP,
                                                         DeviceId,
                                                         e.Attribute("time").Value.Replace(" ", "%20"));

                        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                        try
                        {
                            snapshot.C_img = await client.GetByteArrayAsync(new Uri(imagePath));

                            db.Photographs.Add(snapshot);
                        }
                        catch (Exception) { }
                    }
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception) { }
            }
        }
Beispiel #40
0
        private async void parsing(string Url)
        {
            //try {
            chargement.Visibility = Visibility.Visible;
            HttpClient http    = new System.Net.Http.HttpClient();
            var        reponse = await http.GetByteArrayAsync(Url);

            str = System.Text.Encoding.GetEncoding("UTF-8").GetString(reponse, 0, reponse.Length - 1);
            var          strdecode = System.Net.WebUtility.HtmlDecode(str);
            HtmlDocument htmldoc   = new HtmlDocument();

            htmldoc.LoadHtml(strdecode);
            var AutorList = htmldoc.DocumentNode.Descendants("p").Where(x => (x.Attributes.Contains("class") && String.Equals((String)x.GetAttributeValue("class", "null"), "uk-text-right"))).ToList();
            var QuoteList = htmldoc.DocumentNode.Descendants("h3").ToList();

            //int n = b.Count
            for (int i = 0; i < AutorList.Count(); i++)
            {
                Temp1.Add(new Template
                {
                    Author = AutorList[i].InnerText.ToString(),
                    Text   = QuoteList[i].InnerText.ToString()
                });
            }
            Qquote.DataContext    = Temp1;
            chargement.Visibility = Visibility.Collapsed;
            if (Temp1.Count == 0)
            {
                MessageDialog mesg = new MessageDialog("Please verify your Internet connection");
                mesg.ShowAsync();
            }

            //}
            //catch
            //{
            //    MessageDialog mesg = new MessageDialog("Please verify your Internet connection");
            //    mesg.ShowAsync();
            //}
        }
Beispiel #41
0
        public async Task <TResponse> Get <TResponse>(string url, NameValueCollection queryString, NameValueCollection headers = null)
            where TResponse : class, new()
        {
            TResponse response = null;

            try
            {
                System.Net.Http.HttpClient client = _httpClient;
                if (queryString == null)
                {
                    queryString = new NameValueCollection();
                }
                url = CreateUrl(url, queryString);

                if (headers != null)
                {
                    var keys = headers.AllKeys;
                    for (var i = 0; i < keys.Length; i++)
                    {
                        client.DefaultRequestHeaders.Add(keys[i], headers[keys[i]]);
                    }
                }

                //Logger.Debug("请求:{0} {1}", "GET", url);
                byte[] rData = await client.GetByteArrayAsync(url);

                string rJson = Encoding.UTF8.GetString(rData);
                //Logger.Debug("应答:\r\n{0}", rJson);
                response = Newtonsoft.Json.JsonConvert.DeserializeObject <TResponse>(rJson);
            }
            catch (System.Exception e)
            {
                TResponse r = new TResponse();
                //Logger.Error("请求异常:\r\n{0}", e.ToString());
                return(r);
            }
            return(response);
        }
        private async void parsing(string Url)
        {
            try
            {
                chargement.Visibility = Visibility.Visible;
                HttpClient http    = new System.Net.Http.HttpClient();
                var        reponse = await http.GetByteArrayAsync(Url);

                String       str       = System.Text.Encoding.GetEncoding("UTF-8").GetString(reponse, 0, reponse.Length - 1);
                var          strdecode = System.Net.WebUtility.HtmlDecode(str);
                HtmlDocument htmldoc   = new HtmlDocument();
                htmldoc.LoadHtml(strdecode);
                var l0 = htmldoc.DocumentNode.Descendants("img").Where(x => (x.Attributes.Contains("class") && String.Equals((String)x.GetAttributeValue("class", "null"), "uk-border-rounded"))).ToList();
                var l1 = htmldoc.DocumentNode.Descendants("p").ToList();
                var l2 = htmldoc.DocumentNode.Descendants("h2").ToList();
                int i  = 0;
                while (i < 10)
                {
                    LList.Add(new ImageModel
                    {
                        lien = "http://simplereminders.com/domains/simplereminders.com" + l0[i].GetAttributeValue("src", null).ToString(),
                        //Quote = l1[i].InnerText.ToString(),
                        //Post = l2[i-1].InnerText.ToString()
                    });

                    i++;
                }

                ImageGrid.DataContext = LList;
                chargement.Visibility = Visibility.Collapsed;
            }

            catch
            {
                MessageDialog mesg = new MessageDialog("Please verify your Internet connection");
                mesg.ShowAsync();
            }
        }
Beispiel #43
0
 public Task <byte[]> GetByteArrayAsync(string requestUri)
 {
     return(_builtInHttpClient.GetByteArrayAsync(requestUri));
 }
Beispiel #44
0
 public Task <byte[]> GetByteArrayAsync(string requestUri)
 {
     return(_inner.GetByteArrayAsync(requestUri));
 }
Beispiel #45
0
 /// <summary>
 /// Asynchronously downloads a file as a byte array from the specified URL.
 /// </summary>
 public Task <byte[]> DownloadAsync(string url)
 {
     return(_client.GetByteArrayAsync(url));
 }
Beispiel #46
0
        void BindRTB(HttpResponseMessage response, int fileorfolder)
        {
            if (!response.IsSuccessStatusCode)
            {
                Message = "The GitHub Module Name/Path provided is invalid. Error :" + response.ReasonPhrase + ".";
                Console.WriteLine("The GitHub Module Name/Path provided is invalid. Error :" + response.ReasonPhrase + ".");
            }
            else
            {
                var     dataObjects = response.Content.ReadAsStringAsync();
                dynamic srcData     = Json.Decode(dataObjects.Result);

                if (fileorfolder == 0)
                {
                    foreach (var item in srcData)
                    {
                        string itemext = string.Empty;
                        int    index   = item.name.IndexOf(".");
                        if (index == -1)
                        {
                        }
                        else
                        {
                            itemext = item.name.Substring(index);
                        }
                        if (!ExtList.Contains(itemext) || string.IsNullOrEmpty(itemext))
                        {
                            //do nothing
                        }
                        else
                        {
                            foreach (var item1 in ExcludeFiles)
                            {
                                if (item.name == item1)
                                {
                                    flag    = true;
                                    Message = "Successful Message : File:" + item.name + " : has been successfully excluded.";
                                    Console.WriteLine("Successful Message : File:" + item.name + " : has been successfully excluded.");
                                    break;
                                }
                                else
                                {
                                    flag = false;
                                }
                            }

                            if (!flag)
                            {
                                try
                                {
                                    string Filepath = ModulePath + "\\" + item.name;
                                    using (var client = new System.Net.Http.HttpClient())
                                    {
                                        var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", sToken);
                                        credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
                                        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
                                        var contents = client.GetByteArrayAsync(item.download_url).Result;
                                        System.IO.File.WriteAllBytes(Filepath, contents);
                                    }

                                    Message = "Successful Message : File:" + item.name + " : has been successfully downloaded.";
                                    Console.WriteLine("Successful Message : File:" + item.name + " : has been successfully downloaded.");
                                }
                                catch (System.Exception e)
                                {
                                    Message = "Error while getting the File:" + item.name + "(" + e.Message + ")";
                                    Console.WriteLine(Message);
                                    FileErr = true;
                                }
                            }
                            flag = false;
                        }
                    }
                }
                else
                {
                    //Do nothing;
                }
            }
        }
Beispiel #47
0
 public async Task <byte[]> GetByteArrayAsync(string uri) => await _Client.GetByteArrayAsync(uri);
 /// <summary>
 ///     Send a GET request to the specified Uri and return the response body as a byte array in a synchronous operation.
 /// </summary>
 /// <param name="requestUri">The Uri the request is sent to.</param>
 /// <exception cref="ArgumentNullException">
 ///     The <paramref name="requestUri"/> was null.
 /// </exception>
 public static byte[] GetByteArray(this HttpClient client, Uri requestUri)
 {
     return(Task.Run(() => client.GetByteArrayAsync(requestUri)).Result);
 }
Beispiel #49
0
        static async Task Main(string[] args)
        {
            var app = new CommandLineApplication();

            app.HelpOption("-h|--help");
            var optionUrl         = app.Option("-u|--url <URL>", "The server url to request", CommandOptionType.SingleValue).IsRequired();
            var optionConnections = app.Option <int>("-c|--connections <N>", "Total number of HTTP connections to open. Default is 10.", CommandOptionType.SingleValue);
            var optionWarmup      = app.Option <int>("-w|--warmup <N>", "Duration of the warmup in seconds. Default is 5.", CommandOptionType.SingleValue);
            var optionDuration    = app.Option <int>("-d|--duration <N>", "Duration of the test in seconds. Default is 5.", CommandOptionType.SingleValue);
            var optionHeaders     = app.Option("-H|--header <HEADER>", "HTTP header to add to request, e.g. \"User-Agent: edge\"", CommandOptionType.MultipleValue);
            var optionVersion     = app.Option("-v|--version <1.0,1.1,2.0>", "HTTP version, e.g. \"2.0\". Default is 1.1", CommandOptionType.SingleValue);
            var optionCertPath    = app.Option("-t|--cert <filepath>", "The path to a cert pfx file.", CommandOptionType.SingleValue);
            var optionCertPwd     = app.Option("-p|--certpwd <password>", "The password for the cert pfx file.", CommandOptionType.SingleValue);

            app.OnExecuteAsync(async cancellationToken =>
            {
                Console.WriteLine("Http Client");
                Console.WriteLine($"args: {string.Join(" ", args)}");

                ServerUrl = optionUrl.Value();

                WarmupTimeSeconds = optionWarmup.HasValue()
                    ? int.Parse(optionWarmup.Value())
                    : 5;

                ExecutionTimeSeconds = optionDuration.HasValue()
                    ? int.Parse(optionDuration.Value())
                    : 5;

                Connections = optionConnections.HasValue()
                    ? int.Parse(optionConnections.Value())
                    : 10;

                Headers = new List <string>(optionHeaders.Values);

                if (!optionVersion.HasValue())
                {
                    Version = HttpVersion.Version11;
                }
                else
                {
                    switch (optionVersion.Value())
                    {
                    case "1.0": Version = HttpVersion.Version10; break;

                    case "1.1": Version = HttpVersion.Version11; break;

                    case "2.0": Version = HttpVersion.Version20; break;

                    default:
                        Console.WriteLine("Unkown HTTP version: {0}", optionVersion.Value());
                        break;
                    }
                }

                if (optionCertPath.HasValue())
                {
                    CertPath = optionCertPath.Value();
                    Console.WriteLine("CerPath: " + CertPath);
                    CertPassword = optionCertPwd.Value();
                    if (CertPath.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                    {
                        Console.WriteLine($"Downloading cert from: {CertPath}");
                        var httpClientHandler = new HttpClientHandler
                        {
                            ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                        };

                        var httpClient = new System.Net.Http.HttpClient(httpClientHandler);
                        var bytes      = await httpClient.GetByteArrayAsync(CertPath);
                        Certificate    = new X509Certificate2(bytes, CertPassword);
                        Console.WriteLine("Cert Thumb: " + Certificate.Thumbprint);
                    }
                    else
                    {
                        Console.WriteLine($"Using cert from: {CertPath}");
                        Certificate = new X509Certificate2(CertPath, CertPassword);
                    }
                }

                await RunAsync();
            });

            await app.ExecuteAsync(args);
        }
Beispiel #50
0
        ///// <summary>
        ///// Basic認証用
        ///// </summary>
        //public RawlerLib.BasicAuthorization BasicAuthorization { get; set; }

        /// <summary>
        /// HttpGet
        /// </summary>
        /// <param name="url"></param>
        /// <param name="enc"></param>
        /// <returns></returns>
        public virtual async Task <string> HttpGet(string url, Encoding enc = null, string referer = default(string))
        {
            await Sleep();

            if (UseCache)
            {
                var h = GetCashe(url);
                if (h != null)
                {
                    return(h);
                }
            }

            ErrMessage = string.Empty;
            string result = string.Empty;
            bool   retry  = false;

            if (ReportUrl)
            {
                ReportManage.Report(this, "GET " + url, true, true);
            }

            try
            {
                System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler, false);
                client.DefaultRequestHeaders.UserAgent.ParseAdd(this.UserAgent);
                client.DefaultRequestHeaders.Referrer = new Uri(referer);
                var data = await client.GetByteArrayAsync(url);

                if (enc != null)
                {
                    result = enc.GetString(data, 0, data.Length);
                }
                else
                {
                    result = GetAutoEncoding(data, out encoder);
                }
            }
            catch (Exception e)
            {
                if (visbleErr)
                {
                    ReportManage.ErrReport(this, "Url:" + url + " " + e.Message);
                }
                ErrMessage = e.Message;
                retry      = false;
            }
            if (retry)
            {
                count++;
                if (count <= tryCount)
                {
                    ReportManage.ErrReport(this, "HttpGet:" + url + "にリトライ待機中");
                    await Task.Delay(new TimeSpan(0, 0, 10 * count * count));

                    result = await this.HttpGet(url, enc);
                }
                else
                {
                    ReportManage.ErrReport(this, "HttpGet:" + url + "に失敗しました");
                    result = string.Empty;
                }
            }
            if (UseCache)
            {
                casheDic.GetValueOrAdd(url, new WeakReference <string>(result));
            }

            return(result);
        }
Beispiel #51
-1
        private async void GetDividents()
        {
            HttpClient client = new HttpClient();
            var response = await client.GetByteArrayAsync(this.DIVIDENTSURL);
            string source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
            source = WebUtility.HtmlDecode(source);
            HtmlAgilityPack.HtmlDocument result = new HtmlAgilityPack.HtmlDocument();
            result.LoadHtml(source);
            List<HtmlNode> required = result.DocumentNode.Descendants().Where(x => (x.Name == "table" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("exdiv"))).ToList();
            var tr = required[0].Descendants("tr").Where(x => (x.Name == "tr" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("yield"))).ToList();
            if (tr.Any())
            {
                foreach (var item in tr)
                {
                    var test = item.Descendants("td").ToList();
                    string name = test[0].InnerText.Trim();
                    string exDiv = test[3].InnerText.Trim();
                    string payDate = test[4].InnerText.Trim();
                    string payout = test[5].InnerText.Trim();
                    string price = test[7].InnerText.Trim();
                    string yield = test[8].InnerText.Trim();

                    int rowId = this.dataGridDividents.Rows.Add();
                    this.dataGridDividents.Rows[rowId].Cells[0].Value = name;
                    this.dataGridDividents.Rows[rowId].Cells[1].Value = exDiv;
                    this.dataGridDividents.Rows[rowId].Cells[2].Value = payDate;
                    this.dataGridDividents.Rows[rowId].Cells[3].Value = payout;
                    this.dataGridDividents.Rows[rowId].Cells[4].Value = price;
                    this.dataGridDividents.Rows[rowId].Cells[5].Value = yield;
                }
            }
        }