2013-08-29 144 views
3

我刚刚从GoDaddy购买了一些在线存储,而我尝试将FTP存入我的存储帐户。问题是,我可以使用FileZilla查看和修改我的帐户,但是由于“主机名无法解析”错误,我的C Sharp程序甚至无法访问它。“主机名无法解析”从“ftp”URL

我认为这是因为我的帐户的整个ftp地址在url中有两个“@”符号,这是URI创建过程中的肆虐。

无论如何,我可以解决这个问题,或者我因为GoDaddy存储的命名约定而被搞砸了吗?

的URL为:FTP:[slashslash] lastname.firstname @ gmail.com @ onlinefilefolder.com /主页/

+2

到底你在做什么?你有任何示例代码?确切地说,你传入的godaddy主机名的格式是什么? –

+0

URL地址为:ftp://[email protected]@onlinefilefolder.com/Home/The%20Files/accounts%20name.txt – dcfjoe

+0

URIs可以有'@'它是一个保留的分隔符。 http://tools.ietf.org/html/rfc3986#section-2.2看来你的解析器不太好。 – Hogan

回答

2

的异常是从System.Uri,其源自(尽管它是由标准定义可接受的)将不允许有两个@符号。

// This will reproduce the reported exception, I assume it is what your code is 
// doing either explicitly, or somewhere internally 
new Uri(@"ftp://[email protected]@onlinefilefolder.com/Home/") 

一个潜在的解决方法是百分之编码的第一@符号,这将使无一例外地被实例化Uri实例 - 但可能会或可能不依赖于服务器的行为的工作(我只用这接近几次,但它为我工作):

new Uri(@"ftp://lastname.firstname%[email protected]/Home/") 
+0

您的建议已经停止了错误,我的程序甚至“成功”上传到我的在线存储帐户,但每当我使用FileZilla去那里时,该文件实际上并不存在。看来百分比编码对我来说不是一个可行的选择。 – dcfjoe

3

你需要指定URI中的用户名和密码的一些具体原因是什么?您可以简单地连接到主机,然后提供凭据。

// Create a request to the host 
var request = (FtpWebRequest)WebRequest.Create("ftp://onlinefilefolder.com"); 

// Set the username and password to use 
request.Credentials = new NetworkCredential ("[email protected]","password"); 

request.Method = WebRequestMethods.Ftp.UploadFile; 

var sourceStream = new StreamReader("testfile.txt"); 
var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
sourceStream.Close(); 
request.ContentLength = fileContents.Length; 

Stream requestStream = request.GetRequestStream(); 
requestStream.Write(fileContents, 0, fileContents.Length); 
requestStream.Close(); 

FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

response.Close();