when download page using httpwebrequest, have problem:
before create new uri, escape url, , pass uri constructor. when use httpwebrequest download page, convert quote characterto '. strange.
orignal:
https://fr.wikipedia.org/wiki/roi_julian_!_l'Élu_des_lémurs
escaped, , pass uri constructor:
https://fr.wikipedia.org/wiki/roi_julian_!_l%27%c3%89lu_des_l%c3%a9murs
httpwebrequest send server:
https://fr.wikipedia.org/wiki/roi_julian_!_l'%c3%89lu_des_l%c3%a9murs
below test codes:
private static void test() { var title = "roi_julian_!_l'Élu_des_lémurs"; var url = "https://fr.wikipedia.org/wiki/" + uri.escapedatastring(title); var uri = new uri(url); httpwebdownload(uri); } private static void httpwebdownload(uri uri) { webresponse response = null; streamreader reader = null; httpwebrequest request = (httpwebrequest)webrequest.create(uri); request.method = "get"; request.allowautoredirect = false; response = request.getresponse(); reader = new streamreader(response.getresponsestream(), encoding.utf8); string pageresponse = reader.readtoend(); console.writeline(pageresponse); }
and trace log of system.net:
system.net verbose: 0 : [10640] webrequest::create(https://fr.wikipedia.org/wiki/roi_julian_!_l'Élu_des_lémurs) system.net verbose: 0 : [10640] httpwebrequest#33111870::httpwebrequest(https://fr.wikipedia.org/wiki/roi_julian_!_l'Élu_des_lémurs#-554901600) system.net information: 0 : [10640] current os installation type 'server'. system.net information: 0 : [10640] ras supported: true system.net verbose: 0 : [10640] exiting httpwebrequest#33111870::httpwebrequest() system.net verbose: 0 : [10640] exiting webrequest::create() -> httpwebrequest#33111870 system.net verbose: 0 : [10640] httpwebrequest#33111870::getresponse() system.net error: 0 : [10640] can't retrieve proxy settings uri 'https://fr.wikipedia.org/wiki/roi_julian_!_l'Élu_des_lémurs'. error code: 12180. system.net verbose: 0 : [10640] servicepoint#66337667::servicepoint(fr.wikipedia.org:443) system.net information: 0 : [10640] associating httpwebrequest#33111870 servicepoint#66337667 system.net information: 0 : [10640] associating connection#35489797 httpwebrequest#33111870 system.net information: 0 : [10640] connection#35489797 - created connection 10.168.184.78:55975 198.35.26.96:443. system.net information: 0 : [10640] tlsstream#45795543::.ctor(host=fr.wikipedia.org, #certs=0) system.net information: 0 : [10640] associating httpwebrequest#33111870 connectstream#65677972 system.net information: 0 : [10640] httpwebrequest#33111870 - request: /wiki/roi_julian_!_l'%c3%89lu_des_l%c3%a9murs http/1.1 system.net information: 0 : [10640] connectstream#65677972 - sending headers { host: fr.wikipedia.org connection: keep-alive }.
i thought caused dontescape parameter deprecated, so, add new function fix it, but, failed.
private const ulong userescape = 0x00080000; public static void enableuserescape(uri uri) { fieldinfo fieldinfo = uri.gettype().getfield("m_flags", bindingflags.instance | bindingflags.nonpublic); if (fieldinfo == null) { throw new missingfieldexception("'m_flags' field not found"); } var uriflags = (ulong)fieldinfo.getvalue(uri); uriflags = uriflags | userescape; fieldinfo.setvalue(uri, uriflags); }
before pass uri httpwebdownload(), use function enable userescape, httpwebrequest send kind of url(https://fr.wikipedia.org/wiki/roi_julian_!_l'?lu_des_l?murs) server.
anyone give solution?
thanks
in test method define uri below. may you
a string @"somestring+specialcharacter" called verbatim string . means, "don't apply interpretations special characters inside string until next quote character reached"
var url = @"https://fr.wikipedia.org/wiki/roi_julian_!_l'Élu_des_lémurs";
Comments
Post a Comment