0

我正在查看 NSURLProtocol 并尝试插入一些标题。

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:kAccessCodeProtocolKey inRequest:newRequest];
    self.connection = [NSURLConnection connectionWithRequest:self.request delegate:self];
}

但我的 startLoading 从未被调用

4

3 回答 3

1

I have a feeling you haven't gone threw a deep search, anyway to add a header use the following snippet

NSURL *URL = [NSURL URLWithString:@"http://example.com/..."];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:30.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
于 2014-03-13T18:58:02.140 回答
1

你实现了 canInitWithRequest: 吗?它返回YES吗?如果没有, startLoading 将永远不会被调用。

于 2014-04-14T08:32:09.863 回答
0

You would normally initialize it with an NSURL instance and NSMutableURLRequest will provide you the

- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field

Method so you can set your headers in this way

于 2014-03-13T18:56:53.830 回答