17

我希望能够使用我的 UIWebViewloadRequest方法发送一些额外的标头。

我努力了:

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.reliply.org/tools/requestheaders.php"]];
[req addValue:@"hello" forHTTPHeaderField:@"aHeader"];

[self.theWebView loadRequest:req];

我也尝试过继承UIWebView和拦截该- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType方法。

在那个方法中,我有一个如下所示的代码块:

NSMutableURLRequest *newRequest = [request mutableCopy];
for(NSString *key in [customHeaders allKeys]) {
    [newRequest setValue:[customHeaders valueForKey:key] forHTTPHeaderField:key];
}
[self loadRequest:newRequest];

但由于某些未知原因,它导致 web 视图不加载任何内容(空白框)并且出现错误消息NSURLErrorCancelled (-999)(所有已知的修复程序都不能为我修复它)。

所以我不知道该怎么做。如何将自定义标头与UIWebView请求一起发送?

非常感谢!

4

4 回答 4

19

我发现这是向我的 UIWebView 请求添加标头的方法 - 覆盖此委托方法:

- (BOOL) webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType) navigationType

使用此代码:

BOOL headerIsPresent = [[request allHTTPHeaderFields] objectForKey:@"my custom header"]!=nil;

if(headerIsPresent) return YES;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURL *url = [request URL];
        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

        // set the new headers
        for(NSString *key in [self.customHeaders allKeys]){
            [request addValue:[self.customHeaders objectForKey:key] forHTTPHeaderField:key];
        }

        // reload the request
        [self loadRequest:request];
    });
});
return NO;
于 2012-10-12T09:33:49.277 回答
7

Thomas 的答案不适用于(大多数)具有多个 iFrame 的网页。该解决方案将在整个 UIWebView 上加载 iFrame 请求。例如,如果它为 Google 广告调用 loadRequest。(在一些小的 iFrame 中)advt。在整个 UIWebView 上都加载了,仅此而已。

于 2013-01-09T17:02:34.370 回答
2

我找到了另一种方法,可以使用NSURLProtocol.

-(BOOL)webView:(IMYVKWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSMutableDictionary* mapObject = [NSMutableDictionary dictionary];
    mapObject[@"headers"] = request.allHTTPHeaderFields;
    mapObject[@"navigationType"] = @(navigationType);
    [webViewRequestMap setObject:mapObject forKey:request.URL.absoluteString];
    return YES;
}

webViewRequestMap是静态 NSMutableDictionary*

在您的自定义 NSURLProtocol 代码中:

    @interface IMYCustomURLProtocol : NSURLProtocol
@end
@implementation IMYCustomURLProtocol 
+(void)load
{
     [NSURLProtocol registerClass:self];
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    NSString* urlString = request.URL.absoluteString;
    NSDictionary* dictionary = webViewReuqestMap[urlString];
    if (dictionary)
    {
        [webViewRequestMap removeObjectForKey:urlString];
        if ([request isKindOfClass:[NSMutableURLRequest class]]) {
           [(id)request setValue:@"HAHA" forHTTPHeaderField:@"MeiYou Co.,Ltd"];
        }
    }
    return NO;
}
@end
于 2015-08-15T07:51:51.853 回答
0

这是使用 NSURLProtcol 的完整实现。您可以将此代码放在您喜欢的任何位置(ei 它自己的或添加到现有源文件),它应该可以工作。自定义的两个关键方法是canInitWithRequest:canonicalRequestForRequest:

static NSString * const NSURLProtocolHandledKey = @"NSURLProtocolHandledKey";

@interface WTCURLProtocol : NSURLProtocol<NSURLSessionDelegate>
@property (atomic,strong,readwrite) NSURLSessionDataTask *task;
@property (nonatomic,strong) NSURLSession *session;
@end

@implementation WTCURLProtocol
+(void)load
{
  [NSURLProtocol registerClass:self];
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
  // customize here by returning true for URLs that you want to handle
  return [request.URL.absoluteString hasPrefix:WEB_BASE_URL];
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
  NSMutableURLRequest *newRequest = request.mutableCopy;
  [NSURLProtocol setProperty:@YES forKey:NSURLProtocolHandledKey inRequest:newRequest];

  // customize here by setting your custom headers
  [newRequest setValue:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ" forHTTPHeaderField:@"API-TOKEN"];

  return newRequest;
}

- (void)startLoading
{
  NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];

  self.session  = [NSURLSession sessionWithConfiguration:configure delegate:self delegateQueue:queue];
  self.task = [self.session dataTaskWithRequest:self.request];
  [self.task resume];
}

- (void)stopLoading
{
  [self.session invalidateAndCancel];
  self.session = nil;
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
  if (error != nil) {
    [self.client URLProtocol:self didFailWithError:error];
  }else
  {
    [self.client URLProtocolDidFinishLoading:self];
  }
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
  [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

  completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
  [self.client URLProtocol:self didLoadData:data];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask willCacheResponse:(NSCachedURLResponse *)proposedResponse completionHandler:(void (^)(NSCachedURLResponse * _Nullable))completionHandler
{
  completionHandler(proposedResponse);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)newRequest completionHandler:(void (^)(NSURLRequest *))completionHandler
{
  NSMutableURLRequest *redirectRequest = [newRequest mutableCopy];
  [[self class] removePropertyForKey:NSURLProtocolHandledKey inRequest:redirectRequest];
  [[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:response];

  [self.task cancel];
  [[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]];
}

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
  if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    NSURLCredential *card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,card);
  }
}

@end

于 2019-02-22T03:45:11.200 回答