Tag: uri

从任何URL获取确切的域名

我需要从任何Url中提取确切的域名。 例如, url: http : //www.google.com – >域名:google.com url: http : //www.google.co.uk/path1/path2 – >域名:google.co.uk 怎样才能在c#中实现? 是否有完整的TLD列表或该任务的解析器?

从URI相对路径中删除%20

我正在生成从1个目录到另一个目录的相对路径。 如果OutputDirectoryName属性是包含空格的目录,则使用%20而不是空格对空格进行编码。 我正在创建一个Windows文件夹的相对路径,所以我必须使用空格我的相关路径。 是否有一种干净的方式来指定URI的编码方式? 我知道我可以在relativePath.ToString()上做一个stirng替换,但我想知道是否有更好的实现。 谢谢。 public string GetOutputDirectoryAsRelativePath(string baseDirectory) { Uri baseUri = new Uri(baseDirectory); Uri destinationUri = new Uri(OutputDirectoryName); Uri relativePath = baseUri.MakeRelativeUri(destinationUri); return relativePath.ToString(); }

如何编码包含哈希的路径?

你如何正确编码包含哈希(#)的路径 ? 请注意,哈希不是片段(书签?)指示符,而是路径名称的一部分。 例如,如果有这样的路径: http://www.contoso.com/code/c#/somecode.cs 例如,尝试执行此操作时会导致问题: Uri myUri = new Uri(“http://www.contoso.com/code/c#/somecode.cs”); 似乎它将散列解释为片段指示符。 用%23手动替换#感觉不对。 是否还有其他角色需要更换? 在Uri和HttpUtility中有一些转义方法,但似乎没有一个可以做到这一点。

从Saxon 9.4he中的嵌入资源加载xml和xslt

我使用Saxon 9.4家庭版( Saxon-HE 9.4 .NET )来获得对XSLT 2.0和XPath 2.0以及.NET中的XQuery 1.0的支持。 当我加载没有URI的文件时,我的代码崩溃了。 是否可以在没有与加载的文档相关的URI的情况下加载xml / xsl文档? 如果没有,有没有办法为嵌入在dll文件中的元素定义URI? 任何其他解决方案也将受到赞赏,我唯一的术语是必须从dll文件中加载文件。 只要我从文件加载xml / xsl,我的代码就可以正常工作: const string sourcePath = @”C:\test\TestInvoiceWithError.xml”; const string xsltpath = @”C:\test\UBL-T10-BiiRules.xsl”; 当我尝试从嵌入式资源加载时,代码抛出一个exception,说明“没有提供基本URI” : Stream sourceStream = GetEmbeddedResource(“TestProject1.testfiles.TestInvoice.xml”); Stream xsltStream = GetEmbeddedResource(“TestProject1.testfiles.UBL-T10-BiiRules.xsl”); 我还为具有相对路径的资源创建了Uri,这会引发exception“相对URI不支持此操作”。 : Uri sourceUri = new Uri(“/TestProject1;component/testfiles/TestInvoice.xml”, UriKind.Relative); Uri xsltUri = new Uri(“/TestProject1;component/testfiles/UBL-T10-BiiRules.xsl.xml”, UriKind.Relative); 这是我的代码: using System; […]

在C#/ .NET中获取url的域名

代码: string sURL = “http://subdomain.website.com/index.htm”; MessageBox.Show(new System.Uri(sURL).Host); 给了我“subdomain.website.com” 但我需要主域名“website.com”用于任何url或网站链接。 我怎么做?

检查是否可以访问URL – 帮助优化类

net 4和c#。 如果Uri(字符串)返回HTTP状态代码200,我需要一个能够返回Bool值的Class。 目前我有这个代码(它使用try来查看是否可以连接到Uri),但我希望用“HttpStatusCode.OK”实现。 你知道更好的方法吗? 谢谢。 public static bool IsReachableUri(string uriInput) { // Variable to Return bool testStatus; // Create a request for the URL. WebRequest request = WebRequest.Create(uriInput); request.Timeout = 15000; // 15 Sec WebResponse response; try { response = request.GetResponse(); testStatus = true; // Uri does exist response.Close(); } catch (Exception) { testStatus […]

最佳URLvalidation

我使用下面的代码来检查URLvalidation: public static bool CheckURLValid(string strURL) { Uri uriResult; return Uri.TryCreate(strURL, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp; } 下面的结果应该显示为true ,但不知何故它有自己的模式来validationurl: false :google.com 是的 : http : //www.google.com false : https : //www.google.com.my/webhp?sourceid = chrome-instant&ion = 1&espv = 2&es_th = 1&ie = UTF-8’newwindow = 1&q = check%20if%20valid%20url%20c%23 是的 : https : //stackoverflow.com/questions/ask 即时通讯使用c#,如何增强此检查urlvalidation更准确?

如何创建使用GenericUriParserOptions.DontCompressPath解析的Uri实例

当.NET System.Uri类解析字符串时,它会对输入执行一些规范化,例如对方案和主机名进行下限。 它还会修剪每个路径段的尾随时段。 后一个特性对于OpenID应用程序是致命的,因为一些OpenID(如从Yahoo发布的那些)包括base64编码的路径段,其可以以句点结束。 如何禁用Uri类的周期修剪行为? 使用UriParser.Register注册我自己的方案,使用UriParser.Register初始化的解析器可以避免周期修剪,以及其他一些对OpenID也不可取的操作。 但我无法为HTTP和HTTPS等现有方案注册新的解析器,我必须为OpenID做这些。 我尝试的另一种方法是注册我自己的新方案,并编程自定义解析器以将方案更改回标准HTTP方案作为解析的一部分: public class MyUriParser : GenericUriParser { private string actualScheme; public MyUriParser(string actualScheme) : base(GenericUriParserOptions.DontCompressPath) { this.actualScheme = actualScheme.ToLowerInvariant(); } protected override string GetComponents(Uri uri, UriComponents components, UriFormat format) { string result = base.GetComponents(uri, components, format); // Substitute our actual desired scheme in the string if it’s in […]

用于更改链接的HtmlAgilityPack示例不起作用。 我该如何做到这一点?

codeplex的例子是这样的: HtmlDocument doc = new HtmlDocument(); doc.Load(“file.htm”); foreach(HtmlNode link in doc.DocumentElement.SelectNodes(“//a[@href”]) { HtmlAttribute att = link[“href”]; att.Value = FixLink(att); } doc.Save(“file.htm”); 第一个问题是HtmlDocument。 DocumentElement不存在! 存在的是HtmlDocument。 DocumentNode,但即使我使用它,我也无法按照描述访问href属性。 我收到以下错误: Cannot apply indexing with [] to an expression of type ‘HtmlAgilityPack.HtmlNode’ 当我收到此错误时,这是​​我正在尝试编译的代码: private static void ChangeUrls(ref HtmlDocument doc) { foreach(HtmlNode link in doc.DocumentNode.SelectNodes(“//@href”)) { HtmlAttribute attr = link[“href”]; attr.Value […]

c#type用于处理相对和绝对URI和本地文件路径

我有一个用例,我将处理本地文件路径(例如c:\foo\bar.txt )和URI(例如http://somehost.com/fiz/baz )。 我也将处理相对路径和绝对路径,所以我需要像Path.Combine和朋友这样的function。 我应该使用现有的C#类型吗? Uri类型可能会起作用但是一目了然,它似乎只是URI。