Extending a shortened url to the original full url in java

I am trying to take a shortened url and expand it to my original full size url in lowercase format in java. I managed to find the tutorial online, but I can’t get it to get the full URL. Has anyone done this before or knows how to do it? Any help is huge, thanks

URLConnection conn = null; try { URL inputURL = new URL("http://bit.ly/9mglq8"); conn = inputURL.openConnection(); } catch (MalformedURLException e) { } catch (IOException ioe) { } String realU = conn.toString(); Toast.makeText(ImagetestActivity.this, realU, Toast.LENGTH_LONG).show(); 
+4
source share
3 answers
 System.out.println("Short URL: "+ shortURL); urlConn = connectURL(shortURL); urlConn.getHeaderFields(); System.out.println("Original URL: "+ urlConn.getURL()); /* connectURL - This function will take a valid url and return a URL object representing the url address. */ URLConnection connectURL(String strURL) { URLConnection conn =null; try { URL inputURL = new URL(strURL); conn = inputURL.openConnection(); int test = 0; }catch(MalformedURLException e) { System.out.println("Please input a valid URL"); }catch(IOException ioe) { System.out.println("Can not connect to the URL"); } return conn; } 
+7
source

You probably want to use the expand search in the official bit.ly API or the getHeaderField URLConnection method to capture the Location header.

I have never done any of them, and I suppose that no problems will arise with the latter, but I believe that the official method is probably a way to ensure that you get what you need.

+3
source

From the bit.ly specification:

How does a bit work? bitloos works by issuing "301 redirects": a method for creating a web page at many URLs. When you shorten the link with a bit, you redirect the click from the bit address to the destination URL. Redirecting 301 is the most efficient and search engine friendly method of redirecting web pages, and this is what it uses. Since the Beatles do not reuse or modify links, we believe that our redirects are permanent.

This means that you need to take a look at the Location heading to find out where to redirect when you receive a 301 response code. Here is an example of a 301 response:

HTTP / 1.1 301 moved forever Location: http://www.example.org/index.asp

0
source

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


All Articles