[webkit-dev] Add HTTP headers to WebView URL request with and without redirection
David Kilzer
ddkilzer at webkit.org
Thu Oct 9 02:18:27 PDT 2008
If you want to do it for every request inside a WebView, why not just implement the resource load delegate method this way:
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
{
NSMutableURLRequest *mrequest = [request mutableCopy];
if (mrequest) {
[self AppendHeaders:mrequest];
return [mrequest autorelease];
}
NSLog(@"Could not make mutable copy of %@", [request description]);
return request;
}
Dave
On Wed, 10/8/08, Bill Patterson <billpatt at aol.com> wrote:
> I am needing to add HTTP headers to any URL request from a
> WebView.
>
> I've got it working for direct URLs, but redirect URLs
> are being missed.
>
> I've tried spawning a NSURLConnection and catching the
> redirected URL
> there, and that works, but I can't seem to stop the
> original WebView
> request, so I get multiple URL requests.
>
> Here's the code I've tried:
>
> -(NSURLRequest *)webView:(WebView *)sender
> resource:(id)identifier
> willSendRequest:(NSURLRequest *)request redirectResponse:
> (NSURLResponse *)redirectResponse
> fromDataSource:(WebDataSource
> *)dataSource
> {
> // Update the status message
> @try
> {
> if (redirectResponse)
> {
> NSMutableURLRequest *mrequest = [request
> mutableCopy];
> myConnection = [[NSURLConnection alloc]
> initWithRequest:mrequest
> delegate:self];
> // attempt to cause this original request to be
> cancelled
> //[[webView mainFrame] stopLoading]; // when
> uncommented, this
> results in a crash
> return mrequest;
> } else
> {
> NSMutableURLRequest *mrequest = [request
> mutableCopy];
>
> if (mrequest)
> {
> [mrequest retain];
> [self AppendHeaders:mrequest];
> return mrequest;
> }
> }
> return request;
> }
> @catch (id exception)
> {
> return request;
> }
> return request;
> }
>
> - (NSURLRequest *)connection:(NSURLConnection *)connection
> willSendRequest:(NSURLRequest
> *)redirectRequest
> redirectResponse:(NSURLResponse
> *)redirectResponse
> {
>
> if (redirectRequest && redirectResponse) {
> NSMutableURLRequest *mrequest = [redirectRequest
> mutableCopy];
>
> if (mrequest)
> {
> [mrequest retain];
> [self AppendHeaders:mrequest];
> return mrequest;
> }
> }
> return redirectRequest;
> }
>
> Using Charles, I can see the redirected URL being loaded.
> It loads 3
> times with the code above, the first time with the correct
> headers.
> I need it to just load once. Without the NSURLConnection
> initWithRequest it loads once, but without the headers.
>
> Any suggestions would be greatly appreciated.
>
> Bill Patterson
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
More information about the webkit-dev
mailing list