Guest User

Untitled

a guest
Mar 21st, 2018
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. public static String shorten(String longUrl) {
  2. if (longUrl == null) {
  3. return longUrl;
  4. }
  5.  
  6. StringBuilder sb = null;
  7. String line = null;
  8. String urlStr = longUrl;
  9.  
  10. try {
  11. URL url = new URL("https://www.googleapis.com/urlshortener/v1/url");
  12. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  13. connection.setDoOutput(true);
  14. connection.setRequestMethod("POST");
  15. connection.setRequestProperty("User-Agent", "toolbar");
  16.  
  17. OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
  18. writer.write("url=" + URLEncoder.encode(urlStr, "UTF-8"));
  19. writer.close();
  20.  
  21. BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  22. sb = new StringBuilder();
  23. while ((line = rd.readLine()) != null) {
  24. sb.append(line + 'n');
  25. }
  26.  
  27. String json = sb.toString();
  28. return json.substring(json.indexOf("http"), json.indexOf(""", json.indexOf("http")));
  29. } catch (MalformedURLException e) {
  30. e.printStackTrace();
  31. return longUrl;
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. return longUrl;
  35. }
  36. }
  37.  
  38. [23:30:44 WARN]: java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.googleapis.com/urlshortener/v1/url
  39. [23:30:44 WARN]: at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
  40. [23:30:44 WARN]: at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
  41. [23:30:44 WARN]: at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
  42.  
  43. private static String linkCorrector(String link) throws ClientProtocolException, IOException{
  44. HttpClient client = new DefaultHttpClient();
  45. HttpParams params = client.getParams();
  46. HttpClientParams.setRedirecting(params, false);
  47. HttpGet method = new HttpGet(link);
  48. HttpResponse resp = client.execute(method);
  49. String location = null;
  50. Header h = resp.getLastHeader("Location");
  51. if(h == null || h.getValue() == null){
  52. location = "";
  53. }
  54. else{
  55. location = resp.getLastHeader("Location").getValue();
  56. }
  57. return location;
  58. }
  59.  
  60. import java.io.BufferedReader;
  61. import java.io.IOException;
  62. import java.io.InputStreamReader;
  63. import java.net.URL;
  64. import java.util.HashMap;
  65. import java.util.Map;
  66.  
  67. import javax.net.ssl.HttpsURLConnection;
  68.  
  69. import org.apache.commons.lang.StringUtils;
  70.  
  71. import flexjson.JSONDeserializer;
  72. import flexjson.JSONSerializer;
  73.  
  74.  
  75. public class URLShortnerUtil
  76. {
  77.  
  78. private static final String GOOGLE_SHORTEN_URL = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDLEgrM8I3N3yn8pNhBaZizY"; //replace key's value with your key
  79.  
  80. public static String shortURL(String longURL)
  81. {
  82. String shortURL = "";
  83. HttpsURLConnection con = null;
  84. try
  85. {
  86. Map<String, String> valueMap = new HashMap<>();
  87. valueMap.put("longUrl", longURL);
  88. String requestBody = new JSONSerializer().serialize(valueMap);
  89. con = (HttpsURLConnection) new URL(GOOGLE_SHORTEN_URL).openConnection();
  90. con.setDoOutput(true);
  91. con.setDoInput(true);
  92. con.setRequestMethod("POST");
  93. con.setRequestProperty("Content-Type", "application/json");
  94. con.getOutputStream().write(requestBody.getBytes());
  95. if (con.getResponseCode() == 200)
  96. {
  97. StringBuilder sb = new StringBuilder();
  98. try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())))
  99. {
  100. String line;
  101. while((line = br.readLine()) != null)
  102. {
  103. sb.append(line);
  104. }
  105. Map<String, String> map = new JSONDeserializer<Map<String, String>>().deserialize(sb.toString());
  106.  
  107. if (map != null && StringUtils.isNotEmpty(map.get("id")))
  108. {
  109. shortURL = map.get("id");
  110. return shortURL;
  111. }
  112. }
  113. catch(IOException e)
  114. {
  115. e.printStackTrace();
  116. }
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. e.printStackTrace();
  122. }
  123. return shortURL;
  124.  
  125. }
  126.  
  127. }
Add Comment
Please, Sign In to add comment