连接:willSendRequest:redirectResponse 示例项目
请问有人可以给我一份吗?
.h
#import "UntitledViewController.h"
@implementation UntitledViewController
- (id)init
{
self = [super init];
if (self)
{
NSURL *url = [NSURL URLWithString:@"http://tinyurl.com/a3cx"];
[self loadTinyURL:url];
}
return self;
}
- (void)loadTinyURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (!connection)
NSLog(@"could not connect with: %@", url);
else {
NSLog(@"this works");
}
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
// http statuscodes between 300 & 400 is a redirect ...
if (response && statusCode >= 300 && statusCode < 400)
NSLog(@"redirecting to : %@", [request URL]);
return request;
Could anyone provide me with one please?
.h
#import "UntitledViewController.h"
@implementation UntitledViewController
- (id)init
{
self = [super init];
if (self)
{
NSURL *url = [NSURL URLWithString:@"http://tinyurl.com/a3cx"];
[self loadTinyURL:url];
}
return self;
}
- (void)loadTinyURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (!connection)
NSLog(@"could not connect with: %@", url);
else {
NSLog(@"this works");
}
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode];
// http statuscodes between 300 & 400 is a redirect ...
if (response && statusCode >= 300 && statusCode < 400)
NSLog(@"redirecting to : %@", [request URL]);
return request;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用苹果网站上基于异步或同步的 NSURLConnection 示例代码。您可以通过stackoverflow 链接
you can use NSURLConnection samplecode based on either asynchronous or synchronous from apple website.you can know why we use this delegate method through the stackoverflow link