如何在WebClient url中引用正斜杠

本文关键字:引用 url WebClient | 更新日期: 2023-09-27 18:15:27

我正在使用WebClient连接到web上的数据源,我需要提供用户名和密码。用户名可以包含正斜杠。但是,如果它在连接字符串中,则不起作用。

我的代码:

using (WebClient client = new WebClient)
{
      data = client.DownloadString("https//myURL" + userID + password)

对于userID,如"va2fsf",这一切都可以正常工作,但对于包含正斜杠的userID,如9k/vo1dsk,则不行。
我该如何处理?我尝试使用%2F as in 9k%2Fvo1dsk,但这不起作用。

谢谢你的帮助!

如何在WebClient url中引用正斜杠

使用httutility . urlencode .

data = client.DownloadString("https://myURL" + 
                              HttpUtility.UrlEncode(userID) +
                              HttpUtility.UrlEncode(password))