Shortcuts url C # [closed]

2

I'm setting a code in C # for a url shortener api to send in sms inside an application, everything is working, however the api outside of the code works, but inside the code oo return nothing comes back, I'm not much Experienced in C #, can anyone help me? Here is the code:

public static class Helpers
{
    public static string EncurtadorUrl(string url)
    {
        try
        {
            string urlMigreMe = string.Format("http://tiny-url.info/api/v1/create?url={0}&apikey={APIKEY}chave_aqui&provider=bit_ly");

            var client = new WebClient();
            string response = client.DownloadString(urlMigreMe);

            return response;
        }
        catch 

        {
            return "";
        }
    
asked by anonymous 13.09.2016 / 21:12

2 answers

2

Wrong or improper use of the format of class String that is wrong: when you want to format a string , place the numbering of 0 up to quantity

See editing the code:

public static class Helpers
{
    public static string EncurtadorUrl(string url)
    {
        try
        {                
            string urlMigreMe = string.Format("http://tinyurl.com/api-create.php?url={0}", url);

            var client = new WebClient();

            string response = client.DownloadString(urlMigreMe);

            client.Dispose();

            return response;
        }
        catch (WebException ex)
        {
            throw ex;
        }
    }

    public static string EncurtadorUrl(Uri url)
    {
        return EncurtadorUrl(url.AbsoluteUri);
    }
}
Way to use:

class Program
{
     static void Main(string[] args)
     {
          string url = Helpers.EncurtadorUrl(new Uri("http://www.uol.com.br"));
          //dado contido na variavel url coloque no navegador vai abrir a pagina
     }
}

String.Format Method

    
13.09.2016 / 22:03
2

The code is working perfectly. Just make sure you pass a valid URL, such as:

        string urlMigreMe = string.Format("http://tiny-url.info/api/v1/create?url=http://google.com&apikey=APIKEYAQUI&provider=bit_ly");

        var client = new WebClient();
        string response = client.DownloadString(urlMigreMe);

In this example using the google url, the return value was:

  

link

Issue

To pass the url by parameter, do this:

string urlMigreMe = string.Format("http://tiny-url.info/api/v1/create?url=
                                   {0}&apikey={APIKEY}&provider=bit_ly", URL AQUI);
    
13.09.2016 / 21:18