如何在URL中使用字符串参数?

时间:2015-01-22 10:43:09

标签: c# asp.net .net string url

我正在使用Rest api,所以我发送了一个带URL的请求,我的问题是当用户nome一个名称中包含空格或特殊字符的文件夹时,它什么也没发送。我使用了以下转换,但它不起作用:

string url = "http://localhost:8080/alfresco/service/set/folder/permission/?folderName="
             + folder
             + "&permi="
             + acces
             + "&username="
             + user
             + "&alf_ticket="
             + GetToken();

Uri url2 = new Uri(url);
Response.Write(url2);

2 个答案:

答案 0 :(得分:5)

您必须对变量进行URL编码才能使其安全。使用HttpUtility.UrlEncode

string urlSafeFolder = HttpUtility.UrlEncode(folder);

在您的网址中使用该变量,您就可以了。 (您必须为传入的每个参数执行此操作!)

答案 1 :(得分:0)

当任何用户输入空格或网址中的任何特殊字符时,我们需要使用UrlEncode方法对其进行编码 -

string urlSafeFolder = HttpUtility.UrlEncode(folder);

因此,如果url中存在空格,则会将其转换为%20,依此类推。 有关完整的参考检查http://www.w3schools.com/tags/ref_urlencode.asp