簡體   English   中英

使用UIWebView loadRequest發送自定義標頭

[英]Send custom headers with UIWebView loadRequest

我希望能夠使用我的UIWebView loadRequest方法發送一些額外的標頭。

我試過了:

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請求?

非常感謝!

我發現這是向我的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;

托馬斯的答案不適用於具有多個iFrame的(大多數)網頁。 該解決方案將在完整的UIWebView上加載iFrame請求。 例如,如果它為Google推薦調用loadRequest。 (這是在一些小的iFrame中)的建議。 在整個UIWebView上加載,沒有別的。

我找到另一種方法,可以使用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

這是使用NSURLProticol的完整實現。 您可以將此代碼放在任何您喜歡的地方(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM