Long URLs from Short Using C #

I am using the LongURL.org API to extend short URLs. The great thing about this service is that it returns a long URL, the name of the actual page, and meta information.

The real problem is that getting data takes an excessive amount of time. I am considering moving the request to JavaScript so that the URL is retrieved using the AJAX update panel, the page loads quickly, and these URLs are updated when the user views the content (some search results).

Does anyone know how else I could collect the information described above in the best time frame? I use C # ASP.NET, but I will consider solutions in other languages. Any advice in this area is greatly appreciated.

+3
source share
1 answer

Here I used in the project before ...

private string UrlLengthen(string url)
{
    string newurl = url;

    bool redirecting = true;

    while (redirecting)
    {

        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newurl);
            request.AllowAutoRedirect = false;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 301 || (int)response.StatusCode == 302)
            {
                string uriString = response.Headers["Location"];
                Log.Debug("Redirecting " + newurl + " to " + uriString + " because " + response.StatusCode);
                newurl = uriString;
                // and keep going
            }
            else
            {
                Log.Debug("Not redirecting " + url + " because " + response.StatusCode);
                redirecting = false;
            }
        }
        catch (Exception ex)
        {
            ex.Data.Add("url", newurl);
            Exceptions.ExceptionRecord.ReportWarning(ex); // change this to your own
            redirecting = false;
        }
    }
    return newurl;
}
+4
source

Source: https://habr.com/ru/post/1753051/


All Articles