c# - How to make request with encoded URL in .NET 4.5+? -
today encountered interesting problem. following code doesn't work correctly.
public static void main() { string html; var requesturi = new uri("https://en.wikipedia.org/wiki/c%c3%b4te_d%27ivoire"); var httpwebrequest = (httpwebrequest)webrequest.create(requesturi); using (var httpwebresponse = (httpwebresponse)httpwebrequest.getresponse()) { using (stream responsestream = httpwebresponse.getresponsestream()) { string characterset = httpwebresponse.characterset; using (var streamreader = new streamreader(responsestream, characterset != null ? encoding.getencoding(characterset) : encoding.utf8)) { html = streamreader.readtoend(); } } } }
the problem in url decoding. in uri constructor original string automatically decoded , result "https://en.wikipedia.org/wiki/côte_d'ivoire". requesting url results in 304 location "https://en.wikipedia.org/wiki/c%c3%b4te_d%27ivoire", original encoded url, , causes infinite loop.
what should do? this configuation doesn't cause allows ignore dots , slashes.
for example, fiddler works fine in situation. requesting "https://en.wikipedia.org/wiki/côte_d'ivoire" results in 1 redirect.
update
it works in .net 4.0.
Comments
Post a Comment