c# - How to create Uri without fragmentation (convert # to %23) -


i trying advanced searches using query string when include # doesn't converted %23 when creating uri.

var webaddress = "www.worldwideweb.com/abc#d#e"; var uri = new uri(webaddress).absoluteuri; 

when that, exception thrown. when include # symbol fragments instead. in example

var webaddress = "https://api.stackexchange.com/2.2/search/advanced?site=stackoverflow&q=[c#] or [java]" var uri = new uri(webaddress).absoluteuri; 

uri equals

https://api.stackexchange.com/2.2/search/advanced?site=stackoverflow&q=[c#]%20or%20[java]

how make

https://api.stackexchange.com/2.2/search/advanced?site=stackoverflow&q=[c#] or [f#]

into

https://api.stackexchange.com/2.2/search/advanced?site=stackoverflow&q=[c%23]%20or%20[f%23]

i'm using .net framework 4.6.2

my approach use uribuilder , dictionary each query parameter. can urlencode value each parameter valid url.

this code like:

var ub = new uribuilder("https", "api.stackexchange.com"); ub.path = "/2.2/search/advanced"; // query string parameters var query = new dictionary<string,string> (); query.add("site", "stackoverflow"); query.add("q", "[c#] or [f#]"); query.add("filter", "!.ue46gejxv)w0gsb"); query.add("page","1"); query.add("pagesize","2");  // iterate on each dictionary item , urlencode value ub.query = string.join("&",     query.select(kv => kv.key + "=" + webutility.urlencode(kv.value)));  var wc = new mywebclient(); wc.downloadstring(ub.uri.absoluteuri).dump("result"); 

this result in url in ub.uri.absoluteuri:

https://api.stackexchange.com/2.2/search/advanced?site=stackoverflow&q=%5bc%23%5d+or+%5bf%23%5d&filter=!.ue46gejxv)w0gsb&page=1&pagesize=2

as stackapi returns content zipped, use automaticdecompression on subclassed webclient (as shown here feroze):

class mywebclient:webclient {    protected override webrequest getwebrequest(uri uri)     {           var wr = base.getwebrequest(uri) httpwebrequest;        wr.automaticdecompression = decompressionmethods.deflate | decompressionmethods.gzip;        return wr;    } } 

which, when combined other code, generates output me:

{     "items" : [{             "tags" : ["c#", "asp.net-mvc", "iis", "web-config"],             "last_activity_date" : 1503056272,             "question_id" : 45712096,             "link" : "https://stackoverflow.com/questions/45712096/can-not-read-web-config-file",             "title" : "can not read web.config file"         }, {             "tags" : ["c#", "xaml", "uwp", "narrator"],             "last_activity_date" : 1503056264,             "question_id" : 45753140,             "link" : "https://stackoverflow.com/questions/45753140/narrator-scan-mode-for-textblock-the-narrator-reads-the-text-properties-twice",             "title" : "narrator. scan mode. textblock narrator reads text properties twice"         }     ] } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -