Goo.gl을 사용하여 단축 URL 여기에서 json 데이터는 POST 요청으로 http 헤더에 전송됩니다. longUrl
{'longUrl': 'https://www.google.com'}
이 loginUrl에서 URL 방식을 사용하여 리디렉션
송신 변수는 HTTP 헤더 포스트의 요청으로 전송하고 최종적 google.com 줄어듬 URL로서 생성된다.
public void sendPostBody() throws Exception {
String url = "https://www.googleapis.com/urlshortener/v1/url?key=YOUR_GENERATED_KEY"; //Generated your own key on your google account
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("Content-type","application/json");
post.setEntity(new StringEntity("{'longUrl': 'https://www.google.com'}", "UTF8"));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(" The shorten Goo.gl Url is :::: "+result.toString());
}
이 결과는 다음과 같은 형식으로 JSON을 포함
{ "kind": "urlshortener#url", "id": "YourShorternURL", "longUrl": "https://www.google.com"}
귀하를 단축 URL은 JSON에서 "ID"매개 변수입니다.
출처 : http://adityagoyal-java.blogspot.in/2016/09/shorten-url-using-googl-by-http-post.html