.net - Nuget package for bitly to shorten the links -
i need shorten links using bitly in c#. there nuget package this? can 1 provide me code can use that.
check out https://www.nuget.org/packages/bitlyapi/ or make own call bit.ly api. api easy use , work with.
public string shorten(string longurl, string login, string apikey) { var url = string.format("http://api.bit.ly/shorten?format=json&version=2.0.1&longurl={0}&login={1}&apikey={2}", httputility.urlencode(longurl), login, apikey); httpwebrequest request = (httpwebrequest)webrequest.create(url); try { webresponse response = request.getresponse(); using (stream responsestream = response.getresponsestream()) { streamreader reader = new streamreader(responsestream, encoding.utf8); javascriptserializer js = new javascriptserializer(); dynamic jsonresponse = js.deserialize<dynamic>(reader.readtoend()); string s = jsonresponse["results"][longurl]["shorturl"]; return s; } } catch (webexception ex) { webresponse errorresponse = ex.response; using (stream responsestream = errorresponse.getresponsestream()) { streamreader reader = new streamreader(responsestream, encoding.getencoding("utf-8")); string errortext = reader.readtoend(); // log errortext } throw; } }
you can login , apikey bit.ly going link https://bitly.com/a/your_api_key
Comments
Post a Comment