URL Encoding using C#

前端 未结 13 2100
北荒
北荒 2020-11-22 08:05

I have an application which sends a POST request to the VB forum software and logs someone in (without setting cookies or anything).

Once the user is logged in I cre

13条回答
  •  孤城傲影
    2020-11-22 08:56

    You should encode only the user name or other part of the URL that could be invalid. URL encoding a URL can lead to problems since something like this:

    string url = HttpUtility.UrlEncode("http://www.google.com/search?q=Example");
    

    Will yield

    http%3a%2f%2fwww.google.com%2fsearch%3fq%3dExample

    This is obviously not going to work well. Instead, you should encode ONLY the value of the key/value pair in the query string, like this:

    string url = "http://www.google.com/search?q=" + HttpUtility.UrlEncode("Example");
    

    Hopefully that helps. Also, as teedyay mentioned, you'll still need to make sure illegal file-name characters are removed or else the file system won't like the path.

提交回复
热议问题