private static bool CheckForinternetConnection() { //this might be a possible way to check for internet connection, correct if wrong. try { using (var client = new WebClient()) try { //this will be a test to see if it can establish an internet connection using var stream = client.OpenRead("https://www.google.com") { { result = (p.Send("8.8.8.8", 15000).Status == IPStatus.Success) || (p.Send("8.8.4.4", 15000).Status == IPStatus.Success) || (p.Send("4.2.2.1", 15000).Status == IPStatus.Success); } } } catch { return false; } return result; } }
protected void btnGetLoc_Click(object sender, EventArgs e) { WebClient wc = new WebClient(); Stream data = wc.OpenRead(String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim())); String str; using (StreamReader sr = new StreamReader(data)) { str = sr.ReadToEnd(); data.Close(); } //String url = String.Empty; //if (txtIP.Text.Trim() != String.Empty) //{ // url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim()); // XDocument xDoc = XDocument.Load(url); // if (xDoc == null | xDoc.Root == null) // { // throw new ApplicationException("Data is not Valid"); // } // Xml1.TransformSource = "IP.xslt"; // Xml1.DocumentContent = xDoc.ToString(); //} }
/// <summary> /// Retrieves the specified remote script using a WebClient. /// </summary> /// <param name="file">The remote URL</param> private static string RetrieveScript(string file) { string script = null; try { Uri url = new Uri(file, UriKind.Absolute); using (WebClient client = new WebClient()) using (Stream buffer = client.OpenRead(url)) using (StreamReader reader = new StreamReader(buffer)) { script = StripWhitespace(reader.ReadToEnd()); HttpContext.Current.Cache.Insert(file, script, null, Cache.NoAbsoluteExpiration, new TimeSpan(7, 0, 0, 0)); } } catch (System.Net.Sockets.SocketException) { // The remote site is currently down. Try again next time. } catch (UriFormatException) { // Only valid absolute URLs are accepted } return script; }
private static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Usage: stacktrace_decoder <info_file_uri> <pdb_file>"); return; } string info_file_uri = args[0]; string pdb_file = args[1]; var web_client = new WebClient(); var stream = new StreamReader(web_client.OpenRead(info_file_uri)); var version_regex = new Regex( @"^I.*\] Principia version " + @"([0-9]{10}-[A-Za-z]+)-[0-9]+-g([0-9a-f]{40}) built"); Match version_match; do { version_match = version_regex.Match(stream.ReadLine()); } while (!version_match.Success); string tag = version_match.Groups[1].ToString(); string commit = version_match.Groups[2].ToString(); var base_address_regex = new Regex(@"^I.*\] Base address is ([0-9A-F]+)$"); string base_address_string = base_address_regex.Match(stream.ReadLine()).Groups[1].ToString(); Int64 base_address = Convert.ToInt64(base_address_string, 16); var stack_regex = new Regex( @"^\s+@\s+[0-9A-F]+\s+\(No symbol\) \[0x([0-9A-F]+)\]"); Match stack_match; do { stack_match = stack_regex.Match(stream.ReadLine()); } while (!stack_match.Success); var file_regex = new Regex( @"file\s+:\s+.*\\principia\\([a-z_]+)\\([a-z_]+\.[ch]pp)"); var line_regex = new Regex(@"line\s+:\s+([0-9]+)"); for (; stack_match.Success; stack_match = stack_regex.Match(stream.ReadLine())) { Int64 address = Convert.ToInt64(stack_match.Groups[1].ToString(), 16); Int64 dbh_base_address = 0x1000000; string rebased_address = Convert.ToString(address - base_address + dbh_base_address, 16); var p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = kDBH; p.StartInfo.Arguments = '"' + pdb_file + "\" laddr \"" + rebased_address + '"'; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); Match file_match = file_regex.Match(output); if (file_match.Success) { string file = file_match.Groups[1].ToString() + '/' + file_match.Groups[2].ToString(); string line = line_regex.Match(output).Groups[1].ToString(); string url = "https://github.com/mockingbirdnest/Principia/blob/" + commit + '/' + file + "#L" + line; Console.WriteLine("[`" + file + ":" + line + "`](" + url + ")"); } } }
public static void Main() { WebClient cl = new WebClient(); Stream str = cl.OpenRead("http://www.google.com"); StreamReader read = new StreamReader(str); Console.WriteLine(read.ReadToEnd()); str.Close(); }
private static String getPage(String url) { using(WebClient cl = new WebClient()) using(Stream data = cl.OpenRead(url)) using(StreamReader r = new StreamReader(data)) { return r.ReadToEnd(); } }
static void Main() { using (WebClient download = new WebClient()) { try { download.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "../../AC.jpg"); download.OpenRead("http://www.devbg.org/img/Logo-BASD.jpg"); } catch (Exception Ex) { Console.WriteLine("invalid address!",Ex.Data); } } }
/// <summary> /// 读取网页内容 /// </summary> /// <returns></returns> public static string ReadHtml() { WebClient client = new WebClient(); Stream strm = client.OpenRead("http://www.baidu.com"); StreamReader sr = new StreamReader(strm); string line = string.Empty; string message = string.Empty; while ((line = sr.ReadLine()) != null) { message += line; } strm.Close(); return message; }
public static bool CheckForInternetConnection() { try { using (var client = new WebClient()) using (var stream = client.OpenRead("http://www.google.com")) { return true; } } catch { return false; } }
public static void Main(String [] args) { WebClient client = new WebClient(); client.BaseAddress = args[0]; client.DownloadFile(args[1], args[2]); StreamReader input = new StreamReader(client.OpenRead(args[1])); Console.WriteLine(input.ReadToEnd()); Console.WriteLine ("Request header count: {0}", client.Headers.Count); WebHeaderCollection header = client.ResponseHeaders; Console.WriteLine ("Response header count: {0}", header.Count); for (int i = 0; i < header.Count; i++) Console.WriteLine(" {0} : {1}", header.GetKey(i), header[i]); input.Close(); }
public static string API(string API) { try { WebClient Client = new WebClient(); string baseurl = API; Stream data = Client.OpenRead(baseurl); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); data.Close(); reader.Close(); return s; } catch (Exception ex) { return ex.Message.ToString(); } }
static void SendMessageWithHttpStream(IBus bus) { #region send-message-with-http-stream using (WebClient webClient = new WebClient()) { MessageWithStream message = new MessageWithStream { SomeProperty = "This message contains a stream", StreamProperty = webClient.OpenRead("http://www.particular.net") }; bus.Send("Sample.PipelineStream.Receiver", message); } #endregion Console.WriteLine(); Console.WriteLine("Message with http stream sent"); }
public string GetLocation() { string wholePage = string.Empty; try { using (WebClient webClient = new WebClient()) { Stream stream = webClient.OpenRead("https://freegeoip.net/json/"); StreamReader streamReader = new StreamReader(stream); wholePage = streamReader.ReadToEnd(); streamReader.Close(); stream.Close(); } return wholePage; } catch(Exception e) { return e.ToString(); } }
public string ReadFile() { String fileUrl = "http://marketinvoice.looker.com/looks/XN9ChdBFx4KMSBTMPzQK6P6k6p9FJGWv.txt"; WebClient client = new WebClient(); Stream data = client.OpenRead(fileUrl); StreamReader reader = new StreamReader(data); string text = reader.ReadToEnd(); string[] contentCollection = text.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int length = contentCollection.Length; string result = "[ "; /** Headers **/ string[] headers = new string[] { }; int count = 0; string firstLine = contentCollection[0]; if (firstLine != null) { headers = firstLine.Replace(" ", "").Replace("%", "Percent").Replace("(", "").Replace(")", "").Split('\t'); count = headers.Length; } /** Values **/ string[] row = new string[] { }; for (int countLines = 1; countLines < length; countLines++) { row = contentCollection[countLines].Split('\t'); result += "{"; for (int countValues = 0; countValues < count; countValues++) { result += String.Format("{0}: \"{1}\", ", headers[countValues], row[countValues]); } result += "}, "; } result = result.Replace(@"\", ""); result = result.Remove(result.Length - 1); result += "]"; return result; }
static void Main() { using (WebClient client = new WebClient()) { try { // Reads some file address in Internet Console.WriteLine("Please, enter some file address in Internet: "); Console.ForegroundColor = ConsoleColor.Yellow; string uri = Console.ReadLine(); Console.ResetColor(); // Takes the name of the file Match filename = Regex.Match(uri, @"[^\/]+\.\w+$"); // Checks the size of the file before to be downloaded client.OpenRead(uri); Int64 bytes = Convert.ToInt64(client.ResponseHeaders["Content-Length"]); Console.Write("\nThe size of the file is " + bytes.ToString() + " Bytes. Press <Enter> to start downloading"); Console.ReadLine(); // Downloads the file client.DownloadFile(uri, filename.ToString()); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("The file is successfully downloaded!"); Console.ResetColor(); } catch (WebException) { Error("File not found!"); } catch (ArgumentException) { Error("The path is not of a legal form!"); } catch (PathTooLongException) { Error("The specified path, file name, or both are too long!"); } } }
private void maxWins() { try { using (var client = new WebClient()) { Stream stream = client.OpenRead("http://www.google.com"); StreamReader reader = new StreamReader(stream); string response = reader.ReadToEnd(); Debug.Log(response); } } catch (System.InvalidOperationException e) { if (e is WebException) { Debug.Log(e.Message); //Debug.Log (e.StackTrace); Debug.Log(e.GetBaseException()); } } }
/// <summary> /// Extracts the single task data all. /// this method extracts all task data from job page /// </summary> /// <param name="url">job page such as (base url + jobnumber)</param> /// <param name="jobnumber">Jobnumber. </param> public void extractSingleTaskDataAll(string url, int jobnumber) { try { WebClient client = new WebClient (); //html = client.DownloadString (pageUrl); Stream st = client.OpenRead (url); Encoding encode = Encoding.GetEncoding ("utf-8"); StreamReader sr = new StreamReader (st, encode); string html = sr.ReadToEnd (); sr.Close (); st.Close (); extractJobData (html); insertToDatabase (extractJobData (html), extractAllTableRows (url), jobnumber); } catch (WebException e) { Debug.Log (e.ToString ()); return; } }
private void SavePicnikPhoto(Member member, Photo photo, String picnikPhotoUrl) { Debug.Assert(member != null, "member != null"); Debug.Assert(photo != null, "photo != null"); Debug.Assert(!String.IsNullOrEmpty(picnikPhotoUrl), "!String.IsNullOrEmpty(picnikPhotoUrl)"); using (var client = new WebClient()) using (var stream = client.OpenRead(picnikPhotoUrl)) using (var largeImage = Image.FromStream(stream)) { using (var mediumImage = Photo.Resize480x480(largeImage)) { Photo.SaveToDiskRelativePath(mediumImage, GetMediumFullPath(member, photo)); } using (var thumbImage = Photo.ResizeTo124x91(largeImage)) { Photo.SaveToDiskRelativePath(thumbImage, GetThumbFullPath(member, photo)); } Photo.SaveToDiskRelativePath(largeImage, GetLargeFullPath(member, photo)); } }
private void ReadWordsFromUrl(WordHintDbContext db, User adminUser, string lastWord) { using (WebClient client = new WebClient()) using (Stream stream = client.OpenRead(JSON_URL)) using (StreamReader streamReader = new StreamReader(stream)) using (JsonTextReader reader = new JsonTextReader(streamReader)) { reader.SupportMultipleContent = true; string currentValue = null; List <string> currentList = null; int totalCount = 25000; int count = 0; bool hasFound = false; var serializer = new JsonSerializer(); while (reader.Read()) { // output the stream one chunk at a time // Log.Information(string.Format("{0,-12} {1}", // reader.TokenType.ToString(), // reader.Value != null ? reader.Value.ToString() : "(null)")); switch (reader.TokenType) { // JsonToken.StartObject = deserialize only when there's "{" character in the stream case JsonToken.StartObject: break; // JsonToken.PropertyName = deserialize only when there's a "text": in the stream case JsonToken.PropertyName: currentValue = reader.Value.ToString(); break; // JsonToken.String = deserialize only when there's a "text" in the stream case JsonToken.String: currentList.Add(reader.Value.ToString()); break; // JsonToken.StartArray = deserialize only when there's "[" character in the stream case JsonToken.StartArray: currentList = new List <string>(); break; // JsonToken.EndArray = deserialize only when there's "]" character in the stream case JsonToken.EndArray: count++; // skip until we reach last word beginning if (lastWord != null) { if (currentValue.ToUpperInvariant().Equals(lastWord)) { hasFound = true; } } else { hasFound = true; } // store to database if (hasFound) { // update that we are processing this word, ignore length and comment WordDatabaseService.UpdateState(db, source, new Word() { Value = currentValue.ToUpper(), Source = source, CreatedDate = DateTime.Now }, writer, true); // disable storing state since we are doing it manually above WordDatabaseService.AddToDatabase(db, source, adminUser, currentValue, currentList, writer, false); // if (writer != null) writer.WriteLine("Added '{0} => {1}'", currentValue, string.Join(",", currentList)); if ((count % 10) == 0) { if (writer != null) { writer.WriteLine("[{0}] / [{1}]", count, totalCount); } } } // and reset currentList = null; currentValue = null; break; // JsonToken.EndObject = deserialize only when there's "}" character in the stream case JsonToken.EndObject: currentList = null; currentValue = null; break; } } } /* * // reading the whole thing took approx the same time as the streaming version * { * var json = streamReader.ReadToEnd(); * var jobj = JObject.Parse(json); * * var totalCount = jobj.Properties().Count(); * int count = 0; * foreach (var item in jobj.Properties()) * { * count++; * * var currentValue = item.Name; * var currentList = item.Values().Select(a => a.Value<string>()); * * WordDatabaseService.AddToDatabase(db, source, adminUser, currentValue, currentList); * * // if (writer != null) writer.WriteLine("Added '{0} => {1}'", currentValue, string.Join(",", currentList)); * if (writer != null) writer.WriteLine("[{0}] / [{1}]", count, totalCount); * } * } */ }
private static void Main(string[] args) { bool unity_crash = false; Func <string, string> comment = Comment; bool snippets = true; string commit = null; if (args.Length < 2) { PrintUsage(); return; } string info_file_uri = args[0]; string principia_directory = args[1]; for (int i = 2; i < args.Length; ++i) { string flag = args[i]; var match = Regex.Match(flag, "--unity-crash-at-commit=([0-9a-f]{40})"); if (!unity_crash && match.Success) { unity_crash = true; commit = match.Groups[1].ToString(); } else if (snippets && flag == "--no-snippet") { snippets = false; } else if (comment == Comment && flag == "--no-comment") { comment = (_) => ""; } else { PrintUsage(); return; } } var web_client = new WebClient(); var stream = new StreamReader(web_client.OpenRead(info_file_uri), Encoding.UTF8); if (!unity_crash) { var version_regex = new Regex( @"^I.*\] Principia version " + @"([0-9]{10}-\w+)-[0-9]+-g([0-9a-f]{40})(-dirty)? built"); Match version_match; do { Check(!stream.EndOfStream, $"Could not find Principia version line in {info_file_uri}"); version_match = version_regex.Match(stream.ReadLine()); } while (!version_match.Success); commit = version_match.Groups[2].ToString(); bool dirty = version_match.Groups[3].Success; if (dirty) { Console.Write(comment( $"Warning: version is dirty; line numbers may be incorrect.")); } } Int64 principia_base_address = GetBaseAddress(unity_crash, @"GameData\\Principia\\x64\\principia.dll:principia.dll " + @"\(([0-9A-F]+)\)", "interface\\.cpp", stream); Console.Write( comment($"Using Principia base address {principia_base_address:X}")); var stack_regex = new Regex( unity_crash ? @"0x([0-9A-F]+) .*" : @"@\s+[0-9A-F]+\s+.* \[0x([0-9A-F]+)(\+[0-9]+)?\]"); Match stack_match; if (unity_crash) { Match stack_start_match; do { stack_start_match = Regex.Match(stream.ReadLine(), @"========== OUTPUTING STACK TRACE =================="); } while (!stack_start_match.Success); } do { stack_match = stack_regex.Match(stream.ReadLine()); } while (!stack_match.Success); IntPtr handle = new IntPtr(1729); SymSetOptions(SYMOPT_LOAD_LINES); Win32Check(SymInitializeW(handle, null, fInvadeProcess: false)); string principia_dll_path = Path.Combine(principia_directory, "principia.dll"); Win32Check(File.Exists(principia_dll_path)); Win32Check( SymLoadModuleExW(handle, IntPtr.Zero, principia_dll_path, null, principia_base_address, 0, IntPtr.Zero, 0) != 0); var trace = new Stack <string>(); for (; stack_match.Success; stack_match = stack_regex.Match(stream.ReadLine())) { Int64 address = Convert.ToInt64(stack_match.Groups[1].ToString(), 16); IMAGEHLP_LINEW64 line = new IMAGEHLP_LINEW64(); SYMBOL_INFOW symbol = new SYMBOL_INFOW(); int inline_trace = SymAddrIncludeInlineTrace(handle, address); if (inline_trace != 0) { Win32Check(SymQueryInlineTrace(handle, address, 0, address, address, out int current_context, out int current_frame_index)); for (int i = 0; i < inline_trace; ++i) { Win32Check(SymGetLineFromInlineContextW( handle, address, current_context + i, 0, out int dsp, line)); Win32Check(SymFromInlineContextW(handle, address, current_context + i, out Int64 displacement64, symbol)); trace.Push(ParseLine(handle, line, symbol, commit, snippets) ?? comment("Inline frame not in Principia code")); } } if (SymGetLineFromAddrW64(handle, address, out int displacement, line)) { Win32Check( SymFromAddrW(handle, address, out Int64 displacement64, symbol)); trace.Push(ParseLine(handle, line, symbol, commit, snippets) ?? comment($"Not in Principia code: {stack_match.Groups[0]}")); }
private async void Main_Load(object sender, System.EventArgs e) { var uiContext = SynchronizationContext.Current; uiContext.Post(UpdateCurrentOperation, "Searching for RemotePlay.exe..."); _remotePlayPath = FindRemotePlay(); if (string.IsNullOrEmpty(_remotePlayPath)) { if (MessageBox.Show("The PS4 Remote Play couldn't be found. Please select the file that needs to be patched.", "Not found", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel) { Environment.Exit(0); } using (var dialog = new OpenFileDialog()) { dialog.Title = "Select the RemotePlay.exe you want to patch"; dialog.Filter = "RemotePlay.exe|RemotePlay.exe"; if (dialog.ShowDialog() != DialogResult.OK) { Environment.Exit(0); } _remotePlayPath = dialog.FileName; } } // For some reason the FileVersion FileVersionInfo provides isn't exactly the one we're looking for. // Sometimes padding is added to one of the parts. var version = FileVersionInfo.GetVersionInfo(_remotePlayPath); var fileVersion = $"{version.FileMajorPart}.{version.FileMinorPart}.{version.FileBuildPart}.{version.FilePrivatePart}"; await Task.Run(() => { try { new RetryPolicy() .MaxAttempts(int.MaxValue) .Abort.IfException <Exception>(ex => MessageBox.Show($"An error occured while downloading the latest patches.\n{ex}", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Cancel) .Try(() => { using (var cryptoProvider = new MD5CryptoServiceProvider()) using (var client = new WebClient()) { uiContext.Post(UpdateCurrentOperation, "Computing RemotePlay.exe's hash..."); var hash = string.Join(string.Empty, cryptoProvider.ComputeHash(File.ReadAllBytes(_remotePlayPath)).Select(b => b.ToString("X2"))); uiContext.Post(UpdateCurrentOperation, "Downloading latest patches from GitHub..."); using (var zipStream = new ZipInputStream(client.OpenRead(PatchesLocation))) { var zipEntry = zipStream.GetNextEntry(); while (zipEntry != null) { var patchName = zipEntry.Name; if (patchName.StartsWith(fileVersion, StringComparison.InvariantCultureIgnoreCase) && patchName.EndsWith(hash, StringComparison.InvariantCultureIgnoreCase)) { using (var memoryStream = new MemoryStream()) { zipStream.CopyTo(memoryStream); _patch = memoryStream.ToArray(); } uiContext.Post(UpdateCurrentOperation, "Ready !"); uiContext.Post(EnablePatchButton, true); return; } zipEntry = zipStream.GetNextEntry(); } throw new Exception("Patch not found or RemotePlay.exe is already patched !"); } } }); } catch (AggregateException) { Environment.Exit(0); } }); }
private async Task LoadData(DateTime data) { var time = data; var month = time.Month.ToString("00"); var day = time.Day.ToString("00"); var tempFileName = ""; var ImageUrl = ""; Directory.CreateDirectory(ImagesDirectory); var files = Directory.GetFiles(ImagesDirectory); Regex findImage = new Regex($@"{time.Year}-{month}-{day}.*{Settings.LabType.Value}.jpg"); foreach (string file in files) { Match match = findImage.Match(file); if (match.Success) { tempFileName = file; break; } } var FilePath = Path.Combine(ImagesDirectory, tempFileName); if (File.Exists(FilePath)) { LogMessage("Loading lab layout image from cache", 3); ImagePathToDraw = FilePath; Graphics.InitImage(FilePath, false); ImageState = ImageCheckState.ReadyToDraw; return; } LogMessage("Loading new lab layout image from site", 3); ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }; ImageState = ImageCheckState.Downloading; var imageBytes = new byte[0]; using (WebClient webClient = new WebClient()) { var mainPageStream = webClient.OpenRead(poeLabLink); using (StreamReader mainPageReader = new StreamReader(mainPageStream)) { MatchCollection labPagesLinks = labPageLinkCatch.Matches(mainPageReader.ReadToEnd()); string labName = "uber"; switch (Settings.LabType.Value) { case "normal": labName = "normal"; break; case "cruel": labName = "cruel"; break; case "merciless": labName = "merc"; break; case "uber": labName = "uber"; break; } ; foreach (Match labPageLink in labPagesLinks) { if (labPageLink.Value.ToLower().Contains(labName)) { var labPageStream = webClient.OpenRead(labPageLink.Groups[1].Value); using (StreamReader labPageReader = new StreamReader(labPageStream)) { Regex imageLinkCatch = new Regex($@"labfiles\/.*{Settings.LabType.Value}\.jpg"); MatchCollection matches1 = imageLinkCatch.Matches(labPageReader.ReadToEnd()); foreach (Match match in matches1) { if (match.Value.Length == 0) { continue; } ImageUrl = poeLabImagePrefix + match.Value; FilePath = Path.Combine(ImagesDirectory, match.Value.Replace("labfiles/", "")); break; } } break; } } } try { imageBytes = await webClient.DownloadDataTaskAsync(ImageUrl); } catch // (Exception ex) { ImageState = ImageCheckState.NotFound404; //Not found return; } } Bitmap downloadedImage; try { using (var ms = new MemoryStream(imageBytes)) { downloadedImage = new Bitmap(ms); } if (!Directory.Exists(ImagesDirectory)) { Directory.CreateDirectory(ImagesDirectory); } downloadedImage = new Bitmap(downloadedImage); //Fix for exception (bitmap save problem https://stackoverflow.com/questions/5813633/a-generic-error-occurs-at-gdi-at-bitmap-save-after-using-savefiledialog ) downloadedImage = downloadedImage.Clone(new Rectangle(302, 111, ImageWidth, ImageHeight), downloadedImage.PixelFormat); downloadedImage.Save(FilePath, ImageFormat.Jpeg); Graphics.InitImage(FilePath, false); ImageState = ImageCheckState.ReadyToDraw; ImagePathToDraw = FilePath; downloadedImage.Dispose(); } catch (Exception ex) { LogError($"{Name}: Error while cropping or saving image: {ex.Message}", 10); ImageState = ImageCheckState.FailedToLoad; } }
private void btnDownLoad_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); DialogResult result = fbd.ShowDialog(); if (result == DialogResult.OK) { string path = Path.GetFullPath(fbd.SelectedPath); path = ChangePathToGoal(path); // Những file có thể tải là . css , javascript , image (một số định dạng cơ bản png v.v.v , gif , int checkcss = 0, checkjs = 0, checkimage = 0; // check đã load URL với path lưu file linksrc.txt thành công chưa int i = 0; // biến lưu thứ tự của image được lưu . HtmlWeb web = new HtmlWeb(); HtmlAgilityPack.HtmlDocument docfilecss = new HtmlAgilityPack.HtmlDocument(); // tải file css hoặc những file trong thuộc tính href của thẻ link HtmlAgilityPack.HtmlDocument docfilejs = new HtmlAgilityPack.HtmlDocument(); // tải file js hoặc những file khác có đuôi 2 kí tự trong thuộc tính href của thẻ link HtmlAgilityPack.HtmlDocument docfileimage = new HtmlAgilityPack.HtmlDocument(); FileStream fs = null; StreamWriter sw = null; // tạo 2 biến này để lưu lại URL những link bắt được để tải về. để kiểm tra xem những link nào không tải được. WebClient myclient1 = new WebClient(); Stream response = myclient1.OpenRead(txtURL.Text); myclient1.DownloadFile(txtURL.Text, path + "\\index.html"); // Download file html xuống . response.Close(); //---------Đọc file img , gif-------------- try { // load URL docfileimage = web.Load(txtURL.Text); fs = new FileStream(path + "\\" + "text.txt", FileMode.OpenOrCreate); // cô bỏ file chứa tất cả link src này ở đâu củng đươc để check những gì nó get đươc nha sw = new StreamWriter(fs); checkimage = 1; } catch (Exception h10) { MessageBox.Show(h10.Message); } if (checkimage == 1) // khi load URL thành công { try { foreach (HtmlNode link in docfileimage.DocumentNode.SelectNodes("//img")) // chọn thẻ img { try { string a = link.Attributes["src"].Value; // chọn src trong thẻ img sw.WriteLine(a); string savefile = path + "\\" + i + returnstring(a); WebClient myclient = new WebClient(); myclient.DownloadFile(a, savefile); i++; } catch { try { // một số urlg khi get về không đúng định dạng , có thể nó lưu trong cache , vd chỉ /abc/xyz/ayk.png . nên phải fix url lại để có thể download được . string a = txtURL.Text + link.Attributes["src"].Value; sw.WriteLine(a); string savefile = path + "\\" + i + returnstring(a); WebClient myclient = new WebClient(); myclient.DownloadFile(a, savefile); i++; } catch { MessageBox.Show("sever error return file"); // có một số web không thể get file về được như youtube .v.v. } } } } catch (Exception k4) { MessageBox.Show(k4.Message); } } //// -----------Đọc file css------------------ try { // load URL docfilecss = web.Load(txtURL.Text); checkcss = 1; } catch (Exception k) { MessageBox.Show(k.Message); } if (checkcss == 1) // khi load URL thành công { try { foreach (HtmlNode link in docfilecss.DocumentNode.SelectNodes("//link")) // chọn thẻ link { try { string a = link.Attributes["href"].Value; string savefile = path + "\\" + i + returnstring(a); WebClient myclient = new WebClient(); myclient.DownloadFile(a, savefile); i++; sw.WriteLine(a); } catch { MessageBox.Show("có link không tải được"); // một số url khi get về không đúng định dạng , có thể nó lưu trong cache , vd chỉ /abc/xyz/ayk.png . nên phải fix url lại để có thể download được . string a = txtURL.Text + link.Attributes["href"].Value; sw.WriteLine(link.Attributes["href"].Value); string savefile = path + "\\" + i + returnstring(a); WebClient myclient = new WebClient(); try { myclient.DownloadFile(a, savefile); i++; } catch (Exception k1) { MessageBox.Show(k1.Message); } } } } catch (Exception k3) { MessageBox.Show(k3.Message); } } // ---- docfile javascript ------ try { // load URL docfilejs = web.Load(txtURL.Text); checkjs = 1; } catch (Exception k6) { MessageBox.Show(k6.Message); } if (checkjs == 1) // khi load URL thành công { try { foreach (HtmlNode link in docfilejs.DocumentNode.SelectNodes("//script")) // chọn thẻ link { try { string a = link.Attributes["src"].Value; // chọn src trong thẻ script string savefile = path + "\\" + i + ".js"; WebClient myclient = new WebClient(); myclient.DownloadFile(a, savefile); i++; sw.WriteLine(a); } catch { MessageBox.Show("có link không tải được"); string a = txtURL.Text + link.Attributes["src"].Value; sw.WriteLine(link.Attributes["src"].Value); string savefile = path + "\\" + i + returnstring(a); WebClient myclient = new WebClient(); try { myclient.DownloadFile(a, savefile); i++; } catch (Exception k1) { MessageBox.Show(k1.Message); } } } } catch (Exception k5) { MessageBox.Show(k5.Message); } } MessageBox.Show("Tất cả lưu ở " + path); } }
/* goodB2G2() - use badsource and goodsink by reversing statements in second if */ private void GoodB2G2() { string password; if (IO.STATIC_READONLY_FIVE == 5) { password = ""; /* init password */ /* read input from WebClient */ { try { using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read password from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ password = sr.ReadLine(); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, "Error with stream reading", exceptIO); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure password is inititialized before the Sink to avoid compiler errors */ password = null; } if (IO.STATIC_READONLY_FIVE == 5) { if (password != null) { /* FIX: Decrypt password before using in getConnection() */ { using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.Padding = PaddingMode.None; aesAlg.Key = Encoding.UTF8.GetBytes("ABCDEFGHABCDEFGH"); // Create a decryptor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(Encoding.UTF8.GetBytes(password))) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. password = srDecrypt.ReadToEnd(); } } } } } try { /* POTENTIAL FLAW: use password directly in SqlConnection() */ using (SqlConnection connection = new SqlConnection(@"Data Source=(local);Initial Catalog=CWE256;User ID=" + "sa" + ";Password="******"select * from test_table", connection)) { command.ExecuteNonQuery(); } } } catch (SqlException exceptSql) { IO.Logger.Log(NLog.LogLevel.Warn, "Error with database connection", exceptSql); } } } }
protected static void web_commit(string filename, ImageFormat format) { using (var client = new WebClient()) { client.Headers[HttpRequestHeader.Accept] = "*/*"; // client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"; // client.Headers[HttpRequestHeader.AcceptLanguage] = "zh-Hans-CN, zh-Hans; q=0.8, en-US; q=0.5, en; q=0.3"; // client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8"; // client.Headers[HttpRequestHeader.ContentEncoding] = "gzip, deflate, br"; client.Headers[HttpRequestHeader.Cookie] = "JSESSIONID=CA3D895ED79C8D9433F102E469AD8FB3"; // client.Headers[HttpRequestHeader.Accept] = "*/*"; client.Headers[HttpRequestHeader.Host] = "oa.chinasupercloud.com"; // client.Headers[HttpRequestHeader.CacheControl] = "no-cache"; Stream stream = client.OpenRead("https://oa.chinasupercloud.com/api/verifycode/generate?rand=123"); Bitmap bitmap; bitmap = new Bitmap(stream); if (bitmap != null) { bitmap.Save(filename, format); } stream.Flush(); stream.Close(); // client.Dispose(); Console.WriteLine("get there image"); client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; // client.Headers[HttpRequestHeader.Accept ] = "*/*"; // client.Headers[HttpRequestHeader.ContentLength] = "68"; //client.Headers[HttpRequestHeader.Cookie] = "loginName=\"chenjx @chinasupercloud.com\"; [email protected]; JSESSIONID=C4BCB32E27C87F6B1A49127644B228CE; token=286744344783\""; // client.Headers[HttpRequestHeader.Connection] = "keep-alive"; // client.Headers[HttpRequestHeader.Host] = "oa.chinasupercloud.com"; // client.Headers[HttpRequestHeader.] = "gzip,deflate"; // client.Headers[HttpResponseHeader.CacheControl] = "no-cache"; // client.Headers[HttpResponseHeader.Connection] = "keep-alive"; // client.Headers[HttpResponseHeader.ContentEncoding] = "gzip"; // client.Headers[HttpResponseHeader.ContentType] = "application/json; charset=UTF-8"; // client.Headers[HttpResponseHeader.Pragma] = "no-cache"; // client.Headers[HttpRequestHeader.Server] = "nginx/1.10.2"; // client.Headers[HttpResponseHeader.TransferEncoding] = "chunked"; // client.Headers[HttpResponseHeader.Vary] = "Accept-Encoding"; var values = new NameValueCollection(); string code = Console.ReadLine(); Console.WriteLine(code); values["verycode"] = code; values["email"] = "*****@*****.**"; values["password"] = "******"; var response = client.UploadValues("https://oa.chinasupercloud.com/api/login", "POST", values); var responseString = Encoding.Default.GetString(response); Console.WriteLine(responseString); // string restr= client.DownloadString("https://oa.chinasupercloud.com/api/product/list"); // Console.WriteLine(restr); } //using(var client =new WebClient()) //{ //} }
/* goodB2G1() - use badsource and goodsink by changing second IO.STATIC_READONLY_FIVE==5 to IO.STATIC_READONLY_FIVE!=5 */ private void GoodB2G1() { int data; if (IO.STATIC_READONLY_FIVE == 5) { data = int.MinValue; /* Initialize data */ /* read input from WebClient */ { try { string stringNumber = ""; using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read data from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ stringNumber = sr.ReadLine(); } } if (stringNumber != null) // avoid NPD incidental warnings { try { data = int.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing data from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = 0; } if (IO.STATIC_READONLY_FIVE != 5) { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ IO.WriteLine("Benign, fixed string"); } else { /* FIX: test for a zero denominator */ if (data != 0) { IO.WriteLine("100/" + data + " = " + (100 / data) + "\n"); } else { IO.WriteLine("This would result in a divide by zero"); } } }
/* goodB2G2() - use badsource and goodsink by reversing statements in second if */ private void GoodB2G2() { int data; if (PrivateReturnsTrue()) { data = int.MinValue; /* Initialize data */ /* read input from WebClient */ { try { string stringNumber = ""; using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read data from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ stringNumber = sr.ReadLine(); } } if (stringNumber != null) // avoid NPD incidental warnings { try { data = int.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing data from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = 0; } if (PrivateReturnsTrue()) { /* Need to ensure that the array is of size > 3 and < 101 due to the GoodSource and the large_fixed BadSource */ int[] array = { 0, 1, 2, 3, 4 }; /* FIX: Verify index before writing to array at location data */ if (data >= 0 && data < array.Length) { array[data] = 42; } else { IO.WriteLine("Array index out of bounds"); } } }
private void button1_Click(object sender, EventArgs e) { int tmp; if (int.TryParse(textBox1.Text, out tmp)) { if (tmp >= 1 && tmp <= 600000 && textBox1.Text.Length == 6) { progressBar1.Value = 0; progressBar1.Minimum = 0; progressBar1.Maximum = 106; textBox2.Text = ""; string url; WebClient wc = new WebClient();//创建WebClient对象 Stream News; StreamReader Newsr; string line = ""; Match m; Regex ResultNum = new Regex("<div align=\"center\">([\\d\\.]+)</div></td>"); Regex date = new Regex(@"date=(\d{4}-\d{2}-\d{2})'>"); Regex title = new Regex("target=\"_self\">(.+)</a></h1>"); StringBuilder Result = new StringBuilder(); int count; int count2 = 0; for (int year = 1990; year < 2017; year++) { for (int jidu = 1; jidu < 5; jidu++) { count = 0; url = "http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/" + textBox1.Text + ".phtml?year=" + year.ToString() + "&jidu=" + jidu.ToString(); News = wc.OpenRead(url); Newsr = new StreamReader(News, Encoding.Default); while ((line = Newsr.ReadLine()) != null) { m = title.Match(line); if (count2 == 0)//保证以下只出现一次 { if (m.Success) { Result.Append(m.Groups[1].ToString() + "\r\n"); Result.Append("date \t , 开盘价 \t , 最高价 \t, 收盘价 \t , 最低价 \t , 交易量(股) \t , 交易金额(元) \r\n"); } } //导出csv出现中文乱码 通过streamwriter 编码已经解决 m = date.Match(line); if (m.Success) { Result.Append(m.Groups[1].ToString()); } m = ResultNum.Match(line); if (m.Success) { Result.Append(","); Result.Append(m.Groups[1].ToString()); count++; if (count % 6 == 0) { Result.Append("\r\n"); } } } News.Close(); Newsr.Close(); count2++; if (progressBar1.Value < progressBar1.Maximum) { progressBar1.Value++; output("( " + Math.Round(((double)progressBar1.Value * 100 / (double)progressBar1.Maximum), 2, 0).ToString() + "% )"); } } } //WriteTxt(Result.ToString()); string str2 = Environment.CurrentDirectory; string filepath = str2 + "\\" + textBox1.Text + ".csv"; output("文件已保存到" + filepath); try { StreamWriter sw = new StreamWriter(filepath, false, UnicodeEncoding.GetEncoding("GB2312")); sw.Write(Result.ToString()); sw.Close(); MessageBox.Show("保存成功!"); } catch (Exception ex) { } } else { MessageBox.Show("输入股票号有误"); } } }
/* goodB2G1() - use badsource and goodsink by changing second privateTrue to privateFalse */ private void GoodB2G1() { int count; if (privateTrue) { count = int.MinValue; /* Initialize count */ /* read input from WebClient */ { try { string stringNumber = ""; using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read count from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ stringNumber = sr.ReadLine(); } } if (stringNumber != null) // avoid NPD incidental warnings { try { count = int.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing count from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure count is inititialized before the Sink to avoid compiler errors */ count = 0; } if (privateFalse) { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ IO.WriteLine("Benign, fixed string"); } else { /* FIX: Validate count before using it in a call to Thread.Sleep() */ if (count > 0 && count <= 2000) { Thread.Sleep(count); } } }
public override void Bad() { int data; if (IO.STATIC_READONLY_FIVE == 5) { data = int.MinValue; /* Initialize data */ /* read input from WebClient */ { try { string stringNumber = ""; using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read data from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ stringNumber = sr.ReadLine(); } } if (stringNumber != null) // avoid NPD incidental warnings { try { data = int.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing data from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = 0; } if (IO.STATIC_READONLY_FIVE == 5) { int[] array = null; /* POTENTIAL FLAW: Verify that data is non-negative, but still allow it to be 0 */ if (data >= 0) { array = new int[data]; } else { IO.WriteLine("Array size is negative"); } /* do something with the array */ array[0] = 5; IO.WriteLine(array[0]); } }
public static void SText(string type, string url, bool antifiddler, bool verboselog) { Console.OutputEncoding = Encoding.UTF8; #region antifiddler if (antifiddler) { wc.Proxy = wp; wc.Encoding = Encoding.UTF8; } else if (!antifiddler) { wc.Proxy = null; wc.Encoding = Encoding.UTF8; } else { dummy.Error("AntiFiddler: Erro Desconhecido."); } #endregion bool checkurl = Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); if (!checkurl) { dummy.Error("URL Invalida."); return; } if (type == "javaw") { Process[] pname = Process.GetProcessesByName("javaw"); if (pname.Length == 0) { dummy.Warning("Processo javaw nao encontrado"); return; } dummy.Success("Coletando strings do javaw [" + pegapid("javaw") + "]"); PuxarStrings("javaw"); using (Stream stream = wc.OpenRead(url)) { using (StreamReader streamReader = new StreamReader(stream)) { string linha; while ((linha = streamReader.ReadLine()) != null) { string value = linha.Split(new char[] { 'ø' })[0]; string str = linha.Split(new char[] { 'ø' })[1]; if (verboselog) { dummy.Info("Testando client: " + str); } string final = File.ReadAllText(strings); if (final.Contains(value)) { dummy.Error("Client Encontrado: " + str); deldir(); break; } } } } } else if (type == "explorer") { Process[] pname = Process.GetProcessesByName("explorer"); if (pname.Length == 0) { dummy.Warning("Processo nao encontrado"); return; } dummy.Success("Coletando strings do explorer [" + pegapid("explorer") + "]"); PuxarStrings("explorer"); using (Stream stream = wc.OpenRead(url)) { using (StreamReader streamReader = new StreamReader(stream)) { string linha; while ((linha = streamReader.ReadLine()) != null) { string value = linha.Split(new char[] { 'ø' })[0]; string str = linha.Split(new char[] { 'ø' })[1]; if (verboselog) { dummy.Info("Testando client: " + str); } string final = File.ReadAllText(strings); if (final.Contains(value)) { dummy.Error("Client Encontrado: " + str); deldir(); break; } } } } } else if (type == "lsass") { Process[] pname = Process.GetProcessesByName("lsass"); if (pname.Length == 0) { dummy.Warning("Processo lsass nao encontrado"); return; } dummy.Success("Coletando strings do lsass [" + pegapid("lsass") + "]"); PuxarStrings("lsass"); using (Stream stream = wc.OpenRead(url)) { using (StreamReader streamReader = new StreamReader(stream)) { string linha; while ((linha = streamReader.ReadLine()) != null) { string value = linha.Split(new char[] { 'ø' })[0]; string str = linha.Split(new char[] { 'ø' })[1]; if (verboselog) { dummy.Info("Testando client: " + str); } string final = File.ReadAllText(strings); if (final.Contains(value)) { dummy.Error("Client Encontrado: " + str); break; } } } } } else { dummy.Error("Tipo Incorreto!"); } }
public static void GetInfoItems() { var count = 1; var list = GetItem(); var listVideo = new StreamWriter(Directory.GetCurrentDirectory() + "\\ListVideo.txt"); var listInfo = new StreamWriter(Directory.GetCurrentDirectory() + "\\ListInfo.txt"); foreach (var item in list) { var webClient = new WebClient(); string html = null; using (var stream = webClient.OpenRead(new Uri(item.LinkPost))) if (stream != null) { using (var reader = new StreamReader(stream)) { html = reader.ReadToEnd(); } } var doc = new HtmlDocument(); doc.LoadHtml(html); var curenPath = "//*[@id='player_playing']"; var nodes = doc.DocumentNode.SelectNodes(curenPath); foreach (var node in nodes) { var lines = node.InnerHtml.Split(';'); foreach (var line in lines) { if (!line.Contains("videoSource")) { continue; } if (line.Contains("= false")) { continue; } var temp = line.Split('\''); item.LinkVideo = temp[1]; } } curenPath = "//*[@class='lead_detail_video width_common space_bottom_10']"; nodes = doc.DocumentNode.SelectNodes(curenPath); foreach (var node in nodes) { item.Desc = node.InnerText; } curenPath = "//*[@class='block_timer left txt_666']"; nodes = doc.DocumentNode.SelectNodes(curenPath); foreach (var node in nodes) { var temp = node.LastChild.InnerText.Split(';'); item.Date = temp[temp.Length - 1].Trim(); } Console.WriteLine("{0}%", count * 100 / list.Count); listInfo.WriteLine(count++); listInfo.WriteLine(item.Date); listInfo.WriteLine(item.Id); listInfo.WriteLine(item.Title); listInfo.WriteLine(item.Desc); listInfo.WriteLine(item.LinkPost); listInfo.WriteLine(item.LinkVideo); listVideo.WriteLine(item.LinkVideo); listInfo.WriteLine(item.Timer); listInfo.WriteLine(); } listInfo.Close(); listVideo.Close(); }
public override void Bad() { int count; if (PrivateReturnsTrue()) { count = int.MinValue; /* Initialize count */ /* read input from WebClient */ { try { string stringNumber = ""; using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read count from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ stringNumber = sr.ReadLine(); } } if (stringNumber != null) // avoid NPD incidental warnings { try { count = int.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing count from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure count is inititialized before the Sink to avoid compiler errors */ count = 0; } if (PrivateReturnsTrue()) { int i; using (StreamWriter file = new StreamWriter(@"badSink.txt")) { /* POTENTIAL FLAW: Do not validate count before using it as the for loop variant to write to a file */ for (i = 0; i < count; i++) { try { file.Write("Hello"); } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, "Error with stream writing", exceptIO); } } /* Close stream reading objects */ try { if (file != null) { file.Close(); } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, "Error closing Stream Writer", exceptIO); } } } }
public async Task FortStats(string platform, [Remainder] string username) { if (Tokens.Load().FortniteToken == null) { await ReplyAsync( "Fortnite Token has not been initialised for this bot, the owner must go to: https://fortnitetracker.com/site-api to get one"); return; } if (platform.ToLower() != "pc" && platform.ToLower() != "xbl" && platform.ToLower() != "psn") { await FortStats(); return; } var url = $"https://api.fortnitetracker.com/v1/profile/{platform.ToLower()}/{username}"; var client = new WebClient(); client.Headers.Add("TRN-Api-Key", Tokens.Load().FortniteToken); var stream = client.OpenRead(new Uri(url)); var reader = new StreamReader(stream ?? throw new InvalidOperationException()); var fstats = JsonConvert.DeserializeObject <FortniteProfile>(reader.ReadToEnd()); var CurrentSoloSeasonStats = fstats.stats.curr_p2; var LifetimeSoloStats = fstats.stats.p2; var CurrentDuoStats = fstats.stats.curr_p10; var LifetimeDuoStats = fstats.stats.p10; var CurrentSquadStats = fstats.stats.curr_p9; var LifetimeSquadStats = fstats.stats.p9; var CurrentSolo = $"__**Current Solo Stats**__\n" + $"KD: {CurrentSoloSeasonStats.kd.value}\n" + $"Kills: {CurrentSoloSeasonStats.kills.value}\n" + $"Kills/Game: {CurrentSoloSeasonStats.kpg.value}\n" + $"Matches: {CurrentSoloSeasonStats.matches.value}\n" + $"Score: {CurrentSoloSeasonStats.score.value}\n" + $"Score/Game: {CurrentSoloSeasonStats.scorePerMatch.value}\n" + $"Wins: {CurrentSoloSeasonStats.top1.value}\n" + $"Top10: {CurrentSoloSeasonStats.top10.value}\n" + $"Top25: {CurrentSoloSeasonStats.top25.value}\n" + $"TRNRating: {CurrentSoloSeasonStats.trnRating.value}"; var LifeSolo = $"__**Lifetime Solo Stats**__\n" + $"KD: {LifetimeSoloStats.kd.value}\n" + $"Kills: {LifetimeSoloStats.kills.value}\n" + $"Kills/Game: {LifetimeSoloStats.kpg.value}\n" + $"Matches: {LifetimeSoloStats.matches.value}\n" + $"Score: {LifetimeSoloStats.score.value}\n" + $"Score/Game: {LifetimeSoloStats.scorePerMatch.value}\n" + $"Wins: {LifetimeSoloStats.top1.value}\n" + $"Top10: {LifetimeSoloStats.top10.value}\n" + $"Top25: {LifetimeSoloStats.top25.value}\n" + $"TRNRating: {LifetimeSoloStats.trnRating.value}"; var CurrentDuo = $"__**Current Duo Stats**__\n" + $"KD: {CurrentDuoStats.kd.value}\n" + $"Kills: {CurrentDuoStats.kills.value}\n" + $"Kills/Game: {CurrentDuoStats.kpg.value}\n" + $"Matches: {CurrentDuoStats.matches.value}\n" + $"Score: {CurrentDuoStats.score.value}\n" + $"Score/Game: {CurrentDuoStats.scorePerMatch.value}\n" + $"Wins: {CurrentDuoStats.top1.value}\n" + $"Top5: {CurrentDuoStats.top5.value}\n" + $"Top12: {CurrentDuoStats.top12.value}\n" + $"TRNRating: {CurrentDuoStats.trnRating.value}"; var LifetimeDuo = $"__**Lifetime Duo Stats**__\n" + $"KD: {LifetimeDuoStats.kd.value}\n" + $"Kills: {LifetimeDuoStats.kills.value}\n" + $"Kills/Game: {LifetimeDuoStats.kpg.value}\n" + $"Matches: {LifetimeDuoStats.matches.value}\n" + $"Score: {LifetimeDuoStats.score.value}\n" + $"Score/Game: {LifetimeDuoStats.scorePerMatch.value}\n" + $"Wins: {LifetimeDuoStats.top1.value}\n" + $"Top5: {LifetimeDuoStats.top5.value}\n" + $"Top12: {LifetimeDuoStats.top12.value}\n" + $"TRNRating: {LifetimeDuoStats.trnRating.value}"; var CurrentSquad = $"__**Current Squad Stats**__\n" + $"KD: {CurrentSquadStats.kd.value}\n" + $"Kills: {CurrentSquadStats.kills.value}\n" + $"Kills/Game: {CurrentSquadStats.kpg.value}\n" + $"Matches: {CurrentSquadStats.matches.value}\n" + $"Score: {CurrentSquadStats.score.value}\n" + $"Score/Game: {CurrentSquadStats.scorePerMatch.value}\n" + $"Wins: {CurrentSquadStats.top1.value}\n" + $"Top3: {CurrentSquadStats.top3.value}\n" + $"Top6: {CurrentSquadStats.top6.value}\n" + $"TRNRating: {CurrentSquadStats.trnRating.value}"; var LifetimeSquad = $"__**Lifetime Squad Stats**__\n" + $"KD: {LifetimeSquadStats.kd.value}\n" + $"Kills: {LifetimeSquadStats.kills.value}\n" + $"Kills/Game: {LifetimeSquadStats.kpg.value}\n" + $"Matches: {LifetimeSquadStats.matches.value}\n" + $"Score: {LifetimeSquadStats.score.value}\n" + $"Score/Game: {LifetimeSquadStats.scorePerMatch.value}\n" + $"Wins: {LifetimeSquadStats.top1.value}\n" + $"Top3: {LifetimeSquadStats.top3.value}\n" + $"Top6: {LifetimeSquadStats.top6.value}\n" + $"TRNRating: {LifetimeSquadStats.trnRating.value}"; var user = $"Fortnite Profile of: {fstats.epicUserHandle}\n" + $"Platform: {fstats.platformName}\n"; var recent = fstats.recentMatches.OrderByDescending(x => x.dateCollected).Select(x => $"__**Recent Matches**__\n" + $"Time: {x.dateCollected}\n" + $"Match ID: {x.id}\n" + $"Kills: {x.kills}\n" + $"Matches: {x.matches}\n" + $"Minutes Played: {x.minutesPlayed}\n" + $"Playlist: {x.playlist}\n" + $"{(x.top1 == 0 ? "" : $"Win: {x.top1}\n")}" + $"{(x.top3 == 0 ? "" : $"Top3: {x.top3}\n")}" + $"{(x.top5 == 0 ? "" : $"Top5: {x.top5}\n")}" + $"{(x.top6 == 0 ? "" : $"Top6: {x.top6}\n")}" + $"{(x.top10 == 0 ? "" : $"Top10: {x.top10}\n")}" + $"{(x.top12 == 0 ? "" : $"Top12: {x.top12}\n")}" + $"{(x.top25 == 0 ? "" : $"Top25: {x.top25}\n")}" + $"Score: {x.score}").ToList(); var pages = new List <string> { user, CurrentSolo, LifeSolo, CurrentDuo, LifetimeDuo, CurrentSquad, LifetimeSquad }; var pgpage = pages.Select(x => new PaginatedMessage.Page { description = x }); pages.AddRange(recent); var paginated = new PaginatedMessage { Pages = pgpage, Title = $"Fortnite Stats for: {fstats.epicUserHandle}", Color = Color.Blue }; await PagedReplyAsync(paginated); }
/// <summary> /// Get HTML code using server or client method /// </summary> /// <param name="url">URL to obtain HTML from</param> private string GetHtml(string url) { if (UseServerRequestType) { // Create web client and try to obatin HTML using it WebClient client = new WebClient(); try { StreamReader reader = StreamReader.New(client.OpenRead(url)); return reader.ReadToEnd(); } catch (Exception e) { lblError.Text = String.Format(ResHelper.GetString("validation.exception"), e.Message); lblError.Visible = true; return null; } } else { // Get HTML stored using javascript return ValidationHelper.Base64Decode(hdnHTML.Value); } }
private void PopulateAllianceDatabaseFromEveApi(object stateInfo) { var methodName = "GenerateAllianceDB"; LogTrace(methodName); //Core.StealthBot.Logging.LogMessage(Core.StealthBot.AllianceDB, new LogEventArgs(LogSeverityTypes.Debug, // "GenerateAllianceDB", "Starting the download of the alliance database.")); using (var downloadClient = new WebClient()) { downloadClient.Proxy = null; try { using (var downloadStream = downloadClient.OpenRead("http://api.eve-online.com/eve/AllianceList.xml.aspx")) using (var downloadStreamReader = new StreamReader(downloadStream)) { var xDoc = new XmlDocument(); try { xDoc.Load(downloadStreamReader); } catch (XmlException xe) { LogException(xe, methodName, "Caught exception while parsing Alliance API response:"); return; } var xNodeList = xDoc.SelectNodes("/eveapi/result/rowset/row"); foreach (XmlNode xNode in xNodeList) { var tempCachedAlliance = new CachedAlliance(); tempCachedAlliance.AllianceId = Convert.ToInt32(xNode.Attributes["allianceID"].Value); tempCachedAlliance.Name = xNode.Attributes["name"].Value; tempCachedAlliance.Ticker = xNode.Attributes["shortName"].Value; if (!_cachedAlliances.Contains(tempCachedAlliance)) { _cachedAlliances.Add(tempCachedAlliance); } if (!_cachedAlliancesById.ContainsKey(tempCachedAlliance.AllianceId)) { _cachedAlliancesById.Add(tempCachedAlliance.AllianceId, tempCachedAlliance); } } IsDatabaseReady = true; IsInitialized = true; IsDatabaseBuilding = false; } } catch (WebException ex) { //Check for server unavailable -- eveonline.com is down if (ex.Message == "The remote server returned an error: (503) Server Unavailable." || ex.Message == "Remote server returned: (503) The server is not available." || ex.Message.Contains("The server committed a protocol violation.")) { return; } IsDatabaseReady = false; IsInitialized = true; IsDatabaseBuilding = false; throw; } } //Core.StealthBot.Logging.LogMessage(Core.StealthBot.AllianceDB, new LogEventArgs(LogSeverityTypes.Debug, // "GenerateAllianceDB", "Done downloading alliance database.")); }
private static byte[] getUrlBinary(string url) { WebClient client = new WebClient(); client.Headers["Accept-Language"] = "en"; Stream datastream = client.OpenRead(url); MemoryStream bin = new MemoryStream(); datastream.CopyTo(bin); return bin.ToArray(); }
public override void Bad() { string data; if (IO.STATIC_READONLY_FIVE == 5) { data = ""; /* Initialize data */ /* read input from WebClient */ { try { using (WebClient client = new WebClient()) { using (StreamReader sr = new StreamReader(client.OpenRead("http://www.example.org/"))) { /* POTENTIAL FLAW: Read data from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ data = sr.ReadLine(); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } } } else { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = null; } if (IO.STATIC_READONLY_FIVE == 5) { string xmlFile = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { /* running on Windows */ xmlFile = "..\\..\\CWE643_Xpath_Injection__Helper.xml"; } else { /* running on non-Windows */ xmlFile = "../../CWE643_Xpath_Injection__Helper.xml"; } if (data != null) { /* assume username||password as source */ string[] tokens = data.Split("||".ToCharArray()); if (tokens.Length < 2) { return; } string username = tokens[0]; string password = tokens[1]; /* build xpath */ XPathDocument inputXml = new XPathDocument(xmlFile); XPathNavigator xPath = inputXml.CreateNavigator(); /* INCIDENTAL: CWE180 Incorrect Behavior Order: Validate Before Canonicalize * The user input should be canonicalized before validation. */ /* POTENTIAL FLAW: user input is used without validate */ string query = "//users/user[name/text()='" + username + "' and pass/text()='" + password + "']" + "/secret/text()"; string secret = (string)xPath.Evaluate(query); } } }
public static string GetOriginalLink(string url) { using (WebClient client = new WebClient()) { using (Stream stream = client.OpenRead(url)) { using (StreamReader reader = new StreamReader(stream)) { string content = reader.ReadToEnd(); int startindex = content.IndexOf("RD_PARM1") ; if ( startindex > 0) { content = content.Substring(startindex + 9); int endIndex = content.IndexOf("&"); return content.Substring(0, endIndex); } } } } return null; }
/// <summary> /// open a readable stream from the supplied url /// </summary> /// <param name="address">url</param> /// <returns>readable stream</returns> public Stream OpenRead(Uri address) { return(_webClient.OpenRead(address)); }
public AdressLocation geoCode() { //to Read the Stream StreamReader sr = null; AdressLocation loc = new AdressLocation(); //The Google Maps API Either return JSON or XML. We are using XML Here //Saving the url of the Google API string url = String.Format("http://maps.googleapis.com/maps/api/geocode/xml?address=" + "Marathahalli,Bangalore" + "&sensor=false"); //to Send the request to Web Client WebClient wc = new WebClient(); try { sr = new StreamReader(wc.OpenRead(url)); } catch (Exception ex) { throw new Exception("The Error Occured" + ex.Message); } try { XmlTextReader xmlReader = new XmlTextReader(sr); bool latread = false; bool longread = false; while (xmlReader.Read()) { xmlReader.MoveToElement(); switch (xmlReader.Name) { case "lat": if (!latread) { xmlReader.Read(); loc.Latitude = xmlReader.Value.ToString(); latread = true; } break; case "lng": if (!longread) { xmlReader.Read(); loc.Longitude = xmlReader.Value.ToString(); longread = true; } break; } } } catch (Exception ex) { throw new Exception("An Error Occured" + ex.Message); } return(loc); }
public static string SendSMSNEW(string API, string mobile) { try { WebClient Client = new WebClient(); string baseurl = API; Stream data = Client.OpenRead(baseurl); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); data.Close(); reader.Close(); insertDATA(s, mobile); return s; } catch (Exception ex) { insertDATA(ex.Message.ToString(), mobile); return ex.Message.ToString(); } }
public static object InvokeWebService(string url, string classname, string methodname, object[] args) { string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling"; try { if ((classname == null) || (classname == "")) { classname = WebServiceHelper.GetWsClassName(url); } //获取WSDL WebClient wc = new WebClient(); Stream stream = wc.OpenRead(url + "?WSDL"); ServiceDescription sd = ServiceDescription.Read(stream); ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider csc = new CSharpCodeProvider(); ICodeCompiler icc = csc.CreateCompiler(); //设定编译参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false; cplist.GenerateInMemory = true; cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); if (true == cr.Errors.HasErrors) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //生成代理实例,并调用方法 System.Reflection.Assembly assembly = cr.CompiledAssembly; Type t = assembly.GetType(@namespace + "." + classname, true, true); object obj = Activator.CreateInstance(t); System.Reflection.MethodInfo mi = t.GetMethod(methodname); return(mi.Invoke(obj, args)); } catch (Exception ex) { throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); } }
public static string GetHtmlFrom(string url) { var wc = new WebClient(); var resStream = wc.OpenRead(url); if (resStream != null) { var sr = new StreamReader(resStream, System.Text.Encoding.Default); return sr.ReadToEnd(); } return "null"; }
/// <summary> /// 获取用户信息 /// </summary> private bool login() { string errorUrlUser = @"/CardCenter/ErrPageUser.aspx?ErrorCode="; string errorUrlCompany = @"/CardCenter/ErrPageCompany.aspx?ErrorCode="; string ErrPageCode = ""; string companyErr = string.Empty; string userErr = string.Empty; if (!this.Page.IsPostBack) { try { string strAppId = "CardPrinting"; if (string.IsNullOrEmpty(Request.QueryString["token"])) { Response.Redirect(errorUrlUser + Server.UrlEncode("没有url中的requestToken!"), false); // Response.Redirect("ErrPage.html"); return(false);; } string requestToken = Request.QueryString["token"].ToString(); WebClient web = new WebClient(); // string requestTokenAddress= string requestTokenAddress = System.Web.Configuration.WebConfigurationManager.AppSettings["requestTokenAddress"].ToString(); requestTokenAddress = requestTokenAddress + requestToken + @"/" + strAppId; ErrPageCode = "正在换取" + requestTokenAddress; //通过远程HTTP方式获取实际AccessToken Stream getAccessToken = web.OpenRead(requestTokenAddress); StreamReader reader = new StreamReader(getAccessToken); string strAccessToken = reader.ReadToEnd(); getAccessToken.Close(); reader.Dispose(); web.Dispose(); if (string.IsNullOrEmpty(strAccessToken)) { Response.Redirect(errorUrlUser + Server.UrlEncode("获取AccessToken失败!"), false); return(false); } else { //转换为AccessToken对像 AccessToken objAccessToken = new AccessToken(); try { JsonSerializer serializer = new JsonSerializer(); StringReader strReaderAccessToken = new StringReader(strAccessToken); objAccessToken = serializer.Deserialize <AccessToken>(new JsonTextReader(strReaderAccessToken)); } catch (Exception ex) { Response.Redirect(errorUrlUser + Server.UrlEncode("转换AccessToken对像失败!" + ex.ToString()), false); return(false); } if (objAccessToken == null || objAccessToken.access_token == null || objAccessToken.access_token == "") { Response.Redirect(errorUrlUser + Server.UrlEncode("获取objAccessToken.access_token失败!"), false); return(false); } //CompanyInfoEntity obj = new CompanyInfoEntity() { TEL_CO = "", CONTAC_CO = "", TRADE_CO = "", COP_GB_CODE = "", CUSTOMS_CODE = "", FULL_NAME = "" }; Session["CompanyAllData"] = null; ErrPageCode = "正在获取企业信息"; //获取公司信息 companyErr = GetCompany(objAccessToken); if (companyErr != "") { ErrPageCode = ErrPageCode + companyErr; Response.Redirect(errorUrlCompany + companyErr, false); return(false); } Session["UserInfoEntity"] = null; ErrPageCode = "正在获取用户信息"; //获取用户 userErr = GetUserInfo(objAccessToken); if (userErr != "") { ErrPageCode = ErrPageCode + userErr; Response.Redirect(errorUrlUser + userErr, false); return(false); } } } catch (Exception ex) { string url = string.Empty; if (!userErr.Trim().Equals("")) { url = errorUrlUser; } else { url = errorUrlCompany; } Response.Redirect(url + ex.Message.ToString() + "|" + ErrPageCode, false); return(false); } } return(true); }
//public String Proxy { get; set; } //public String UserAgent { get; set; } //public bool CEExternalRead { get; set; } //public bool AllRes { get; set; } //public bool SkipeAliveCheck { get; set; } static public Byte[] Gethtml(String URI, int range, String UA, bool CRReplace, String LastMod = null) { URI = URI.Replace("2ch.net", "5ch.net"); if (ViewModel.Setting.CEExternalRead) { return HTMLTranceOutRegex(URI, range, UA, LastMod); } using (WebClient get = new WebClient()) { get.Headers["User-Agent"] = ViewModel.Setting.UserAgent4; try { if (ViewModel.Setting.ProxyAddress != "") get.Proxy = new WebProxy(ViewModel.Setting.ProxyAddress); using (System.IO.StreamReader html = new System.IO.StreamReader(get.OpenRead(URI), Encoding.GetEncoding("Shift_JIS"))) { String title = "もうずっと人大杉", ketu = ""; //dat構築用StringBuilder var Builddat = new StringBuilder(510 * 1024); bool alive = true, NewCGI = false; //タイトルの検索 for (String line = html.ReadLine(); !html.EndOfStream; line = html.ReadLine()) { if (Regex.IsMatch(line, @"<title>(.+?)<\/title>")) { title = Regex.Match(line, @"<title>(.+?)<\/title>").Groups[1].Value; break; } else if (Regex.IsMatch(line, @"<title>(.+?)$")) { title = Regex.Match(line, @"<title>(.+?)$").Groups[1].Value; NewCGI = true; break; } } if (Regex.IsMatch(title, @"(5ちゃんねる error \d+|もうずっと人大杉|datが存在しません.削除されたかURL間違ってますよ)")) return new byte[] { 0 }; if (Regex.IsMatch(title, @"(2|5)ch\.net\s(\[\d+\])")) { var tmatch = Regex.Match(title, @"(2|5)ch\.net\s(\[\d+\])").Groups; title = title.Replace(tmatch[0].Value, $"{tmatch[1].Value}ch.net\t {tmatch[2].Value}"); } if (CRReplace) title = title.Replace("©", "©"); //新CGI形式と古いCGI形式で処理を分ける if (NewCGI) { String line = html.ReadLine(); //スレッド本文探索 do { if (Regex.IsMatch(line, @"<d(?:iv|l) class=.(?:thread|post).+?>")) break; line = html.ReadLine(); } while (!html.EndOfStream); //スレ生存チェック if (!ViewModel.Setting.SkipAliveCheck) { if (Regex.IsMatch(line, @"<div class=" + '"' + @"[a-zA-Z\s]+?" + '"' + @">(.+?過去ログ倉庫.+?|レス数が\d{3,}を超えています.+?(書き込み.*?|表.?示)でき.+?)</div>") == false) { return new byte[] { 0, 0 }; } } var Bres = new StringBuilder(5 * 1024); //pinkレスずれ処理用 bool pink = URI.Contains("bbspink.com"); int datResnumber = 1, htmlResnumber = 0; long ThreadTime = long.Parse(Regex.Match(URI, @"/(\d{9,})").Groups[1].Value); var ResMatches = Regex.Matches(line, @"<(?:div|dl) class=.post. id=.\d.+?>(.+?(?:</div></div>|</dd></dl>))"); foreach (Match Res in ResMatches) { //Match date = Regex.Match(Res.Groups[1].Value, @"<(?:div|span) class=.date.+?>(.+?)</(?:div|span)>(?:<(?:div|span) class=.be\s.+?.>(.+?)</(?:div|span)>)?"); Match date = Regex.Match(Res.Groups[1].Value, @"<(?:div|span) class=.date.+?>(.+?(?:</span><span class=" + '"' + @"\w+?" + '"' + @">.*?)?)</(?:div|span)>(?:<(?:div|span) class=.be\s.+?.>(.+?)</(?:div|span)>)?"); String number = Regex.Match(Res.Groups[1].Value, @"<(?:div|span) class=.number.+?>(\d{1,5})(?: : )?</(?:div|span)>").Groups[1].Value; //0,NGの検出 if (number == "0" && date.Groups[1].Value == "NG") { //飛ばす continue; } //htmlでレスが飛んでいるときを検出 if (pink && int.TryParse(number, out htmlResnumber) && datResnumber < htmlResnumber) { for (int j = htmlResnumber - datResnumber; j > 0; --j) { Builddat.Append("うふ~ん<>うふ~ん<>うふ~ん ID:DELETED<>うふ~ん<>うふ~ん<>\n"); } datResnumber = htmlResnumber; } //String name = Regex.Match(Res.Groups[1].Value, $"<(?:div|span) class={'"'}name{'"'}>((?:{'"'}.*?{'"'}|'.*?'|[^'{'"'}])+?)</(?:div|span)>").Groups[1].Value; String name = Regex.Match(Res.Groups[1].Value, $"<(?:div|span) class=.name.+?>(.+?(?:</b>|</a>))</(?:div|span)>").Groups[1].Value; //目欄が空の時フォントカラー指定を消す if (!name.Contains("<a href=" + '"' + "mailto:")) { name = Regex.Replace(name, @"<font color=.green.>", ""); name = name.Replace("</font>", ""); } //ID部のspanタグ削除 String dateid = date.Groups[1].Value; if (dateid.Contains("</span><span ")) { dateid = Regex.Replace(dateid, $"</span><span class={'"'}" + @"\w+?" + $"{'"'}>", " "); } //日付IDがNGになっているとき if (dateid.Contains("NG NG")) { DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); UnixEpoch = UnixEpoch.AddSeconds(ThreadTime); String time = UnixEpoch.ToLocalTime().ToString("yyyy/MM/dd(ddd) HH:mm:ss.00"); dateid = time + " ID:NG0"; } //beリンク処理 String be = ""; if (!string.IsNullOrEmpty(date.Groups[2].Value)) { var mb = Regex.Match(date.Groups[2].Value, @"<a href.+?(\d{2,}).+?>(.+)$"); be = $" <a href={'"'}javascript:be({mb.Groups[1].Value});{'"'}>{mb.Groups[2].Value}"; } String message = Regex.Match(Res.Groups[1].Value, @"<d(?:iv|d) class=.(?:message|thread_in).+?>(?:<span class=.escaped.>)?(.+?)(?:</span>)?(?:</div></div>|</dd></dl>)").Groups[1].Value; //安価のリンク修正、http://potato.2ch.net/test/read.cgi/jisaku/1447271149/9→../test/read.cgi/jisaku/1447271149/9 Bres.Append(message); foreach (Match item in Regex.Matches(message, @"(<a href=.)(?:https?:)?//\w+\.((?:2|5)ch\.net|bbspink\.com)(/test/read.cgi/\w+/\d+/\d{1,4}.\s.+?>>>\d{1,5}</a>)")) { Bres.Replace(item.Groups[0].Value, item.Groups[1].Value + ".." + item.Groups[3].Value); } //お絵かきリンク修正 foreach (Match item in Regex.Matches(message, $@"<a\s(?:class={'"'}image{'"'}\s)?href=" + '"' + @"(?:https?:)?//jump.(?:2|5)ch\.net/\?(https?://[a-zA-Z\d]+?\.8ch.net\/.+?\.\w+?)" + '"' + @">https?://[a-zA-Z\d]+?\.8ch\.net\/.+?\.\w+?</a>")) { Bres.Replace(item.Groups[0].Value, "<img src=" + '"' + item.Groups[1].Value + '"' + ">"); } //p53など、レス前後にスペースが無いときに補う。 if (!Regex.IsMatch(message, @"^\s.+\s$")) { Bres.Insert(0, " "); Bres.Append(" "); } Bres.Insert(0, ":" + dateid + be + "<dd>"); Bres.Insert(0, "<dt>" + number + " :" + name); Bres.Append("<br><br>"); Builddat.Append(html2dat(Bres.ToString())); if (!String.IsNullOrEmpty(title)) { Builddat.Append(title + "\n"); title = ""; } else Builddat.Append("\n"); Bres.Clear(); datResnumber++; } ketu = Regex.Match(line, @"<(?:div|li) class=.+?>(?<datsize>\d+?)KB</(?:div|li)>").Groups[1].Value; } else { if (!ViewModel.Setting.SkipAliveCheck) { //dat落ちかチェック for (String line = html.ReadLine(); !html.EndOfStream; line = html.ReadLine()) { if (Regex.IsMatch(line, @"<div.*?>(.+?過去ログ倉庫.+?|レス数が\d{3,}を超えています.+?(書き込み.*?でき|表示しません).+?)</div>")) { alive = false; break; } else if (Regex.IsMatch(line, @"<h1 style.+>.+?<\/h1>")) { alive = true; break; } } //生きているなら終了 if (alive) return new byte[] { 0, 0 }; } String ResHtml = html.ReadToEnd(); System.Collections.Concurrent.ConcurrentDictionary<int, string> Trancedat = new System.Collections.Concurrent.ConcurrentDictionary<int, string>(4, 1005); System.Threading.Tasks.ParallelOptions option = new System.Threading.Tasks.ParallelOptions(); option.MaxDegreeOfParallelism = 4; System.Threading.Tasks.Parallel.ForEach<Match>(Regex.Matches(ResHtml, @"<dt>(\d{1,4})\s:.+?<br><br>(?:\r|\n)").Cast<Match>(), option, match => { Trancedat[int.Parse(match.Groups[1].Value) - 1] = html2dat(match.Groups[0].Value) + "\n"; }); Builddat.Append(Trancedat[0].Substring(0, Trancedat[0].Length - 1) + title + "\n"); for (int i = 1; i < Trancedat.Count; ++i) Builddat.Append(Trancedat[i]); if (!ViewModel.Setting.AllReturn || range > -1) ketu = Regex.Match(ResHtml, @"<font\scolor.+?><b>(\d+)\sKB<\/b><\/font>").Groups[1].Value; } //if (ViewModel.Setting.Replace5chURI || ViewModel.Setting.ReplaceHttpsLink) //{ // Builddat = new StringBuilder(HTMLtoDat.ResContentReplace(Builddat.ToString())); //} //Byte[] Bdat = Encoding.GetEncoding("Shift_JIS").GetBytes(Builddat.ToString()); Byte[] Bdat = Encoding.GetEncoding("Shift_JIS").GetBytes((ViewModel.Setting.Replace5chURI || ViewModel.Setting.ReplaceHttpsLink) ? (HTMLtoDat.ResContentReplace(Builddat.ToString())) : (Builddat.ToString())); if (ViewModel.Setting.AllReturn || range < 0) return Bdat; int size; try { size = int.Parse(ketu); } catch (FormatException) { size = 0; } //差分返答処理 return DifferenceDetection(Bdat, LastMod, UA, range, size); } } catch (System.Threading.ThreadAbortException e) { throw e; } catch (Exception err) { ViewModel.OnModelNotice(URI + "をHTMLから変換中にエラーが発生しました。\n" + err.ToString()); return new byte[] { 0 }; } } }
public static void ValidateSatyamImageNetClassificationAggregationResultByGUID(string guid, string confusingImageListFilePath = null, bool prepareDataForTraining = false, string outputDirectory = null) { Dictionary <string, ConfusingReason> imageBlackListReason = new Dictionary <string, ConfusingReason>(); if (confusingImageListFilePath != null) { imageBlackListReason = getConfusingImageList(confusingImageListFilePath); } //get all aggregated results SatyamAggregatedResultsTableAccess resultsDB = new SatyamAggregatedResultsTableAccess(); List <SatyamAggregatedResultsTableEntry> results = resultsDB.getEntriesByGUID(guid); resultsDB.close(); int noTotal = 0; int noCorrect = 0; SortedDictionary <string, Dictionary <string, int> > confusionMatrix_res_groundtruth = new SortedDictionary <string, Dictionary <string, int> >(); SortedDictionary <string, Dictionary <string, int> > confusionMatrix_groundtruth_res = new SortedDictionary <string, Dictionary <string, int> >(); StringBuilder s = new StringBuilder(); s.Append("<!DOCTYPE html>\n"); s.Append("<html>\n"); s.Append("<body>\n"); String title = String.Format("<h1>Job GUID {0} Incorrect Result Summary</h1>\n", guid); s.Append(title); WebClient wc = new WebClient(); for (int i = 0; i < results.Count; i++) { SatyamSaveAggregatedDataSatyam data = new SatyamSaveAggregatedDataSatyam(results[i]); //String uri = data.SatyamURI; //string[] uri_parts= uri.Split('/'); //string[] name_parts = uri_parts[uri_parts.Length - 1].Split('_'); string fileName = URIUtilities.filenameFromURI(data.SatyamURI); string imageCategoryName = fileName.Split('_')[0]; String resultString = data.AggregatedResultString; SingleObjectLabelingAggregatedResult result = JSonUtils.ConvertJSonToObject <SingleObjectLabelingAggregatedResult>(resultString); // skip all ambulances and black listed if (IsBlackListed(data.SatyamURI, imageBlackListReason)) { continue; } if (!confusionMatrix_res_groundtruth.ContainsKey(result.Category)) { confusionMatrix_res_groundtruth.Add(result.Category, new Dictionary <string, int>() { { GroundTruth[imageCategoryName], 0 } }); } else { if (!confusionMatrix_res_groundtruth[result.Category].ContainsKey(GroundTruth[imageCategoryName])) { confusionMatrix_res_groundtruth[result.Category].Add(GroundTruth[imageCategoryName], 0); } } if (!confusionMatrix_groundtruth_res.ContainsKey(GroundTruth[imageCategoryName])) { confusionMatrix_groundtruth_res.Add(GroundTruth[imageCategoryName], new Dictionary <string, int>() { { result.Category, 0 } }); } else { if (!confusionMatrix_groundtruth_res[GroundTruth[imageCategoryName]].ContainsKey(result.Category)) { confusionMatrix_groundtruth_res[GroundTruth[imageCategoryName]].Add(result.Category, 0); } } if (result.Category.Equals(GroundTruth[imageCategoryName], StringComparison.InvariantCultureIgnoreCase)) { noCorrect++; } else { //Console.WriteLine("{0}, Groundtruth: {1}, Aggregated: {2}, Votes: {3}", // fileName, GroundTruth[imageCategoryName], result.Category, // JSonUtils.ConvertObjectToJSon(result.metaData)); String record = String.Format("<p>{0}, Groundtruth: {1}, Aggregated: {2}, Votes: {3}</p>\n", fileName, GroundTruth[imageCategoryName], result.Category, JSonUtils.ConvertObjectToJSon(result.metaData)); String img = String.Format("<img src=\"{0}\" >\n", data.SatyamURI); s.Append(record); s.Append(img); } // prepare training dataset if (prepareDataForTraining) { if (GroundTruth[imageCategoryName] != "ambulance" && result.Category != "ambulance") { Image im = Image.FromStream(wc.OpenRead(data.SatyamURI)); if (!Directory.Exists(outputDirectory + result.Category)) { Directory.CreateDirectory(outputDirectory + result.Category); } im.Save(outputDirectory + "\\" + result.Category + "\\" + fileName); } } noTotal++; confusionMatrix_res_groundtruth[result.Category][GroundTruth[imageCategoryName]]++; confusionMatrix_groundtruth_res[GroundTruth[imageCategoryName]][result.Category]++; } Console.WriteLine("Result: {0}/{1}, precision: {2}", noCorrect, noTotal, (double)noCorrect / noTotal); // write the confusion matrix s.Append("<p>"); String row = "\t\t"; foreach (string resultCategory in confusionMatrix_res_groundtruth.Keys) { row += resultCategory + "\t"; } row += "<br>\n"; s.Append(row); Console.WriteLine(row); foreach (string groundTruthCategory in confusionMatrix_groundtruth_res.Keys) { row = groundTruthCategory + "\t"; foreach (string resultCategory in confusionMatrix_res_groundtruth.Keys) { if (confusionMatrix_groundtruth_res[groundTruthCategory].ContainsKey(resultCategory)) { row += confusionMatrix_groundtruth_res[groundTruthCategory][resultCategory].ToString(); } else { row += "0"; } row += "\t"; } row += "<br>\n"; s.Append(row); Console.WriteLine(row); } s.Append("</p>\n"); s.Append("</body>\n"); s.Append("</html>\n"); string dataToBeSaved = s.ToString(); SatyamJobStorageAccountAccess storage = new SatyamJobStorageAccountAccess(); string FileName = "AggregatedIncorrectResults-" + guid + ".html"; storage.SaveATextFile("singleobjectlabeling", guid, FileName, dataToBeSaved); }
public override void Bad() { double data; data = double.MinValue; /* Initialize data */ /* read input from WebClient */ { WebClient client = new WebClient(); StreamReader sr = null; try { sr = new StreamReader(client.OpenRead("http://www.example.org/")); /* FLAW: Read data from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ string stringNumber = sr.ReadLine(); if (stringNumber != null) // avoid NPD incidental warnings { try { data = double.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing data from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } finally { /* clean up stream reading objects */ try { if (sr != null) { sr.Close(); } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error closing StreamReader"); } } } /* serialize data to a byte array */ byte[] dataSerialized = null; try { BinaryFormatter bf = new BinaryFormatter(); using (var ms = new MemoryStream()) { bf.Serialize(ms, data); dataSerialized = ms.ToArray(); } CWE197_Numeric_Truncation_Error__double_NetClient_to_short_75b.BadSink(dataSerialized); } catch (SerializationException exceptSerialize) { IO.Logger.Log(NLog.LogLevel.Warn, "Serialization exception in serialization", exceptSerialize); } }
//appID wx80b8ea65e4f18e65 //appsecret b3c748800db56e4019814109a79dc06b /// <summary> /// 获取AccessToken /// </summary> public static string WXGetAccessToken(object[] infoValue, bool useCache) { if (useCache) { TimeSpan ts = DateTime.Now - TmoShare.WX_ACCESS_TOKEN_TIME; if (ts.TotalSeconds <= 3600) { return(TmoShare.WX_ACCESS_TOKEN); } } string strRecode = ""; try { string appID = TmoShare.WX_APP_ID; // "wx80b8ea65e4f18e65"; string appSecret = TmoShare.WX_APP_SECRET; //"b3c748800db56e4019814109a79dc06b"; string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appSecret + ""; #region 特别注意 //{"errcode":45009,"errmsg":"api freq out of limit"} //接口调用频率限制如下: //接口 每日限额 //获取access_token 2000 #endregion if (infoValue[0].ToString() != "") { using (WebClient client = new WebClient()) { try { Stream data = client.OpenRead(url); using (StreamReader reader = new StreamReader(data, Encoding.Default)) { string resultData = reader.ReadToEnd(); resultData = resultData.Trim(); data.Close(); reader.Close(); switch (resultData) { case "": strRecode = "err_null"; break; case "45009": TmoShare.WriteLog("调用WXGetAccessToken当天过于频繁! 详细信息:err_wx_45009"); strRecode = "err_wx_45009"; break; case "40013": TmoShare.WriteLog("调用WXGetAccessToken当天过于频繁! 详细信息:err_wx_45009"); strRecode = "err_wx_45009"; break; //{"errcode":40013,"errmsg":"invalid appid"} default: Dictionary <string, TmoCommon.JsonHelper.JsonNode> jsNodes = TmoCommon.JsonHelper.GetJsonValues(resultData); if (jsNodes != null || jsNodes.Count > 0) { if (jsNodes.ContainsKey("access_token")) { TmoShare.WX_ACCESS_TOKEN = jsNodes["access_token"].Value; //string expires_in = jsNodes["expires_in"].Value; TmoShare.WX_ACCESS_TOKEN_TIME = DateTime.Now; } else if (jsNodes.ContainsKey("errcode")) { //{"errcode":40013,"errmsg":"invalid appid"} strRecode = "err_" + jsNodes["errcode"].Value + "_" + jsNodes["errmsg"].Value; } else { TmoShare.WX_ACCESS_TOKEN = ""; //string expires_in = jsNodes["expires_in"].Value; TmoShare.WX_ACCESS_TOKEN_TIME = DateTime.Now; } } else { strRecode = "err_json_converter"; } break; } } } catch (Exception ex) { TmoShare.WriteLog("调用WXGetAccessToken接口发生未知错误! 详细信息:" + ex.Message); strRecode = "err_002"; } } } else { strRecode = "err_wx_003"; TmoShare.WriteLog("调用WXGetAccessToken未传入医生编码!"); } } catch (Exception e) { strRecode = "err_wx_001"; TmoShare.WriteLog("当前医生" + infoValue[0].ToString() + "获取列表失败! 原因:err_cdp_001(未知异常失败)" + e.Message.ToString()); } strRecode = TmoShare.WX_ACCESS_TOKEN; return(strRecode); }
static void Main(string[] args) { WebClient client = new WebClient(); var url = "http://www.intesolv.com/triangle.txt"; //string downloadString = client.DownloadString("http://www.intesolv.com/triangle.txt"); int result = 0; int current = 0; int next = 0; int tracker = 0; string test = ""; //grab web page using (var stream = client.OpenRead(url)) using (var reader = new StreamReader(stream)) { string line; //read line by line while ((line = reader.ReadLine()) != null) { //evaluate line by highest integer for (int x = 0; x < line.Length; x++) { for (int y = x; y < line.Length; y++) { if (line[y] == ' ' || line[y] == '\n') { /*if (y == line.Length - 1) * { * //test = line.Substring(x); * test = line.Substring(x, line.Length - 1); * next = Int32.Parse(test); * } * else * {*/ test = line.Substring(x, y - x); next = Int32.Parse(test); //} if (current < next) { current = next; } if (line[y] == '\n') { break; } x = y; x++; } } } //add integer to sum result += current; //proceed next line current = 0; next = 0; } } //present largest Console.WriteLine(result); }
public static async Task SendWebClientRequests(bool tracingDisabled, string url, string requestContent) { Console.WriteLine($"[WebClient] sending requests to {url}"); using (var webClient = new WebClient()) { webClient.Encoding = Utf8; if (tracingDisabled) { webClient.Headers.Add(HttpHeaderNames.TracingEnabled, "false"); } using (Tracer.Instance.StartActive("WebClient")) { using (Tracer.Instance.StartActive("DownloadData")) { webClient.DownloadData(GetUrlForTest("DownloadData", url)); Console.WriteLine("Received response for client.DownloadData(String)"); webClient.DownloadData(new Uri(GetUrlForTest("DownloadData2", url))); Console.WriteLine("Received response for client.DownloadData(Uri)"); } using (Tracer.Instance.StartActive("DownloadDataAsync")) { webClient.DownloadDataAsyncAndWait(new Uri(GetUrlForTest("DownloadDataAsync", url))); Console.WriteLine("Received response for client.DownloadDataAsync(Uri)"); webClient.DownloadDataAsyncAndWait(new Uri(GetUrlForTest("DownloadDataAsync2", url)), null); Console.WriteLine("Received response for client.DownloadDataAsync(Uri, Object)"); } using (Tracer.Instance.StartActive("DownloadDataTaskAsync")) { await webClient.DownloadDataTaskAsync(GetUrlForTest("DownloadDataTaskAsync", url)); Console.WriteLine("Received response for client.DownloadDataTaskAsync(String)"); await webClient.DownloadDataTaskAsync(new Uri(GetUrlForTest("DownloadDataTaskAsync2", url))); Console.WriteLine("Received response for client.DownloadDataTaskAsync(Uri)"); } using (Tracer.Instance.StartActive("DownloadFile")) { webClient.DownloadFile(GetUrlForTest("DownloadFile", url), "DownloadFile.string.txt"); Console.WriteLine("Received response for client.DownloadFile(String, String)"); webClient.DownloadFile(new Uri(GetUrlForTest("DownloadFile2", url)), "DownloadFile.uri.txt"); Console.WriteLine("Received response for client.DownloadFile(Uri, String)"); } using (Tracer.Instance.StartActive("DownloadFileAsync")) { webClient.DownloadFileAsyncAndWait(new Uri(GetUrlForTest("DownloadFileAsync", url)), "DownloadFileAsync.uri.txt"); Console.WriteLine("Received response for client.DownloadFileAsync(Uri, String)"); webClient.DownloadFileAsyncAndWait(new Uri(GetUrlForTest("DownloadFileAsync2", url)), "DownloadFileAsync.uri_token.txt", null); Console.WriteLine("Received response for client.DownloadFileAsync(Uri, String, Object)"); } using (Tracer.Instance.StartActive("DownloadFileTaskAsync")) { await webClient.DownloadFileTaskAsync(GetUrlForTest("DownloadFileTaskAsync", url), "DownloadFileTaskAsync.string.txt"); Console.WriteLine("Received response for client.DownloadFileTaskAsync(String, String)"); await webClient.DownloadFileTaskAsync(new Uri(GetUrlForTest("DownloadFileTaskAsync2", url)), "DownloadFileTaskAsync.uri.txt"); Console.WriteLine("Received response for client.DownloadFileTaskAsync(Uri, String)"); } using (Tracer.Instance.StartActive("DownloadString")) { webClient.DownloadString(GetUrlForTest("DownloadString", url)); Console.WriteLine("Received response for client.DownloadString(String)"); webClient.DownloadString(new Uri(GetUrlForTest("DownloadString2", url))); Console.WriteLine("Received response for client.DownloadString(Uri)"); } using (Tracer.Instance.StartActive("DownloadStringAsync")) { webClient.DownloadStringAsyncAndWait(new Uri(GetUrlForTest("DownloadStringAsync", url))); Console.WriteLine("Received response for client.DownloadStringAsync(Uri)"); webClient.DownloadStringAsyncAndWait(new Uri(GetUrlForTest("DownloadStringAsync2", url)), null); Console.WriteLine("Received response for client.DownloadStringAsync(Uri, Object)"); } using (Tracer.Instance.StartActive("DownloadStringTaskAsync")) { await webClient.DownloadStringTaskAsync(GetUrlForTest("DownloadStringTaskAsync", url)); Console.WriteLine("Received response for client.DownloadStringTaskAsync(String)"); await webClient.DownloadStringTaskAsync(new Uri(GetUrlForTest("DownloadStringTaskAsync2", url))); Console.WriteLine("Received response for client.DownloadStringTaskAsync(Uri)"); } using (Tracer.Instance.StartActive("OpenRead")) { webClient.OpenRead(GetUrlForTest("OpenRead", url)).Close(); Console.WriteLine("Received response for client.OpenRead(String)"); webClient.OpenRead(new Uri(GetUrlForTest("OpenRead2", url))).Close(); Console.WriteLine("Received response for client.OpenRead(Uri)"); } using (Tracer.Instance.StartActive("OpenReadAsync")) { webClient.OpenReadAsyncAndWait(new Uri(GetUrlForTest("OpenReadAsync", url))); Console.WriteLine("Received response for client.OpenReadAsync(Uri)"); webClient.OpenReadAsyncAndWait(new Uri(GetUrlForTest("OpenReadAsync2", url)), null); Console.WriteLine("Received response for client.OpenReadAsync(Uri, Object)"); } using (Tracer.Instance.StartActive("OpenReadTaskAsync")) { using Stream readStream1 = await webClient.OpenReadTaskAsync(GetUrlForTest ("OpenReadTaskAsync", url)); Console.WriteLine("Received response for client.OpenReadTaskAsync(String)"); using Stream readStream2 = await webClient.OpenReadTaskAsync(new Uri (GetUrlForTest("OpenReadTaskAsync2", url))); Console.WriteLine("Received response for client.OpenReadTaskAsync(Uri)"); } using (Tracer.Instance.StartActive("UploadData")) { webClient.UploadData(GetUrlForTest("UploadData", url), new byte[0]); Console.WriteLine("Received response for client.UploadData(String, Byte[])"); webClient.UploadData(new Uri(GetUrlForTest("UploadData2", url)), new byte[0]); Console.WriteLine("Received response for client.UploadData(Uri, Byte[])"); webClient.UploadData(GetUrlForTest("UploadData3", url), "POST", new byte[0]); Console.WriteLine("Received response for client.UploadData(String, String, Byte[])"); webClient.UploadData(new Uri(GetUrlForTest("UploadData4", url)), "POST", new byte[0]); Console.WriteLine("Received response for client.UploadData(Uri, String, Byte[])"); } using (Tracer.Instance.StartActive("UploadDataAsync")) { webClient.UploadDataAsyncAndWait(new Uri(GetUrlForTest("UploadDataAsync", url)), new byte[0]); Console.WriteLine("Received response for client.UploadDataAsync(Uri, Byte[])"); webClient.UploadDataAsyncAndWait(new Uri(GetUrlForTest("UploadDataAsync2", url)), "POST", new byte[0]); Console.WriteLine("Received response for client.UploadDataAsync(Uri, String, Byte[])"); webClient.UploadDataAsyncAndWait(new Uri(GetUrlForTest("UploadDataAsync3", url)), "POST", new byte[0], null); Console.WriteLine("Received response for client.UploadDataAsync(Uri, String, Byte[], Object)"); } using (Tracer.Instance.StartActive("UploadDataTaskAsync")) { await webClient.UploadDataTaskAsync(GetUrlForTest("UploadDataTaskAsync", url), new byte[0]); Console.WriteLine("Received response for client.UploadDataTaskAsync(String, Byte[])"); await webClient.UploadDataTaskAsync(new Uri(GetUrlForTest("UploadDataTaskAsync2", url)), new byte[0]); Console.WriteLine("Received response for client.UploadDataTaskAsync(Uri, Byte[])"); await webClient.UploadDataTaskAsync(GetUrlForTest("UploadDataTaskAsync3", url), "POST", new byte[0]); Console.WriteLine("Received response for client.UploadDataTaskAsync(String, String, Byte[])"); await webClient.UploadDataTaskAsync(new Uri(GetUrlForTest("UploadDataTaskAsync4", url)), "POST", new byte[0]); Console.WriteLine("Received response for client.UploadDataTaskAsync(Uri, String, Byte[])"); } File.WriteAllText("UploadFile.txt", requestContent); using (Tracer.Instance.StartActive("UploadFile")) { webClient.UploadFile(GetUrlForTest("UploadFile", url), "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFile(String, String)"); webClient.UploadFile(new Uri(GetUrlForTest("UploadFile2", url)), "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFile(Uri, String)"); webClient.UploadFile(GetUrlForTest("UploadFile3", url), "POST", "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFile(String, String, String)"); webClient.UploadFile(new Uri(GetUrlForTest("UploadFile4", url)), "POST", "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFile(Uri, String, String)"); } using (Tracer.Instance.StartActive("UploadFileAsync")) { webClient.UploadFileAsyncAndWait(new Uri(GetUrlForTest("UploadFileAsync", url)), "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFileAsync(Uri, String)"); webClient.UploadFileAsyncAndWait(new Uri(GetUrlForTest("UploadFileAsync2", url)), "POST", "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFileAsync(Uri, String, String)"); webClient.UploadFileAsyncAndWait(new Uri(GetUrlForTest("UploadFileAsync3", url)), "POST", "UploadFile.txt", null); Console.WriteLine("Received response for client.UploadFileAsync(Uri, String, String, Object)"); } using (Tracer.Instance.StartActive("UploadFileTaskAsync")) { await webClient.UploadFileTaskAsync(GetUrlForTest("UploadFileTaskAsync", url), "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFileTaskAsync(String, String)"); await webClient.UploadFileTaskAsync(new Uri(GetUrlForTest("UploadFileTaskAsync2", url)), "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFileTaskAsync(Uri, String)"); await webClient.UploadFileTaskAsync(GetUrlForTest("UploadFileTaskAsync3", url), "POST", "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFileTaskAsync(String, String, String)"); await webClient.UploadFileTaskAsync(new Uri(GetUrlForTest("UploadFileTaskAsync4", url)), "POST", "UploadFile.txt"); Console.WriteLine("Received response for client.UploadFileTaskAsync(Uri, String, String)"); } using (Tracer.Instance.StartActive("UploadString")) { webClient.UploadString(GetUrlForTest("UploadString", url), requestContent); Console.WriteLine("Received response for client.UploadString(String, String)"); webClient.UploadString(new Uri(GetUrlForTest("UploadString2", url)), requestContent); Console.WriteLine("Received response for client.UploadString(Uri, String)"); webClient.UploadString(GetUrlForTest("UploadString3", url), "POST", requestContent); Console.WriteLine("Received response for client.UploadString(String, String, String)"); webClient.UploadString(new Uri(GetUrlForTest("UploadString4", url)), "POST", requestContent); Console.WriteLine("Received response for client.UploadString(Uri, String, String)"); } using (Tracer.Instance.StartActive("UploadStringAsync")) { webClient.UploadStringAsyncAndWait(new Uri(GetUrlForTest("UploadStringAsync", url)), requestContent); Console.WriteLine("Received response for client.UploadStringAsync(Uri, String)"); webClient.UploadStringAsyncAndWait(new Uri(GetUrlForTest("UploadStringAsync2", url)), "POST", requestContent); Console.WriteLine("Received response for client.UploadStringAsync(Uri, String, String)"); webClient.UploadStringAsyncAndWait(new Uri(GetUrlForTest("UploadStringAsync3", url)), "POST", requestContent, null); Console.WriteLine("Received response for client.UploadStringAsync(Uri, String, String, Object)"); } using (Tracer.Instance.StartActive("UploadStringTaskAsync")) { await webClient.UploadStringTaskAsync(GetUrlForTest("UploadStringTaskAsync", url), requestContent); Console.WriteLine("Received response for client.UploadStringTaskAsync(String, String)"); await webClient.UploadStringTaskAsync(new Uri(GetUrlForTest("UploadStringTaskAsync2", url)), requestContent); Console.WriteLine("Received response for client.UploadStringTaskAsync(Uri, String)"); await webClient.UploadStringTaskAsync(GetUrlForTest("UploadStringTaskAsync3", url), "POST", requestContent); Console.WriteLine("Received response for client.UploadStringTaskAsync(String, String, String)"); await webClient.UploadStringTaskAsync(new Uri(GetUrlForTest("UploadStringTaskAsync4", url)), "POST", requestContent); Console.WriteLine("Received response for client.UploadStringTaskAsync(Uri, String, String)"); } var values = new NameValueCollection(); using (Tracer.Instance.StartActive("UploadValues")) { webClient.UploadValues(GetUrlForTest("UploadValues", url), values); Console.WriteLine("Received response for client.UploadValues(String, NameValueCollection)"); webClient.UploadValues(new Uri(GetUrlForTest("UploadValues2", url)), values); Console.WriteLine("Received response for client.UploadValues(Uri, NameValueCollection)"); webClient.UploadValues(GetUrlForTest("UploadValues3", url), "POST", values); Console.WriteLine("Received response for client.UploadValues(String, String, NameValueCollection)"); webClient.UploadValues(new Uri(GetUrlForTest("UploadValues4", url)), "POST", values); Console.WriteLine("Received response for client.UploadValues(Uri, String, NameValueCollection)"); } using (Tracer.Instance.StartActive("UploadValuesAsync")) { webClient.UploadValuesAsyncAndWait(new Uri(GetUrlForTest("UploadValuesAsync", url)), values); Console.WriteLine("Received response for client.UploadValuesAsync(Uri, NameValueCollection)"); webClient.UploadValuesAsyncAndWait(new Uri(GetUrlForTest("UploadValuesAsync2", url)), "POST", values); Console.WriteLine("Received response for client.UploadValuesAsync(Uri, String, NameValueCollection)"); webClient.UploadValuesAsyncAndWait(new Uri(GetUrlForTest("UploadValuesAsync3", url)), "POST", values, null); Console.WriteLine("Received response for client.UploadValuesAsync(Uri, String, NameValueCollection, Object)"); } using (Tracer.Instance.StartActive("UploadValuesTaskAsync")) { await webClient.UploadValuesTaskAsync(GetUrlForTest("UploadValuesTaskAsync", url), values); Console.WriteLine("Received response for client.UploadValuesTaskAsync(String, NameValueCollection)"); await webClient.UploadValuesTaskAsync(new Uri(GetUrlForTest("UploadValuesTaskAsync2", url)), values); Console.WriteLine("Received response for client.UploadValuesTaskAsync(Uri, NameValueCollection)"); await webClient.UploadValuesTaskAsync(GetUrlForTest("UploadValuesTaskAsync3", url), "POST", values); Console.WriteLine("Received response for client.UploadValuesTaskAsync(String, String, NameValueCollection)"); await webClient.UploadValuesTaskAsync(new Uri(GetUrlForTest("UploadValuesTaskAsync4", url)), "POST", values); Console.WriteLine("Received response for client.UploadValuesTaskAsync(Uri, String, NameValueCollection)"); } } } }
/// <summary> /// Prepare Dictionary with requests for CSS validation /// </summary> /// <param name="parameter">Asynchronous parameter containing current url data to resolve absolute URL </param> private Dictionary<string, string> GetValidationRequests(string parameter) { string html = GetHtml(Url); Dictionary<string, string> cssRequests = null; string[] urlParams = parameter.Split(';'); if (!String.IsNullOrEmpty(html)) { cssRequests = new Dictionary<string, string>(); // Get inline CSS AddLog(GetString("validation.css.preparinginline")); StringBuilder sbInline = new StringBuilder(); foreach (Match m in InlineStylesRegex.Matches(html)) { string captured = m.Groups["css"].Value; sbInline.Append(captured); sbInline.Append("\n"); } cssRequests.Add(DocumentValidationHelper.InlineCSSSource, sbInline.ToString()); // Get linked styles URLs WebClient client = new WebClient(); foreach (Match m in LinkedStylesRegex.Matches(html)) { string url = m.Groups["url"].Value; url = Server.HtmlDecode(url); string css = null; if (!String.IsNullOrEmpty(url)) { bool processCss = true; string[] excludedCsss = mExcludedCSS.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); // Check if CSS is not excluded (CMS stylesheets) foreach (string excludedCss in excludedCsss) { if (url.EndsWith(excludedCss, StringComparison.InvariantCultureIgnoreCase)) { processCss = false; break; } } if (processCss && !cssRequests.ContainsKey(url)) { AddLog(String.Format(GetString("validation.css.preparinglinkedstyles"), url)); try { // Get CSS data from URL StreamReader reader = StreamReader.New(client.OpenRead(DocumentValidationHelper.DisableMinificationOnUrl(URLHelper.GetAbsoluteUrl(url, urlParams[0], urlParams[1], urlParams[2])))); css = reader.ReadToEnd(); if (!String.IsNullOrEmpty(css)) { cssRequests.Add(url, css.Trim(new char[] { '\r', '\n' })); } } catch { } } } } } return cssRequests; }
/* uses badsource and badsink */ public override void Bad() { double data = 0.0d; switch (6) { case 6: data = double.MinValue; /* Initialize data */ /* read input from WebClient */ { WebClient client = new WebClient(); StreamReader sr = null; try { sr = new StreamReader(client.OpenRead("http://www.example.org/")); /* FLAW: Read data from a web server with WebClient */ /* This will be reading the first "line" of the response body, * which could be very long if there are no newlines in the HTML */ string stringNumber = sr.ReadLine(); if (stringNumber != null) // avoid NPD incidental warnings { try { data = double.Parse(stringNumber.Trim()); } catch (FormatException exceptNumberFormat) { IO.Logger.Log(NLog.LogLevel.Warn, exceptNumberFormat, "Number format exception parsing data from string"); } } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error with stream reading"); } finally { /* clean up stream reading objects */ try { if (sr != null) { sr.Close(); } } catch (IOException exceptIO) { IO.Logger.Log(NLog.LogLevel.Warn, exceptIO, "Error closing StreamReader"); } } } break; default: /* INCIDENTAL: CWE 561 Dead Code, the code below will never run * but ensure data is inititialized before the Sink to avoid compiler errors */ data = 0.0d; break; } { /* POTENTIAL FLAW: Convert data to a int, possibly causing a truncation error */ IO.WriteLine((int)data); } }
/// <summary> /// Fires specific action and returns result provided by the parent control. /// </summary> /// <param name="dr">Data related to the action.</param> private string GetHtml(string url) { if (UseServerRequestType) { // Create web client and try to obatin HTML using it WebClient client = new WebClient(); try { StreamReader reader = StreamReader.New(client.OpenRead(url)); return reader.ReadToEnd(); } catch { return null; } } else { // Get HTML stored using javascript return HttpUtility.UrlDecode(hdnHTML.Value); } }
public override IProblemData LoadData(IDataDescriptor descriptor) { var values = new List <IList>(); var tList = new List <DateTime>(); var dList = new List <double>(); values.Add(tList); values.Add(dList); using (var client = new WebClient()) { var s = client.OpenRead("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml"); if (s != null) { using (var reader = new XmlTextReader(s)) { reader.MoveToContent(); reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"); reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"); // foreach time do { reader.MoveToAttribute("time"); tList.Add(reader.ReadContentAsDateTime()); reader.MoveToElement(); reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"); // foreach currencys do { // find matching entry if (descriptor.Name.Contains(reader.GetAttribute("currency"))) { reader.MoveToAttribute("rate"); dList.Add(reader.ReadContentAsDouble()); reader.MoveToElement(); // skip remaining siblings while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")) { ; } break; } } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")); } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")); } } } // keep only the rows with data for this exchange rate if (tList.Count > dList.Count) { tList.RemoveRange(dList.Count, tList.Count - dList.Count); } else if (dList.Count > tList.Count) { dList.RemoveRange(tList.Count, dList.Count - tList.Count); } // entries in ECB XML are ordered most recent first => reverse lists tList.Reverse(); dList.Reverse(); // calculate exchange rate deltas var changes = new[] { 0.0 } // first element .Concat(dList.Zip(dList.Skip(1), (prev, cur) => cur - prev)).ToList(); values.Add(changes); var targetVariable = "d(" + descriptor.Name + ")"; var allowedInputVariables = new string[] { targetVariable }; var ds = new Dataset(new string[] { "Day", descriptor.Name, targetVariable }, values); return(new ProblemData(ds, allowedInputVariables, targetVariable)); }
private XmlDocument RetrieveXml(string URLString) { WebClient webClient = new WebClient(); webClient.Credentials = new System.Net.NetworkCredential(API_KEY, API_PASSWORD); XmlDocument xml = new XmlDocument(); using (XmlTextReader xmlTextReader = new XmlTextReader(webClient.OpenRead(URLString))) { while (xmlTextReader.Read()) { xml.Load(xmlTextReader); xmlTextReader.Close(); } } return xml; }
//--------------------------------------------------------- private string GetWebClientData(string targetUrl) { //Cmn.Log.WriteToFile("targetUrl", targetUrl); string _retVal = ""; WebClient _webClient = new WebClient(); //增加SessionID,为了解决当前用户登录问题 // Cmn.Session.SetUserID("kdkkk"); //随便设置一个session 否则SessionID会变 Cmn.Session.Set("cmn_ItfProxy_tmp","test"); targetUrl = Cmn.Func.AddParamToUrl(targetUrl, "CurSessionID=" + Session.SessionID); Cmn.Log.WriteToFile("targetUrl", targetUrl); if (Request.Form.Count > 0) { //post方式 string _postString = ""; for (int _i = 0; _i < Request.Form.Count; _i++) { if (_postString != "") { _postString += "&"; } _postString += Request.Form.Keys[_i] + "=" + System.Web.HttpUtility.UrlEncode(Request.Form[_i].ToString(), Encoding.UTF8); } byte[] _postData = Encoding.GetEncoding("utf-8").GetBytes(_postString); _webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] _responseData = _webClient.UploadData(targetUrl, "POST", _postData);//得到返回字符流 _retVal = Encoding.GetEncoding("utf-8").GetString(_responseData);//解码 } else { Stream _stream = _webClient.OpenRead(targetUrl); StreamReader _rd = new StreamReader(_stream, Encoding.GetEncoding("utf-8")); _retVal = _rd.ReadToEnd(); _stream.Close(); } return _retVal; }
protected override Task<Stream> OpenReadAsync(WebClient wc, string address) => Task.Run(() => wc.OpenRead(address));
public static void OpenRead_InvalidArguments_ThrowExceptions() { var wc = new WebClient(); Assert.Throws<ArgumentNullException>("address", () => { wc.OpenRead((string)null); }); Assert.Throws<ArgumentNullException>("address", () => { wc.OpenRead((Uri)null); }); }
public static string ExecuteGetCommand(string url) { System.Net.ServicePointManager.Expect100Continue = false; using (WebClient client = new WebClient()) { try { using (Stream stream = client.OpenRead(url)) { using (StreamReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } } catch (WebException ex) { // // Handle HTTP 404 errors gracefully and return a null string to indicate there is no content. // if (ex.Response is HttpWebResponse) { if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound) { return null; } } //throw ex; } } return null; }