连接:willSendRequest:redirectResponse 示例项目

发布于 2024-11-02 12:59:16 字数 1174 浏览 0 评论 0原文

请问有人可以给我一份吗?

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-11-09 12:59:16

您可以使用苹果网站上基于异步或同步的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文