Skip to content

6 使用不同的编码

Lewis Zou edited this page Jan 6, 2021 · 1 revision
	class MyDataParser : DataParser
	{
		public MyDataParser()
		{
			SelectableBuilder = context =>
			{
				var text = Encoding.GetEncoding("GB2312").GetString(context.Response.Content.Bytes);
				var uri = context.Request.RequestUri;
				var domain = uri.Port == 80 || uri.Port == 443
					? $"{uri.Scheme}://{uri.Host}"
					: $"{uri.Scheme}://{uri.Host}:{uri.Port}";
				return new HtmlSelectable(text, domain, context.Options.RemoveOutboundLinks);
			};
		}

		protected override Task ParseAsync(DataFlowContext context)
		{
			context.AddData("URL", context.Request.RequestUri);
			context.AddData("Title", context.Selectable.XPath(".//title")?.Value);
			return Task.CompletedTask;
		}
	}
Clone this wiki locally