在我的iPhone应用程序中实现URL缩短服务 [英] Implementing URL Shorteners in my iPhone Application

查看:167
本文介绍了在我的iPhone应用程序中实现URL缩短服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现的网址缩短在我的iPhone应用程序的API?

How do I implement is.gd's URL shortening API in my iPhone Application?

推荐答案

您可以使用CFHTTP API来创建一个HTTP请求。这使您可以方便地调用HTTP GET。用它来发送,其中longurl是URL你想缩短以下要求。

You can use the CFHTTP API to create an HTTP request. This allows you to easily invoke HTTP GET. Use it to send the following request where the longurl is the URL you wish to shorten.

http://is.gd/api.php?longurl=http://www.example.com

您将收到回一个响应头HTTP / 1.1 200 OK如果URL缩短预期,或HTTP / 1.1 500内部服务器错误,如果有一个问题。响应的身体将包含以纯文本缩短的URL,如果一切成功。如果请求不成功,响应的主体将包含一个特定的错误消息。

You will receive back a response header of "HTTP/1.1 200 OK" if the URL was shortened as expected, or "HTTP/1.1 500 Internal Server Error" if there was a problem. The body of the response will contain the shortened URL in plain text if everything was successful. If the request was unsuccessful, the body of the response will contain a specific error message.

您请求可能看起来像这样...

Your request might looking something like this...

CFStringRef requestHeader = CFSTR("Connection");
CFStringRef requestHeaderValue = CFSTR("close");
CFStringRef requestBody = CFSTR("");

CFStringRef url = CFSTR("http://is.gd/api.php?longurl=http://www.example.com");
CFStringRef requestMethod = CFSTR("GET");

CFURLRef requestURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
    requestMethod, requestURL, kCFHTTPVersion1_1);
CFHTTPMessageSetBody(request, requestBody);
CFHTTPMessageSetHeaderFieldValue(request, requestHeader, requestHeaderValue);

CFDataRef serializedRequest = CFHTTPMessageCopySerializedMessage(request);

这篇关于在我的iPhone应用程序中实现URL缩短服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆