2013-11-26 48 views
0

我試圖下載圖像。他們的鏈接可能是image.pnghttp://www.example.com/image.pngc#構建字符串域

我將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); 
} 
+0

什麼變量等於:http://www.example.com//http://www.example.com/image.png? – rhughes

+0

啊對不起var str = http://www.example.com/image.png或只是image.png取決於網站 – R00059159

回答

1

這些是什麼變量一回事,像str4etc等等?而不是try catch,你可以檢查這個字符串是否是一個有效的uri。看看here。嘗試在線調試你的代碼行並檢查每一個變量,然後你會看到哪一行出錯。

編輯

如果我understodd你的權利,那麼這將是您的解決方案:

 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); 

當不請說明從何處得到這個更具體的,它始終是相同的結構?或者不同?

+0

我只需要打開http://www.example.com//http: www.example.com/image.png轉換成http://www.example.com/image.png stackoverflow拿出了htt p – R00059159

+0

我更新了我的答案,希望它有幫助。 – AstralisSomnium