public byte[] Encoding(string sourceStr) { var _encoding = TextEncoding.GetEncoding(EncodePageName); var _sourceBytes = TextEncoding.UTF8.GetBytes(sourceStr); return(TextEncoding.Convert(TextEncoding.UTF8, _encoding, _sourceBytes)); }
/// <summary> /// Output this <see cref="XDocument"/> to a <see cref="Stream"/>. /// </summary> /// <param name="stream"> /// The <see cref="Stream"/> to output the XML to. /// </param> /// <param name="options"> /// If SaveOptions.DisableFormatting is enabled the output is not indented. /// If SaveOptions.OmitDuplicateNamespaces is enabled duplicate namespace declarations will be removed. /// </param> /// <param name="cancellationToken">A cancellation token.</param> public async Task SaveAsync(Stream stream, SaveOptions options, CancellationToken cancellationToken) { XmlWriterSettings ws = GetXmlWriterSettings(options); ws.Async = true; if (_declaration != null && !string.IsNullOrEmpty(_declaration.Encoding)) { try { ws.Encoding = Encoding.GetEncoding(_declaration.Encoding); } catch (ArgumentException) { } } XmlWriter w = XmlWriter.Create(stream, ws); await using (w.ConfigureAwait(false)) { await WriteToAsync(w, cancellationToken).ConfigureAwait(false); await w.FlushAsync().ConfigureAwait(false); } }
private static IHttpHandler FindHandler(string name) { Debug.Assert(name != null); switch (name) { case "idpe.js": return(new ManifestResourceHandler("idpe.js", "text/css", Encoding.GetEncoding("Windows-1252"))); case "running.png": return(new ManifestResourceHandler("running.png", "text/css", Encoding.GetEncoding("Windows-1252"))); case "stopped.png": return(new ManifestResourceHandler("stopped.png", "text/css", Encoding.GetEncoding("Windows-1252"))); case "idpe.css": return(new ManifestResourceHandler("idpe.css", "text/css", Encoding.GetEncoding("Windows-1252"))); case "archivelog": return(new LogArchivePage(_ArchiveLogRelativePath)); case "monitor": default: return(new MonitorPage()); } }
public void Parse() { WebClient client = new WebClient(); client.Encoding = Encoding.GetEncoding( SearchEnc.SearchEncoding(url)); var htmlNode = new HtmlDocument(); while (!String.IsNullOrEmpty(url)) { htmlNode.LoadHtml(client.DownloadString(url)); var documentNode = htmlNode.DocumentNode; try { var ulNode = documentNode .SelectNodes("//ul") .Select(node => node.LastChild); var aNode = ulNode.ElementAt(0).SelectSingleNode("a"); var linkNode = aNode.Attributes ["href"] != null ? HttpUtility.HtmlDecode(sites [6] + aNode.Attributes ["href"].Value.ToString()) : "Can't parse"; date = Tuple.Create(Convert.ToInt32(aNode.InnerText.Split(new Char [] { ' ', '&' }).ToList()[1]), -1); url = linkNode; } catch (ArgumentNullException) { var articlesUrl = documentNode .SelectNodes("//td[not(@*)]/a") .Select(node => node.Attributes ["href"].Value != null ? HttpUtility.HtmlDecode(sites [6] + node.Attributes ["href"].Value) : "Can't parse") .ToArray(); articles = articlesUrl; } } articles = new string[] {}; }
public void Parse() { WebClient client = new WebClient(); client.Encoding = Encoding.GetEncoding( SearchEnc.SearchEncoding(url)); var htmlNode = new HtmlDocument(); htmlNode.LoadHtml(client.DownloadString(url)); var documentNode = htmlNode.DocumentNode; var lastIssue = documentNode .SelectNodes("//tr[2]/td[@class='nr' and last()]/a") .Select(node => node.Attributes ["href"] != null ? HttpUtility.HtmlDecode(url + node.Attributes ["href"].Value.ToString()) : "Can't parse").ElementAt(0); var uriAddress = new Uri(lastIssue); var issueDate = uriAddress.AbsolutePath.Split('/')[1] + '/'; htmlNode.LoadHtml(client.DownloadString(lastIssue)); documentNode = htmlNode.DocumentNode; var articlesArray = documentNode .SelectNodes("//tr/td[@class='pub_pp']/a") .Select(node => node.Attributes ["href"] != null ? HttpUtility.HtmlDecode(url + issueDate + node.Attributes ["href"].Value.ToString()) : "Can't parse") .ToArray(); date = Tuple.Create(Convert.ToInt32(articlesArray [0] .Split('/') [3] .Replace('-', ' ').Split(' ')[0]), Convert.ToInt32(articlesArray [0] .Split('/') [3] .Replace('-', ' ').Split(' ')[1])); articles = articlesArray; }
public void Parse() { WebClient client = new WebClient(); client.Encoding = Encoding.GetEncoding( SearchEnc.SearchEncoding(url)); var htmlNode = new HtmlDocument(); htmlNode.LoadHtml(client.DownloadString(url)); var documentNode = htmlNode.DocumentNode; var lastIssue = url + documentNode.SelectSingleNode("//div[@id='avatar-right']//li[1]/a") .GetAttributeValue("href", "Can't parse"); htmlNode.LoadHtml(client.DownloadString(lastIssue)); documentNode = htmlNode.DocumentNode; date = Tuple.Create(Convert.ToInt32(documentNode .SelectNodes("//div[@class='category-list']/h2/span") [0] .InnerText .Replace(" / ", " ").Split(' ')[0]), Convert.ToInt32(documentNode .SelectNodes("//div[@class='category-list']/h2/span") [0] .InnerText .Replace(" / ", " ").Split(' ')[1])); articles = documentNode .SelectNodes("//div[@class='sectionlist']/ul/li/a") .Select(node => node.Attributes ["href"] != null ? HttpUtility.HtmlDecode(url + node.Attributes ["href"].Value.ToString()) : "Can't parse") .ToArray(); }
private HttpRequestModel TransformModel(HttpRequestMessage request) { var content = new StringContent(Fixture.Create <string>()); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); request.Content = content; request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.Headers.Add("Forwarded", "for=192.0.2.43, for=198.51.100.17"); var body = new Body() { Data = request.Content.ReadAsStringAsync().Result, ContentType = new ContentType() { MediaType = content.Headers.ContentType.MediaType, CharSet = content.Headers?.ContentType.CharSet != null ? Encoding.GetEncoding(content.Headers?.ContentType?.CharSet).WebName : null } }; var method = request.Method; var query = request.RequestUri.Query; var port = request.RequestUri.Port; var headers = new Headers(); request.Headers.ForEach(h => headers.Add(h.Key, string.Join(",", h.Value))); string localPath = request.RequestUri.LocalPath; return(new HttpRequestModel(body, method, headers, query, localPath, port)); }
public string Decode(string strToDecode, string charset) { byte[] data = Convert.FromBase64String(strToDecode); CharSetEncoding encoding = CharSetEncoding.GetEncoding(charset); return(encoding.GetString(data)); }
/// <summary> /// Returns an object that implements the <see cref="IHttpHandler"/> interface and which is responsible for serving the request. /// </summary> /// <returns> /// A new <see cref="IHttpHandler"/> object that processes the request. /// </returns> public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { // In MVC requests, PathInfo isn't set - determine via Path.. // e.g. "/admin/errors/detail" or "/admin/errors/" var match = Regex.Match(context.Request.Path, @"/?(?<resource>\w+)/?$"); var resource = match.Success ? match.Groups["resource"].Value : ""; switch (resource.ToLower(CultureInfo.InvariantCulture)) { case "detail": return(new ErrorDetailPage()); case "html": return(new ErrorHtmlPage()); case "rss": return(new ErrorRssHandler()); case "stylesheet": return(new ManifestResourceHandler("ErrorLog.css", "text/css", Encoding.GetEncoding("Windows-1252"))); case "delete": string errorId = context.Request.QueryString["id"] ?? ""; bool result = false; if (errorId.Length >= 0) { result = ErrorLog.Default.DeleteError(errorId); } return(new MessageHandler(@"<meta http-equiv=""refresh"" content=""0;URL=" + context.Request.Path.Replace("/delete", "") + @""">")); case "protect": // send back a "true" or "false" - this will be handled in javascript return(new MessageHandler(ErrorLog.Default.ProtectError(context.Request.QueryString["id"]).ToString())); case "test": throw new Exception("This is a test exception generated by " + (new PoweredBy()).ToString() + ". Please disregard."); case "testinner": try { int a = 3; int b = 0; int x = a / b; } catch (Exception ex) { throw new Exception("This is a wrapped exception generated by " + (new PoweredBy()).ToString() + ". Please disregard.", ex); } throw new Exception("This is a test exception generated by " + (new PoweredBy()).ToString() + ". Please disregard."); case "testignoredsame": throw new ArgumentException(); case "testignoreddescendent": throw new ArgumentNullException(); default: return(new ErrorLogPage()); } }
private object GetExpectedResponse(HttpResponseModel httpResponseModel) { HttpResponseMessage httpResponseMessage = new HttpResponseMessage { StatusCode = httpResponseModel.StatusCode }; httpResponseModel.Headers.Dictionary.ForEach(s => httpResponseMessage.Headers.Add(s.Key, s.Value.ToString())); var content = new StringContent(httpResponseModel.Body.Data, Encoding.GetEncoding(httpResponseModel.Body.ContentType.CharSet), httpResponseModel.Body.ContentType.MediaType); httpResponseMessage.Content = content; return(httpResponseMessage); }
/// <summary> /// Encodes the post information in the application/form-url-encoded format /// </summary> /// <param name="postData">The post data to encode.</param> /// <returns>The encoded post data.</returns> static public byte[] EncodeBytes(NameValueCollection postData) { if (postData.Count == 0) { return(new byte[0]); } string postDataString = GetQueryString(postData); const int WesternEuropeanWindowsCodePage = 1252; return(Encoding.GetEncoding(WesternEuropeanWindowsCodePage) .GetBytes(postDataString)); }
/// <summary> /// Returns an object that implements the <see cref="IHttpHandler"/> /// interface and which is responsible for serving the request. /// </summary> /// <returns> /// A new <see cref="IHttpHandler"/> object that processes the request. /// </returns> public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { // // The request resource is determined by the looking up the // value of the PATH_INFO server variable. // string resource = context.Request.PathInfo.Length == 0 ? string.Empty : context.Request.PathInfo.Substring(1); switch (resource.ToLower(CultureInfo.InvariantCulture)) { case "detail": return(new ErrorDetailPage()); case "html": return(new ErrorHtmlPage()); case "rss": return(new ErrorRssHandler()); case "stylesheet": return(new ManifestResourceHandler("ErrorLog.css", "text/css", Encoding.GetEncoding("Windows-1252"))); case "test": throw new TestException(); default: { if (resource.Length == 0) { return(new ErrorLogPage()); } else { throw new HttpException(404, "Resource not found."); } } } }
public void Parse() { WebClient client = new WebClient(); client.Encoding = Encoding.GetEncoding( SearchEnc.SearchEncoding(url)); var htmlNode = new HtmlDocument(); htmlNode.LoadHtml(client.DownloadString(url)); var documentNode = htmlNode.DocumentNode; var lastIssue = documentNode .SelectNodes("//dd/a") .Where(node => node.InnerText == "Последний выпуск") .Select(node => sites[1] + node.Attributes["href"].Value.ToString()) .First(); htmlNode.LoadHtml(client.DownloadString(lastIssue)); documentNode = htmlNode.DocumentNode; date = Tuple.Create(Convert.ToInt32(documentNode .SelectNodes("//body/p")[0] .InnerText .Split('№')[1] .Replace("(", string.Empty) .Replace(")", string.Empty) .Substring(1) .Split(' ')[0]), Convert.ToInt32(documentNode .SelectNodes("//body/p")[0] .InnerText .Split('№')[1] .Replace("(", string.Empty) .Replace(")", string.Empty) .Substring(1) .Split(' ')[1])); var aNode = documentNode .SelectNodes("//dd/a"); articles = aNode .Take(aNode.Count - 2) .Select(node => node.Attributes ["href"] != null ? HttpUtility.HtmlDecode(sites[1] + node.Attributes ["href"].Value.ToString()) : "Can't parse") .ToArray(); }
/// <summary> /// Output this <see cref="XDocument"/> to a <see cref="Stream"/>. /// </summary> /// <param name="stream"> /// The <see cref="Stream"/> to output the XML to. /// </param> /// <param name="options"> /// If SaveOptions.DisableFormatting is enabled the output is not indented. /// If SaveOptions.OmitDuplicateNamespaces is enabled duplicate namespace declarations will be removed. /// </param> public void Save(Stream stream, SaveOptions options) { XmlWriterSettings ws = GetXmlWriterSettings(options); if (declaration != null && !string.IsNullOrEmpty(declaration.Encoding)) { try { ws.Encoding = Encoding.GetEncoding(declaration.Encoding); } catch (ArgumentException) { } } using (XmlWriter w = XmlWriter.Create(stream, ws)) { Save(w); } }
private static IHttpHandler FindHandler(string name) { Debug.Assert(name != null); switch (name) { case "detail": return(new ErrorDetailPage()); case "html": return(new ErrorHtmlPage()); case "xml": return(new ErrorXmlHandler()); case "json": return(new ErrorJsonHandler()); case "rss": return(new ErrorRssHandler()); case "digestrss": return(new ErrorDigestRssHandler()); case "download": return(new ErrorLogDownloadHandler()); case "stylesheet": return(new ManifestResourceHandler("ErrorLog.css", "text/css", Encoding.GetEncoding("Windows-1252"))); case "test": throw new TestException(); case "about": return(new AboutPage()); default: return(name.Length == 0 ? new ErrorLogPage() : null); } }
public void Parse() { WebClient client = new WebClient(); client.Encoding = Encoding.GetEncoding( SearchEnc.SearchEncoding(url)); var htmlNode = new HtmlDocument(); htmlNode.LoadHtml(client.DownloadString(url)); var documentNode = htmlNode.DocumentNode; var trNode = documentNode .SelectNodes("//tr[@class='leftmenuarticles']") [0] .SelectSingleNode("td/div"); var aNode = trNode.SelectSingleNode("a"); date = Tuple.Create(Convert.ToInt32(aNode.InnerText.Replace(" / ", " ").Split(' ') [0]), Convert.ToInt32(aNode.InnerText.Replace(" / ", " ").Split(' ')[1])); var link = aNode.Attributes ["href"] != null ? HttpUtility.HtmlDecode(aNode.Attributes ["href"].Value.ToString()) : "Can't parse"; htmlNode.LoadHtml(client.DownloadString(link)); documentNode = htmlNode.DocumentNode; var trArtNodes = documentNode .SelectNodes("//tr[@class='leftmenuarticles']"); List <string> articlesList = new List <string>(); articlesList.Add(link); foreach (var trArtNode in trArtNodes.Skip(1)) { var aArtNodes = trArtNode .SelectNodes("td/div/a"); foreach (var aArtNode in aArtNodes) { articlesList.Add(aArtNode.Attributes ["href"] != null ? HttpUtility.HtmlDecode(aArtNode.Attributes ["href"].Value.ToString()) : "Can't parse"); } } articles = articlesList.ToArray(); }
public void Parse() { WebClient client = new WebClient(); client.Encoding = Encoding.GetEncoding( SearchEnc.SearchEncoding(url)); var htmlNode = new HtmlDocument(); htmlNode.LoadHtml(client.DownloadString(url)); var documentNode = htmlNode.DocumentNode; var lastIssue = documentNode.SelectSingleNode("//div[@class='journal']//div[@class='new-num']/a") .GetAttributeValue("href", "Can't parse"); htmlNode.LoadHtml(client.DownloadString(lastIssue)); documentNode = htmlNode.DocumentNode; date = Tuple.Create(Convert.ToInt32(new Uri(lastIssue).AbsolutePath.Substring(1).Split('-')[0]), Convert.ToInt32(new Uri(lastIssue).AbsolutePath.Substring(1).Split('-')[2].Split('%')[0])); articles = documentNode .SelectNodes("//table[@class='link']//div[@class='link']/a") .Select(node => node.Attributes ["href"] != null ? HttpUtility.HtmlDecode(node.Attributes ["href"].Value.ToString()) : "Can't parse") .ToArray(); }
public byte[] Encoding(byte[] sourceBytes, TextEncoding sourceEncode) { var _encoding = TextEncoding.GetEncoding(EncodePageName); return(TextEncoding.Convert(sourceEncode, _encoding, sourceBytes)); }
private static string ReadContext(HttpContent context, bool databyte = false) { try { Console.WriteLine(context.Headers.ContentEncoding); if (context.Headers.ContentEncoding.Contains("ggggzip")) { Console.WriteLine("DECODE GZIP"); var responseStream = context.ReadAsStreamAsync().Result; GZipStream ms; var stream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Decompress); } if (databyte) { var result = context.ReadAsByteArrayAsync().Result; var encoding = Encoding.GetEncoding("windows-1251"); result = Encoding.Convert(encoding, Encoding.Default, result); Console.WriteLine("RET DATABYTE ENC"); return(Encoding.Default.GetString(result)); } } catch (Exception e) { } try { return(context.ReadAsStringAsync().Result); } catch (Exception e) { var result = context.ReadAsByteArrayAsync().Result; try { var encoding = Encoding.Default; result = Encoding.Convert(encoding, Encoding.Default, result); } catch { try { var encoding = Encoding.GetEncoding(context.Headers.ContentType.CharSet); result = Encoding.Convert(encoding, Encoding.Default, result); } catch { try { var encoding = Encoding.UTF8; result = Encoding.Convert(encoding, Encoding.Default, result); } catch { try { var encoding = Encoding.ASCII; result = Encoding.Convert(encoding, Encoding.Default, result); } catch { try { var encoding = Encoding.Unicode; result = Encoding.Convert(encoding, Encoding.Default, result); } catch (Exception ex) { } } } } } return(Encoding.Default.GetString(result)); } }
/// <summary> /// Converts a byte[] to a string. /// </summary> /// <param name="bytes">The bytes to convert.</param> /// <returns>The resulting string.</returns> private static string GetString(byte[] bytes) { var buffer = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, bytes); return(Encoding.UTF8.GetString(buffer, 0, bytes.Count())); }
/** Load a vocab file {@code <vocabName>.tokens} and return mapping. */ public virtual IDictionary <string, int> Load() { IDictionary <string, int> tokens = new LinkedHashMap <string, int>(); int maxTokenType = -1; string fullFile = GetImportedVocabFile(); AntlrTool tool = g.tool; string vocabName = g.GetOptionString("tokenVocab"); try { Regex tokenDefPattern = new Regex("([^\n]+?)[ \\t]*?=[ \\t]*?([0-9]+)"); string[] lines; if (tool.grammarEncoding != null) { lines = File.ReadAllLines(fullFile, Encoding.GetEncoding(tool.grammarEncoding)); } else { lines = File.ReadAllLines(fullFile); } for (int i = 0; i < lines.Length; i++) { string tokenDef = lines[i]; int lineNum = i + 1; Match matcher = tokenDefPattern.Match(tokenDef); if (matcher.Success) { string tokenID = matcher.Groups[1].Value; string tokenTypeS = matcher.Groups[2].Value; int tokenType; if (!int.TryParse(tokenTypeS, out tokenType)) { tool.errMgr.ToolError(ErrorType.TOKENS_FILE_SYNTAX_ERROR, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION, " bad token type: " + tokenTypeS, lineNum); tokenType = TokenTypes.Invalid; } tool.Log("grammar", "import " + tokenID + "=" + tokenType); tokens[tokenID] = tokenType; maxTokenType = Math.Max(maxTokenType, tokenType); lineNum++; } else { if (tokenDef.Length > 0) // ignore blank lines { tool.errMgr.ToolError(ErrorType.TOKENS_FILE_SYNTAX_ERROR, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION, " bad token def: " + tokenDef, lineNum); } } } } catch (FileNotFoundException) { GrammarAST inTree = g.ast.GetOptionAST("tokenVocab"); string inTreeValue = inTree.Token.Text; if (vocabName.Equals(inTreeValue)) { tool.errMgr.GrammarError(ErrorType.CANNOT_FIND_TOKENS_FILE_REFD_IN_GRAMMAR, g.fileName, inTree.Token, fullFile); } else // must be from -D option on cmd-line not token in tree { tool.errMgr.ToolError(ErrorType.CANNOT_FIND_TOKENS_FILE_GIVEN_ON_CMDLINE, fullFile, g.name); } } catch (Exception e) { tool.errMgr.ToolError(ErrorType.ERROR_READING_TOKENS_FILE, e, fullFile, e.Message); } return(tokens); }
public byte[] Encoding(byte[] sourceBytes) { var encoding = TextEncoding.GetEncoding(EncodePageName); return(TextEncoding.Convert(TextEncoding.UTF8, encoding, sourceBytes)); }
private static IHttpHandler FindHandler(string name) { Debug.Assert(name != null); switch (name) { case "detail": return(new ErrorDetailPage()); case "html": return(new ErrorHtmlPage()); case "xml": return(new DelegatingHttpHandler(ErrorXmlHandler.ProcessRequest)); case "json": return(new DelegatingHttpHandler(ErrorJsonHandler.ProcessRequest)); case "rss": return(new DelegatingHttpHandler(ErrorRssHandler.ProcessRequest)); case "digestrss": return(new DelegatingHttpHandler(ErrorDigestRssHandler.ProcessRequest)); case "download": #if NET_3_5 || NET_4_0 return(new HttpAsyncHandler((context, getAsyncCallback) => HttpTextAsyncHandler.Create(ErrorLogDownloadHandler.ProcessRequest)(context, getAsyncCallback))); #else return(new DelegatingHttpTaskAsyncHandler(ErrorLogDownloadHandler.ProcessRequestAsync)); #endif case "stylesheet": return(new DelegatingHttpHandler(ManifestResourceHandler.Create(StyleSheetHelper.StyleSheetResourceNames, "text/css", Encoding.GetEncoding("Windows-1252"), true))); case "test": throw new TestException(); case "about": return(new AboutPage()); default: return(name.Length == 0 ? new ErrorLogPage() : null); } }
public static string ReadTemplate(string folder, string fileName, string encoding = "GBK") { fileName = Path.Combine(folder, fileName); return(FileHelper.Read(fileName, Encoding.GetEncoding(encoding))); }