【问题标题】:c# building strings domainsc# 构建字符串域
【发布时间】:2013-12-11 06:31:01
【问题描述】:

我正在尝试下载图像。他们的链接可能是image.pnghttp://www.example.com/image.png

我将 image.png 添加到主机并将其传递给列表。所以 image.png 现在是http://www.example.com/image.png

但如果使用其他类型,我得到的是http://www.example.com//http://www.example.com/image.png

我只需要在第三个斜线之后获取字符串。这是我尝试使用的一些代码:

try
{
    path = this.txtOutput.Text + @"\" + str4 + etc;
    client.DownloadFile(str, path);
}
catch(Exception e)
{

    var uri = new Uri(str);
    String host = (String) uri.Host;
    String pathToFile = "http://" + host + "/";

    int len = pathToFile.Length;

    String fin = str.Substring(len, str.Length - len);

    path = this.txtOutput.Text + @"\" + str4 + etc;
    client.DownloadFile(fin, path);
}

【问题讨论】:

标签: c# web-scraping stringbuilder


【解决方案1】:

这些变量是关于什么的,例如str4etc 等等?您可以检查字符串是否是有效的 uri,而不是 try catch。看看here。尝试在线调试您的代码并检查每个变量,然后您将看到哪一行出错了。

编辑

如果我理解正确,那么这将是您的解决方案:

        string wrongResult = "example.com//http://www.example.com/image.png";
        string shouldResult = "example.com/image.png";

        int listIndexOfHttp = wrongResult.LastIndexOf("http:");
        string correctResult = wrongResult.Substring(listIndexOfHttp);

如果不是,请更具体地描述你从哪里得到这个并且它总是相同的结构?还是完全不同?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-02-06
  • 2016-08-23
  • 2016-01-21
  • 1970-01-01
  • 2010-09-21
  • 2013-11-26
  • 1970-01-01
相关资源
最近更新 更多