C#url shortener

Asked

Viewed 1,450 times

2

I’m adjusting a code in c# for a url shortener api to send in the sms inside an application, it’s all working, but the out-of-code api works, but within the code the Return comes back nothing, I’m not very experienced in c#, can anyone help me? Follows 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 "";
        }
  • You are replacing this {APIKEY} with the API key somewhere before making the request?

2 answers

2

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

        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 obtained was:

http://tw.gs/4Yudb2

Editing

To pass the url by parameter, do so:

string urlMigreMe = string.Format("http://tiny-url.info/api/v1/create?url=
                                   {0}&apikey={APIKEY}&provider=bit_ly", URL AQUI);
  • So I want it to shorten a variable url which is a link with the id of a pdf generator, each time is generated a different url, looks like it is:

  • Helpers.Encurtadorurl(string. Format("http://www.agafcomunicacao.com.br/vectradental/GerarPdf/Index?exame={0}", exame.Id); new Enviosms(Parametrogeralservice). Enviasmsexemplo(", """, patient, string.Format("Vectra- Your Dentist requested the exam {1} - Call {0} to schedule." , odontomedica, url), """); Httpresponsemessage responseNaoExist = Request.Createresponse(Httpstatuscode.OK, new { Message = "Registered Exam!" }); Return Task.Fromresult(responseNaoExist);

  • @Augustofelipe I don’t understand. URL’s are different?

  • this, the application generates whenever requested a different url in agafcommunication.com.br/ vectradental/Gerarpd f/Index? exam={0}, in case only in {0} that is the id of the pdf that is different for each request.

  • @Augustofelipe So. You have to make sure that you are passing a valid url to the API. Note that in your code you are using ...create?url={0}, but at no time changes the {0} by the real value of the url

  • It is possible to make this call to a variable url in C#, sorry but I do not have as much knowledge in C#.

  • @Augustofelipe See the edit. You only need to pass the value of the URL with a comma in the string.Format()

Show 2 more comments

2


Wrong or improper use of the method class format String that is wrong: when you want to format a string, put the numbering of 0 until the amount in case there are two, then, {0} and {1}

See code editing:

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

  • But what about { Return ""; } is this result that I need! And it doesn’t give me any results.

  • Wait a minute, if it does not return a data, because there is an error: debug the error to fix something in the code! Do you understand, tell me what the mistake is? I edit it and help you visualize it. If an error occurs, you may have a mechanism to resolve the problem!

  • So I need Sponse to return the shortened url to me, it does not show error but also becomes empty, with no data.

  • I just performed the tests and create an apiKey and it worked! you put your Apikey there?

  • Yes, this api is part of texting an app, in case it shortens a link to view a pdf, I can’t find the error, but I know it’s in the api, because even doing a test and putting some value in Return " "it shows me in sms, but the way I intend it shows nothing, it’s empty.

  • You would like to use another short url generation browser?

  • Actually I’m wearing this because it’s the only one I know in C#, that might be the problem?

  • @Augustofelipe the url was wrong take a look now it will generate the url correctly and you put the result in your browser it will load the page!

  • I made a change to the code @Augustofelipe use it for your tests!

  • 1

    Thanks! Thank you very much, it worked out here! :)

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.