我想使用System.Net.WebRequest发送简单的GET请求。但是当我试图发送包含“空格”字符的URL-s时,我遇到了一个问题。我所做的:
string url = "https://example.com/search?text=some words&page=8";
var webRequest = System.Net.WebRequest.Create(link) as HttpWebRequest;如果我尝试使用这段代码,那么
webRequest.Address == "https://example.com/search?&text=some words&page=8" (#1)
我可以手动为UrlEncoded空间添加"%20“,但是"WebRequest.Create”可以对其进行解码,我也有(#1)。我怎么才能做得对?
对不起我的英语。
发布于 2015-01-23 12:58:15
使用加号(+)代替空格。还删除第一个符号(&);它仅用于非主参数。如图所示
var url = "https://example.com/search?text=some+words&page=8";发布于 2015-01-23 13:23:36
您应该使参数值“url友好”。为此,必须使用HttpUtility.UrlEncode()来“url”值。这不仅修复了空间,还修复了许多其他危险的“怪癖”:
string val1 = "some words";
string val2 = "a <very bad> value & with specials!";
string url = "https://example.com/search?text=" + HttpUtility.UrlEncode(val1) + "&comment=" + HttpUtility.UrlEncode(val2);https://stackoverflow.com/questions/28110442
复制相似问题