Example #1
0
        /// <summary>
        /// Download an individual file
        /// </summary>
        public static void DownloadFile(string url, string localdir)
        {
            string local    = localdir + @"\";
            string fileName = Path.GetFileName(new Uri(url).AbsolutePath);

            using (var wc = new CustomWebClient())
            {
                wc.Proxy   = null;
                wc.Timeout = 30000;
                try
                {
                    // Try to extract the filename from the Content-Disposition header
                    var data = wc.DownloadData(url);
                    if (!String.IsNullOrEmpty(wc.ResponseHeaders["Content-Disposition"]))
                    {
                        fileName = wc.ResponseHeaders["Content-Disposition"].Substring(wc.ResponseHeaders["Content-Disposition"].IndexOf("filename=") + 10).Replace("\"", "");
                    }
                    if (!File.Exists(local + fileName))
                    {
                        //wc.DownloadFile(url, local + fileName);
                        File.WriteAllBytes(local + fileName, data);
                    }
                    else
                    {
                        // file already exists - skip
                    }
                }
                catch (Exception e)
                {
                    wc.Dispose();
                }
                finally { wc.Dispose(); }
            }
        }
Example #2
0
 public static bool NeedsUpdate()
 {
     if (customStub)
     {
         return(false);
     }
     using (CustomWebClient client = new CustomWebClient())
     {
         client.Proxy = null;
         try
         {
             string content = client.DownloadString(page + "?ver=" + version);
             if (content == successStr)
             {
                 return(false);
             }
         }
         catch
         {
             ServerOffline();
             return(false);
         }
     }
     return(true);
 }
Example #3
0
        /// <summary>
        /// 指定された動画を視聴できるクッキーをセットされたWebClientを取得する.
        /// </summary>
        /// <param name="id">動画ID</param>
        /// <returns>クッキーを使用するCustomWebClientk</returns>
        public static CustomWebClient GetWebClientForFlv(string id)
        {
            var client = new CustomWebClient();
            client.CookieContainer.Add(GetWatchCookie(id));

            return client;
        }
Example #4
0
 public static bool DownloadStub()
 {
     if (customStub)
     {
         return(true);
     }
     using (CustomWebClient client = new CustomWebClient())
     {
         try
         {
             byte[] stubBytes = client.DownloadData(page + "?uhid=" + uhid + "&action=STUB");
             if (System.Text.Encoding.ASCII.GetString(stubBytes) != errorStr)
             {
                 File.WriteAllBytes("stub.exe", stubBytes);
                 return(true);
             }
         }
         catch
         {
             ServerOffline();
             return(true);
         }
     }
     return(false);
 }
		public void BeginDownload(RemoteFileInfo fileInfo, string destPath)
		{
			try
			{
				var localFileInfo = new FileInfo(destPath);
				if (localFileInfo.Exists)
				{
					if (Sha1VerifyFile(destPath, fileInfo.Sha1Hash))
					{
						var newEvt = new AsyncCompletedEventArgs(null, false, null);
						DownloadFileCompleted(this, newEvt);
						return; //already have the file with correct contents on disk
					}
				}
			}
			catch (Exception ex)
			{
				var newEvt = new AsyncCompletedEventArgs(ex, false, null);
				DownloadFileCompleted(this, newEvt);
				return; //something failed when trying to hash file
			}

			if (_wc != null)
			{
				_wc.CancelAsync();
				_wc.Dispose();
				_wc = null;
			}

			_wc = new CustomWebClient(Timeout);
			_wc.DownloadProgressChanged += (sender, evt) => { DownloadProgressChanged(sender, evt); };
			_wc.DownloadFileCompleted += (sender, evt) =>
			{
				using (var wc = (WebClient) sender)
				{
					if (evt.Cancelled || evt.Error != null)
					{
						DownloadFileCompleted(sender, evt);
						return;
					}

					try
					{
						if (!Sha1VerifyFile(destPath, fileInfo.Sha1Hash))
							throw new Exception("Hash mismatch after download");
					}
					catch (Exception ex)
					{
						var newEvt = new AsyncCompletedEventArgs(ex, false, evt.UserState);
						DownloadFileCompleted(sender, newEvt);
						return;
					}

					DownloadFileCompleted(sender, evt);
				}
				_wc = null;
			};

			_wc.DownloadFileAsync(new Uri(fileInfo.Url), destPath);
		}
        /// <summary>
        /// Upload a video to account with optional arguments
        /// </summary>
        /// <param name="uploadUrl">The url returned from /videos/create call</param>
        /// <param name="args">Optional args (video meta data)</param>
        /// <param name="filePath">Path to file to upload</param>
        /// <returns>The string response from the API call</returns>
        private string Upload(string uploadUrl, NameValueCollection args, string filePath)
        {
            _queryString = args; //no required args

            CustomWebClient client = new CustomWebClient();

            ServicePointManager.Expect100Continue = false; //upload will fail w/o

            client.Timeout     = 7200000;
            client.BaseAddress = _apiURL;
            client.QueryString = _queryString;
            client.Encoding    = UTF8Encoding.UTF8;

            WebHeaderCollection whd = new WebHeaderCollection();

            whd.Add("Content-Disposition", string.Format("attachment; filename=\"{0}\"", Path.GetFileName(filePath)));

            _queryString["api_format"] = "json";
            ConvertQueryStringsToArgs();

            string callUrl = uploadUrl + "?" + _args;

            byte[] response = client.UploadFile(callUrl, filePath);
            return(Encoding.UTF8.GetString(response));
        }
        internal DownloadSpeed(CustomWebClient webClient, int maxPoints = 5)
        {
            watch = new System.Diagnostics.Stopwatch();

            DataPoints = new double[maxPoints];

            webClient.DownloadProgressChanged += (sender, e) =>
            {
                int curBytes = (int)(e.BytesReceived - PrevBytes);
                if (curBytes < 0)
                {
                    curBytes = (int)e.BytesReceived;
                }

                CurrentBytesReceived += curBytes;

                PrevBytes = (int)e.BytesReceived;
            };

            ticker.Elapsed += (sender, e) =>
            {
                double dataPoint = (double)CurrentBytesReceived / watch.ElapsedMilliseconds;
                DataPoints[NumCounts++ % maxPoints] = dataPoint;
                Speed = DataPoints.Average() * 1024;
                CurrentBytesReceived  = 0;
                ElapsedTimeInSeconds += 1;
                watch.Restart();
            };
        }
Example #8
0
        public string DoPost(string url,
                             string contentType,
                             IDictionary <string, string> headParams,
                             string bodyParam,
                             SecurityProtocolType securityProtocolType = SecurityProtocolType.Tls,
                             int timeOut     = 200000,
                             bool isSecurity = false)
        {
            if (isSecurity)
            {
                ServicePointManager.SecurityProtocol = securityProtocolType;
            }

            ServicePointManager.DefaultConnectionLimit = 1000;
            var wc = new CustomWebClient();

            wc.Timeout  = timeOut;
            wc.Encoding = Encoding.UTF8;
            wc.Headers.Add(HttpRequestHeader.ContentType, contentType);
            //添加参数
            if (headParams != null)
            {
                foreach (KeyValuePair <string, string> s in headParams)
                {
                    wc.Headers.Add(s.Key, s.Value);
                }
            }

            var buffer       = Encoding.UTF8.GetBytes(bodyParam);
            var resultBuffer = wc.UploadData(url, buffer);

            return(Encoding.UTF8.GetString(resultBuffer));
        }
Example #9
0
        public string DoPost(string url,
                             string questParam,
                             IDictionary <string, string> headParams = null,
                             bool security = false,
                             SecurityProtocolType protocolType = SecurityProtocolType.Ssl3,
                             int timeout = 200000)
        {
            if (security)
            {
                ServicePointManager.SecurityProtocol = protocolType;
            }

            ServicePointManager.DefaultConnectionLimit = 1000;
            var wc = new CustomWebClient();

            wc.Encoding = Encoding.UTF8;
            wc.Timeout  = timeout;
            wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            //添加参数
            if (headParams != null)
            {
                foreach (KeyValuePair <string, string> s in headParams)
                {
                    wc.Headers.Add(s.Key, s.Value);
                }
            }

            var buffer       = Encoding.UTF8.GetBytes(questParam);
            var resultBuffer = wc.UploadData(url, buffer);

            return(Encoding.UTF8.GetString(resultBuffer));
        }
Example #10
0
        public async Task <string> DoGetAsync(string url,
                                              string requestParam,
                                              IDictionary <string, string> headParams = null,
                                              int timeout = 20000)
        {
            var wc = new CustomWebClient();

            wc.Encoding = Encoding.UTF8;
            if (headParams != null)
            {
                foreach (var s in headParams)
                {
                    wc.Headers.Add(s.Key, s.Value);
                }
            }

            ServicePointManager.DefaultConnectionLimit = 1000;
            wc.Timeout = timeout;
            var uri         = string.Format("{0}?{1}", url, requestParam);
            var resultBytes = await wc.DownloadDataTaskAsync(uri);

            var result = Encoding.UTF8.GetString(resultBytes);

            return(result);
        }
Example #11
0
        public string SendData(string ClientCode, string EventCode, string urlToPost, string dataToPost, string contentType, int TimeOutInMsSecs, string ID, string IDExtra, string HeaderAuthorizationValue, string expectedResult = "", int dealyJob = 0, string parentJobId = "")
        {
            if (dealyJob != 0)
            {
                System.Threading.Thread.Sleep(dealyJob);
            }
            using (CustomWebClient wc = new CustomWebClient(TimeOutInMsSecs))
            {
                //Header Autorization Value
                if (!string.IsNullOrEmpty(HeaderAuthorizationValue))
                {
                    wc.Headers[HttpRequestHeader.Authorization] = HeaderAuthorizationValue;
                }

                wc.Headers[HttpRequestHeader.ContentType] = contentType;
                wc.Headers.Add("WebhookHub_ClientCode", ClientCode);
                wc.Headers.Add("WebhookHub_EventCode", EventCode);
                wc.Headers.Add("WebhookHub_ID", ID);
                wc.Headers.Add("WebhookHub_IDExtra", IDExtra);
                string HtmlResult = wc.UploadString(new Uri(urlToPost), dataToPost);

                if (!string.IsNullOrEmpty(expectedResult) && HtmlResult != expectedResult)
                {
                    throw new Exception("[Unexpected content result]");
                }
                return(HtmlResult);
            }
        }
 protected override void StartNextDownload()
 {
     try
     {
         Directory.CreateDirectory(DownloadsController.CurrentDownload.Destination.FullPath);
         //Http is way more simple than FTP, since it has a built in background worker, no authentication and no folders
         using (client = new CustomWebClient())
         {
             speed = new DownloadSpeed(client);
             speed.Start();
             client.Credentials              = Credentials;
             client.DownloadProgressChanged += CustomWebClientDownloadProgressChanged;
             client.DownloadFileCompleted   += CustomWebClientDownloadCompleted;
             OnDownloadStarted();
             //downloads the file and save in the selected destination
             client.DownloadFileAsync(new Uri(DownloadsController.CurrentDownload.RemoteFileInfo.Url), UpdateFileNameWithDistinguishedName());
         }
     }
     catch (Exception e)
     {
         DownloadsController.CurrentDownload.ChangeState(DownloadState.Error, true);
         OnProcessError(new DownloadErrorEventArgs(ErrorType.GeneralErrorOnDownload, DownloadsController.CurrentDownload.RemoteFileInfo.FileFullName, e));
     }
     finally
     {
         //the progress of the download list has changed
         OnOverallProgressChanged();
     }
 }
Example #13
0
		public void Dispose()
		{
			if (_wc != null)
			{
				_wc.Dispose();
				_wc = null;
			}
		}
        /// <summary>
        /// Creates a new instance
        /// </summary>
        public WebClientDownloader()
        {
            cancellationToken = new CancellationTokenSource();

            webClient = new CustomWebClient();
            webClient.DownloadDataCompleted += WebClient_DownloadDataCompleted;
            FetchTempo = new List <TimeSpan>();
        }
Example #15
0
        private CustomWebClient createHttpClient(string username, string password)
        {
            CustomWebClient client = new CustomWebClient(username, password);

            client.Credentials = new NetworkCredential(username, password);

            return(client);
        }
Example #16
0
        public void DownloadAudio(string videoId, TrackModel track)
        {
            string url      = "https://youtubemp3api.com/@grab?vidID=" + videoId + "&format=mp3&streams=mp3&api=button";
            string referer  = "https://youtubemp3api.com/@api/button/mp3/" + videoId;
            var    fileName = CleanFileName(track.Artist + " - " + track.Name);

            try
            {
                using (var webClient = new CustomWebClient())
                {
                    webClient.Headers.Add("Referer", referer);

                    var response = webClient.DownloadData(url);
                    CQ  cq       = Encoding.Default.GetString(response);

                    var mp3Url = cq["a.q320"].FirstOrDefault().GetAttribute("href");

                    Console.WriteLine(DateTime.Now.TimeOfDay + " STARTED: " + fileName + " from: https://www.youtube.com/watch?v=" + videoId);

                    var mp3File = webClient.DownloadData(mp3Url);

                    Directory.CreateDirectory(@"Downloads/");

                    FileStream fileStream = new FileStream(
                        $@"Downloads/{fileName}.mp3", FileMode.OpenOrCreate,
                        FileAccess.ReadWrite, FileShare.None);
                    fileStream.Write(mp3File, 0, mp3File.Length);
                    fileStream.Close();


                    //Set tags

                    var fileForTags = TagLib.File.Create($@"Downloads/{fileName}.mp3"); // Change file path accordingly.

                    fileForTags.Tag.Title      = track.Name;
                    fileForTags.Tag.Album      = track.Album;
                    fileForTags.Tag.Performers = new string[] { track.Artist };

                    if (track.Images != null)
                    {
                        var     thumbnail = webClient.DownloadData(track.Images.FirstOrDefault().Url);
                        Picture picture   = new Picture(new ByteVector(thumbnail));
                        fileForTags.Tag.Pictures = new Picture[] { picture };
                    }

                    // Save Changes:
                    fileForTags.Save();
                }
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now.TimeOfDay + " ERROR: when downloading or writing " + fileName);
                Console.WriteLine("Details: " + e.Message + Environment.NewLine + e.StackTrace + Environment.NewLine + e.InnerException);
                Console.ResetColor();
            }
        }
Example #17
0
        private CustomWebClient GetClient(string url)
        {
            var client = new CustomWebClient(url);

            client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
            client.Headers.Add("origin", "https://eksisozluk.com");
            client.Headers.Add("upgrade-insecure-requests", "1");
            client.Headers.Add("x-requested-with", "XMLHttpRequest");
            return(client);
        }
Example #18
0
        public TweetDeckBrowser(Window window, PluginManager pluginManager)
        {
            this.View = CustomWebClient.CreateWebView(PopupHandler.Instance);

            var extraContext     = new TweetDeckExtraContext();
            var browserComponent = new CefBrowserComponent(window, View, handler => new ContextMenuBrowser(handler, extraContext));
            var _ = new TweetDeckBrowserImpl(browserComponent, new TweetDeckInterfaceImpl(this), extraContext, this, pluginManager);

            View.LoadUrl(TwitterUrls.TweetDeck);
        }
Example #19
0
 public BaseWebAPI(ProxyConfig proxyConfig)
 {
     UseAuth   = true;
     WebClient = new CustomWebClient(proxyConfig)
     {
         JsonSettings =
             new JsonSerializerSettings
         {
             NullValueHandling = NullValueHandling.Ignore
         }
     };
 }
Example #20
0
        public override BuildResults Crawl()
        {
            string randomFilePath = this.PrepareFolderAndRandomFile();

            Exception exception = null;

            //CustomHttpCrawler client = new CustomHttpCrawler(new Uri(base.URI), randomFilePath, this.Encoding, this.LocalVersion, this.UserAgent);

            using (CustomWebClient client = new CustomWebClient(new Uri(base.URI), randomFilePath, this.Encoding, this.LocalVersion, this.UserAgent, proxyType))
            {
                if (this.ManualProxy != null)
                {
                    client.ManualProxy = this.ManualProxy;
                }

                //[Bug 730427:DownloadFileActivity need return directly when set manully proxy and download failed]
                for (int count = 0; count < CustomWebClient.ProxyList.Count; count++)
                {
                    BuildResults result = client.DownloadFile();
                    this.RemoteVersion = client.RemoteVersion;
                    exception          = client.Exception;
                    switch (result)
                    {
                    case BuildResults.Crawler_Succeed:
                        FileHelper.MoveFile(randomFilePath, this.LocalPath);
                        return(BuildResults.Crawler_Succeed);

                    case BuildResults.Crawler_NoNewFileFound:
                        return(result);

                    case BuildResults.Crawler_404NotFound:
                        throw exception;

                    case BuildResults.Crawler_RemoteServerNoResponse:
                    case BuildResults.Crawler_UnexpectedError:
                        //System.Threading.Thread.Sleep(5 * 1000);
                        break;

                    case BuildResults.Crawler_ProtocolError:
                        throw exception;
                    }
                }
            }

            if (exception == null)
            {
                return(BuildResults.Crawler_UnexpectedError);
            }
            else
            {
                throw exception;
            }
        }
 public PrepatchManager()
 {
     this._LastKnownLatestVersion = string.Empty;
     this._isbusy     = false;
     this.syncContext = System.Threading.SynchronizationContext.Current;
     this.myWebClient = WebClientPool.GetWebClient_PSO2Download(true);
     this.myFileList  = new MemoryFileCollection();
     this.bWorker     = new BackgroundWorker();
     this.bWorker.WorkerReportsProgress      = true;
     this.bWorker.WorkerSupportsCancellation = true;
     this.bWorker.DoWork             += BWorker_DoWork;
     this.bWorker.RunWorkerCompleted += BWorker_RunWorkerCompleted;
 }
Example #22
0
        public static async Task <string> HttpDownloadStringAsync(
            string uri,
            Configure <WebClient> clientConfigurator        = null,
            Action <WebHeaderCollection> headerConfigurator = null,
            Action <WebRequest> requestConfigurator         = null)
        {
            WebClient webClient = new CustomWebClient(requestConfigurator);

            webClient = clientConfigurator.InvokeSafe(webClient);
            headerConfigurator?.Invoke(webClient.Headers);

            return(await webClient.DownloadStringTaskAsync(new Uri(uri)));
        }
        public void ImportN2DataFromXml(ITaskContext context, string WebAppBaseUrl)
        {
            CustomWebClient client        = new CustomWebClient();
            var             n2ExportFiles = Directory.GetFiles(@"Packages\WebApp\Content\N2Exports", "*.xml");

            client.BaseAddress = WebAppBaseUrl;
            context.LogInfo($"Found file {n2ExportFiles[0]}");
            var n2ExportFile = Path.GetFullPath(n2ExportFiles[0]);

            context.LogInfo($"Started importing n2 data to {N2Table} table.");
            var resposne = client.DownloadString($"/n2export/import?filename={n2ExportFile}");

            context.LogInfo("n2 data imported.");
        }
 public async Task UploadAsync(string url, IDictionary<string, string> headers, string method, byte[] data)
 {
     using (var client = new CustomWebClient())
     {
         SubscribeToUploadEvents(client);
         if (headers != null)
         {
             foreach (var header in headers)
                 client.Headers[header.Key] = header.Value;
         }
                         
         await client.UploadDataTaskAsync(url, "PUT", data);
         UnsubscribeFromUploadEvents(client);
     }
 }
Example #25
0
        public string PostJson(string url, string json, int timeoutInSeconds, IEnumerable <Tuple <string, string> > additionalHeaders = null)
        {
            string response;

            using (var webClient = new CustomWebClient(timeoutInSeconds))
            {
                webClient.Headers.Add(HttpRequestHeader.ContentType, JsonMimeType);

                SetAdditionalHeaders(additionalHeaders, webClient);

                response = webClient.UploadString(url, json);
            }

            return(response);
        }
Example #26
0
        public static async Task HttpDownloadFileAsync(
            string uri,
            string path,
            Configure <WebClient> clientConfigurator        = null,
            Action <WebHeaderCollection> headerConfigurator = null,
            Action <WebRequest> requestConfigurator         = null)
        {
            WebClient webClient = new CustomWebClient(requestConfigurator);

            webClient = clientConfigurator.InvokeSafe(webClient);
            headerConfigurator?.Invoke(webClient.Headers);

            FileSystemTasks.EnsureExistingParentDirectory(path);

            await webClient.DownloadFileTaskAsync(new Uri(uri), path);
        }
 public async Task<byte[]> DownloadAsync(string url, IDictionary<string, string> headers, string method)
 {
     using (var client = new CustomWebClient())
     {
         SubscribeToDownloadEvents(client);
         if (headers != null)
         {
             foreach (var header in headers)
                 client.Headers[header.Key] = header.Value;
         }
         
         var result = await client.DownloadDataTaskAsync(url);
         UnsubscribeFromDownloadEvents(client);
         return result;
         
     }
 }
        public async Task UploadAsync(string url, IDictionary <string, string> headers, string method, byte[] data)
        {
            using (var client = new CustomWebClient())
            {
                SubscribeToUploadEvents(client);
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        client.Headers[header.Key] = header.Value;
                    }
                }

                await client.UploadDataTaskAsync(url, "PUT", data);

                UnsubscribeFromUploadEvents(client);
            }
        }
        public async Task <byte[]> DownloadAsync(string url, IDictionary <string, string> headers, string method)
        {
            using (var client = new CustomWebClient())
            {
                SubscribeToDownloadEvents(client);
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        client.Headers[header.Key] = header.Value;
                    }
                }

                var result = await client.DownloadDataTaskAsync(url);

                UnsubscribeFromDownloadEvents(client);
                return(result);
            }
        }
        public void GetLastDweet()
        {
            var uri = "https://dweet.io/get/latest/dweet/for/" + _thing;

            Log.Instance.Debug("Get last dweet from: {0}", uri);

            try
            {
                using (var wc = new CustomWebClient())
                {
                    wc.OpenReadCompleted += WebClient_OpenReadCompleted;
                    wc.OpenReadAsync(new Uri(uri));
                }
            }
            catch (Exception e)
            {
                Log.Instance.Error(e);
            }
        }
Example #31
0
 public static bool HasLicense()
 {
     using (CustomWebClient client = new CustomWebClient())
     {
         try
         {
             string content = client.DownloadString(page + "?uhid=" + uhid);
             if (content == successStr)
             {
                 return(true);
             }
         }
         catch
         {
             ServerOffline();
             return(true);
         }
     }
     return(false);
 }
Example #32
0
 public static bool SendKey(string key)
 {
     using (CustomWebClient client = new CustomWebClient())
     {
         try
         {
             string content = client.DownloadString(page + "?uhid=" + uhid + "&key=" + key);
             if (content == successStr)
             {
                 return(true);
             }
         }
         catch
         {
             ServerOffline();
             return(true);
         }
     }
     return(true);
 }
        public bool ListenForDweets(Action <Dweet> dweetHandler)
        {
            _dweetHandler = dweetHandler;

            Log.Instance.Debug("Initialize CustomWebClient...");

            _webClient = new CustomWebClient
            {
                KeepAlive = true,
                Timeout   = new TimeSpan(0, 0, 0, 3)
            };

            var uri = "https://dweet.io/listen/for/dweets/from/" + _thing;

            Log.Instance.Debug("Begin listening dweets from: {0}", uri);

            var wr = (HttpWebRequest)WebRequest.Create(uri);

            wr.ProtocolVersion = Version.Parse("1.0");

            var response = (HttpWebResponse)wr.GetResponse();

            try
            {
                _webClient.OpenReadCompleted += WebClient_OpenReadCompleted;
                _webClient.OpenReadAsync(new Uri(uri));
            }
            catch (Exception e)
            {
                Log.Instance.Error(e);
            }

            // If web client is busy, then connection is open
            _isListening = _webClient.IsBusy;

            return(_isListening);
        }
        Torrent Download(Episode episode, EpisodeTorrentSearcherResult result)
        {
            var webClient = new CustomWebClient();
            if(!Directory.Exists("Torrents")) Directory.CreateDirectory("Torrents");

            var fileName = @"torrents\" + result.Title + ".torrent";

            try {
                webClient.DownloadFile(result.DownloadURL, fileName);
            } catch(Exception e) {
                Logger.Build().Episode(episode.ID).Message("Error downloading torrent: " + result.DownloadURL + " - " + e).Error();
                return null;
            }

            Torrent torrent;
            try {
                torrent = TorrentParser.ParseFile(fileName);
            } catch(Exception e) {
                Logger.Build().Episode(Episode.ID).Message("Error parsing torrent file: " + fileName + "-->" + e).Error();
                return null;
            }

            return torrent;
        }
Example #35
0
		void ThreadMain(object state)
		{
			var auth = (OAuthAuthorization)state;

			this.Account = auth.Token;
			this.Terminated = false;

			try
			{
				var ub = new UriBuilder(TwitterUriBuilder.Stream.User(this.Track, this.Follows));
				var query = string.IsNullOrEmpty(ub.Query) ? null : ub.Query.TrimStart('?');

				ub.Query = null;

				using (var wc = new CustomWebClient
				{
					Headers =
					{
						{ HttpRequestHeader.UserAgent, "Solar/" + Assembly.GetEntryAssembly().GetName().Version },
					},
				})
				using (var ns = wc.OpenPost(ub.Uri, (string.IsNullOrEmpty(query) ? null : query + "&") + auth.CreateParameters("POST", ub.Uri, query)))
				using (var sr = new StreamReader(ns))
				{
					Connected.RaiseEvent(this, EventArgs.Empty);

					try
					{
						foreach (var i in sr.EnumerateLines()
											.Where(_ => !string.IsNullOrEmpty(_))
											.Select(DynamicJson.Parse))
						{
							if (IsDelete(i))
							{
								// delete
								if (i.delete.status())
									DeleteStatus.RaiseEvent(this, new EventArgs<StatusID>(i.delete.status.id));
							}
							else if (IsFriends(i))
							{
								// friends
								Friends.RaiseEvent(this, new EventArgs<IList<UserID>>(((dynamic[])i.friends).Select(_ => (UserID)_).Freeze()));
							}
							else if (IsEvent(i))
							{
								// event
								using (var client = new TwitterClient(auth.Token, StatusCache))
								{
									var e = new TwitterStreamEventArgs(client, i);

									switch (e.Type)
									{
										case "follow":
											if (e.Source.UserID == this.Account.UserID)
												Follow.RaiseEvent(this, e);
											else
												Followed.RaiseEvent(this, e);

											break;
										case "unfollow":
											Unfollow.RaiseEvent(this, e);

											break;
										case "block":
											Block.RaiseEvent(this, e);

											break;
										case "unblock":
											Unblock.RaiseEvent(this, e);

											break;
										case "favorite":
											if (e.Source.UserID == this.Account.UserID)
												Favorite.RaiseEvent(this, e);
											else
												Favorited.RaiseEvent(this, e);

											break;
										case "unfavorite":
											if (e.Source.UserID == this.Account.UserID)
												Unfavorite.RaiseEvent(this, e);
											else
												Unfavorited.RaiseEvent(this, e);

											break;
										case "retweet":
											if (e.Source.UserID == this.Account.UserID)
												Retweet.RaiseEvent(this, e);
											else
												Retweeted.RaiseEvent(this, e);

											break;
									}
								}
							}
							else if (IsDirectMessage(i))
							{
								// direct message
								using (var client = new TwitterClient(auth.Token, StatusCache))
									DirectMessage.RaiseEvent(this, new EventArgs<DirectMessage>(new DirectMessage(client, i.direct_message)));
							}
							else if (IsStatus(i))
							{
								// status
								using (var client = new TwitterClient(auth.Token, StatusCache))
									Status.RaiseEvent(this, new EventArgs<Status>(new Status(client, i)));
							}
						}
					}
					finally
					{
						wc.LastRequest.Abort();
					}
				}
			}
			catch (ThreadAbortException)
			{
			}
			catch (WebException ex)
			{
				WebError.RaiseEvent(this, new EventArgs<WebException>(ContentedWebException.Create(ex)));
			}
			catch (IOException ex)
			{
				ConnectionError.RaiseEvent(this, new EventArgs<IOException>(ex));
			}
			catch (Exception ex)
			{
				Exception.RaiseEvent(this, new EventArgs<Exception>(ex));
			}
			finally
			{
				streamingThread = null;
				Disconnected.RaiseEvent(this, EventArgs.Empty);
				this.Terminated = true;
			}
		}
Example #36
0
 public PreemptiveHttpClient(string username, string password)
 {
     _httpClient = createHttpClient(username, password);
 }
Example #37
0
 public Image GetImage(string url, int size)
 {
     using (CustomWebClient customWebClient = new CustomWebClient())
         return((Image) new Bitmap(Image.FromStream((Stream) new MemoryStream(customWebClient.DownloadData(url.Replace("bdocodex.com", "bddatabase.net")))), new Size(size, size)));
 }
        /// <summary>
        /// Uploads the zip to the dataImportAPI
        /// </summary>
        /// <param name='zipPath'>
        /// Path to the zip file (containing data and xml configuration) to upload
        /// </param>
        /// <param name='action'>
        /// Import action.  Can be 'overwrite' or 'append'
        /// </param>
        /// <param name='runAsBackground'>
        /// determines if the SpatialKey server should return as soon as the data is uploaded 
        /// (thus freeing up your connection), or should it wait for the entire import process to complete
        /// </param>
        /// <param name='notifyByEmail'>
        /// if set as true the SpatialKey server will send an email to the authenticated user when the import is complete
        /// </param>
        /// <param name='addAllUsers'>
        /// if set as true the SpatialKey server will add the All Users group as a viewer of the dataset
        /// </param>
        public void UploadZip(string zipPath, string action = "overwrite", bool runAsBackground = true, bool notifyByEmail = false, bool addAllUsers = false)
        {
            Authenticate ();

            string path = "/SpatialKeyFramework/dataImportAPI";

            string url = String.Format ("{0}{1}{2}",
                                        _protocol,
                                        clusterHost,
                                        path,
                                        action,
                                        runAsBackground.ToString().ToLower(),
                                        notifyByEmail.ToString().ToLower(),
                                        addAllUsers.ToString().ToLower());

            Log(String.Format("UploadZip: {0} {1}", url, zipPath));

            // create the jsessionID cookie
            CookieContainer cookieJar = new CookieContainer ();
            Cookie cookie = new Cookie(_jsessionID.Name, _jsessionID.Value);
            cookieJar.Add(new Uri(String.Format("{0}{1}", _protocol, clusterHost)), cookie);

            CustomWebClient client = new CustomWebClient(cookieJar);

            // add the query string
            NameValueCollection query = new NameValueCollection();
            query.Add("action", action);
            query.Add("runAsBackground", runAsBackground.ToString().ToLower());
            query.Add("notifyByEmail", notifyByEmail.ToString().ToLower());
            query.Add("addAllUsers", addAllUsers.ToString().ToLower());
            client.QueryString = query;
            Log(NameValueCollectionToString(query));

            byte[] response = client.UploadFile(url, zipPath);
            Log(Encoding.ASCII.GetString(response));

            Log("UploadZip: Complete");
        }