about uscontact

TinyUrlFactory

Author: zemna
Date: April 17, 2021
Share this:

클래스 명

  • TinyUrlFactory

설명

  • 인터넷 접근주소를 tinyurl.com 사이트를 이용하여 축약한 링크를 반환하는 클래스

제공 함수

  • String CreateShortUrl(String original);
    • original 주소를 tinyurl 주소로 축약하여 반환한다.

[code language="java"] package net.zemna.android.appadviser;

import java.io.*; import java.net.*;

import org.apache.http.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.impl.client.*;

public final class TinyUrlFactory {

// 원본 URL을 TinyUrl.com 사이트를 이용하여 축약한 URL로 변환하여 반환 public static String CreateShortUrl(String original) {

String tinyUrl = null;

try {

HttpClient client = new DefaultHttpClient(); String urlTemplate = "http://tinyurl.com/api-create.php?url={83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}s"; String uri = String.format(urlTemplate, URLEncoder.encode(original)); HttpGet request = new HttpGet(uri); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent();

try { StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == HttpStatus.SC_OK) { String enc = "utf-8"; Reader reader = new InputStreamReader(in, enc); BufferedReader bufferedReader = new BufferedReader(reader); tinyUrl = bufferedReader.readLine(); } else { String errorTemplate = "unexpected response: {83832c2e9762d7d59158bb8e184331dad143c7b73eb69f86229ee6e276689139}d"; String msg = String.format(errorTemplate, statusCode); throw new IOException(msg); } } finally { in.close(); }

} catch (IOException e) { tinyUrl = null; }

return tinyUrl; } } [/code]

Share this:

Leave a Reply

Your email address will not be published.

ZEMNA.NET

Professional software development agency estalished in Indonesia. We offers website setup, web application development, fulfillment service, and etc.
Follow us
Follow us to get latest news and tutorials.
2022 - Copyright, All Rights Reserved, Made by ZEMNA.NET with ❤️
magnifiercrossmenu