这里的代码给出了错误“{”无效的URI:无法解析主机名。“}”
interface User {
id: string;
name: string;
Lastname: string;
}
class UsersCollection {
private id = 15;
constructor(private _users: Array<User> = []) {
}
public create(name: string, lastName: string): User {
let user = {
id: this.id++,
name: name,
Lastname: lastName
};
this._users.push(user);
return user;
}
public searchUser(name: string, lastName: string): User | null {
return this._users.find((user) => {
return user.name == name && user.Lastname == lastName;
})
}
public getOrCreate(name: string, lastName: string) {
let user = this.searchUser(name, lastName);
if (user) {
return user;
}
return this.create(name, lastName);
}
public size(){
return this._users.length;
}
}
let users = new UsersCollection([{
'id': '1',
'name': 'rob',
'Lastname': 'Xyz'
},
{
'id': '2',
'name': 'xyz',
'Lastname': 'abc'
}]);
// Existing user
console.log(users.getOrCreate('rob','Xyz'));
console.log(users.size() == 2);
// Add new one
console.log(users.getOrCreate('rob','123'));
console.log(users.size() == 3);
这里的ftp密码有一个@字符,当删除时允许这段代码继续。但是密码有一个@字符,那我怎么能让它工作呢?
答案 0 :(得分:3)
您需要对@
符号进行十六进制编码:
string encodedPassword = "p@ssword".Replace("@", "%40");
FtpWebRequest FTPRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://"
+ "user123" +
":" + encodedPassword + "@" + downloadUri);
(根据安德鲁的建议)如果你想抓住更多&#34;专业&#34;字符:
添加对System.Web
的引用并使用以下内容:
string encodedPassword = HttpUtility.UrlEncode("p@ssword");
FtpWebRequest FTPRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://"
+ "user123" +
":" + encodedPassword + "@" + downloadUri);
或者,甚至更好:
string userName = "user123";
string password = "p@ssword";
string ftpPath = string.Format("ftp://{0}:{1}@{2}", userName, password, downloadUri);
var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(HttpUtility.UrlEncode(ftpPath));