How can we set HTTP referer in embedded UIWebView?
I had gone through this but still not got success.
1. In viewDidLoad, I wrote this code,
[objWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"MY_URL"]]];
Here is my code :
- (BOOL) webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType) navigationType
{
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:#"Referer"]!=nil;
if (hasReferer) {
// .. is this my referer?
return YES;
} else {
// relaunch with a modified request
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];
[request setHTTPMethod:#"GET"];
[request setValue:#"Referer link" forHTTPHeaderField:#"Referer"];
[objWebView loadRequest:request];
});
});
return NO;
}
}
2. I had tried this also in viewDidLoad
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"MY_URL"]];
[request setValue:#"Referel URL" forHTTPHeaderField:#"Referer"];
[objWebView loadRequest:request];
But doesn't got success yet.
Please help me solve this problem or tell me is there any problem with this code or not?
Hope I presented question clearly.
You can set Default User-Agent using following code
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:#"Safari/528.16", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
It is well explained here.
I am not sure what exactly is your situation. But for me, simply just setting baseURL parameter in -loadHTMLString:baseURL:; will automatically add HTTP header Referer as baseURL to all outgoing request under the HTML page.
Related
I am using ASIHTTPRequest to download video file from URL in background.
I am displaying the downloads with progress-bar & percentage and I want user can control the downloads like pause & resume.
Below is the code:
-(void)Initiate_Download:(NSString*)urlStr contentID:(NSString*)cid progressBar:(UIProgressView*)progressBar
{
NSLog(#"Initiate_Download for cid:%#",cid);
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
NSString *fileName = [NSString stringWithFormat:#"%#.mp4",cid];
NSString *destinationPath = [[self VideoDownloadFolderPath]stringByAppendingPathComponent:fileName];
[request setDownloadDestinationPath:destinationPath];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:#"%#-part",destinationPath]];
[request setDelegate:self];
NSDictionary *rqstDict = [NSDictionary dictionaryWithObjectsAndKeys:cid,#"cid",urlStr,#"url", nil];
[request setUserInfo:rqstDict];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];
}
//Delegate
- (void)requestStarted:(ASIHTTPRequest *)request1
{
//some code
}
- (void)request:(ASIHTTPRequest *)request1 didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
//some code
}
- (void)requestFinished:(ASIHTTPRequest *)request1
{
//some code
}
- (void)requestFailed:(ASIHTTPRequest *)request1
{
//some code
}
You need to save the URL and destination path of the request for each request and to pause the request use code :-
[request Cancel];
and to resume the request you need to create another request with same URL and destination path. For example :-
ASIHTTPRequest *requestToResume = [ASIHTTPRequest requestWithURL:url];
[requestToResume setTemporaryFileDownloadPath:tempfilePath];
[requestToResume setDownloadDestinationPath:filePath];
[requestToResume setDelegate:self];
[requestToResume setDownloadProgressDelegate:self];
[requestToResume setUserInfo:dictInfo];
// This file has part of the download in it already
[requestToResume setAllowResumeForFileDownloads:YES];
[requestToResume setDidFinishSelector:#selector(requestDone:)];
[requestToResume setDidFailSelector:#selector(requestWentWrong:)];
[requestToResume startAsynchronous];
In the above code we get the url of the song from the dictionary which was set as userInfo of the request and now we get these details for resuming the request. When we resume the request the file will be downloaded from the point it was paused, hence it will solve the purpose of resuming the file download.
I have following JSON for sending request on server
{
"name":"Home",
"latitude":"45.5037078163837",
"longitude":"-122.622699737549",
"radius":"240"
}
and URL of for request is
URL: https://api.geoloqi.com/1/place/create
I am making request like this,
NSString *urlString= #"https://api.geoloqi.com/1/place/create ";
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";
jsonRequest = [self JSONString:jsonRequest];
NSData* requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:#"%d", [requestData length]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestData];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:#"Content-Length"];
[request setTimeoutInterval:30.0];
[url release];
[requestData release];
[requestDataLengthString release];
NSURLConnection *m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];
What is wrong with this request ?
Your JSON string seems to be missing 2 quotes after the longitude and radius values:
Change
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";
To this
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\",\"radius\":\"500\" }";
It is not clear from your question what is your issue with that.
Anyway, are you doing also:
[m_URLConnection start];
after creating the connection?
Alternatively, you could create the connection using
[– initWithRequest:delegate:startImmediately:][1]
which allows you to specify that the loading of data shall start immediately.
Also consider using [+sendAsynchronousRequest:queue:completionHandler:][2] as a convenience constructor for your connection.
The Webservice URL you are trying to hit is seem to be using SSL. You may need to put access token in request header in order to get the proper response from web service.
Please see the link for authentication:
https://developers.geoloqi.com/api/authentication
i want to get the image from https url in iphone:-
The url is like this:-
https://staging.api.botentekoop.nl/image.ashx?ID=804306&imagesize=Normal&imagetype=MainImage
i had done some google work and found the help from this link:-
http://www.iphonedevsdk.com/forum/iphone-sdk-development/5511-load-image-internet.html
http://www.iphonedevsdk.com/forum/iphone-sdk-development/80894-load-image-url.html
But there all example is shown for http not from https
CAn anyone help me.
please how to do this
The URL Loading System on iOS works the same for HTTP and HTTPS URLs. Any example you find that works for an HTTP URL should also work for HTTPS.
you can use ASIHTTPRequest framework
thanks for the reply......
i got the solution.
i had done in this way:-
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[[NSURL URLWithString:strURL] host]];
// [theRequest addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
// [theRequest setHTTPMethod:#"GET"];
NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
if(theConnection) {
self.activeDownload = [[NSMutableData data] retain];
}
else {
NSLog(#"theConnection is NULL");
}
I am developing an app that will request the profile picture URL of some users from Facebook servers, but I don't know how many users I will have (it might be 2 or it might be 20). Should I use ASIHTTPRequest with a loop and a synchronous request, or the API graph (with Facebook SDK for iOS) with a loop?
Trying using ASINetworkQueue. It will allow you to create a queue of ASIHTTPRequests that can still be started asynchronously. For example
- (void)getImages
{
if(!self.queue)
self.queue = [[[ASINetworkQueue alloc] init] autorelease];
NSArray* urlStringsToRequest = [NSArray arrayWithObjects:#"http://www.example.com/image1.png",#"http://www.example.com/image2.png",nil];
for(NSString* urlString in urlStringsToRequest)
{
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[self.queue addOperation:request];
}
[self.queue go];
}
- (void)requestDone:(ASIHTTPRequest*)req
{
UIImage* image = [UIImage imageWithData:[req responseData]];
[imageArray addObject:image];
}
- (void)requestWentWrong:(ASIHTTPRequest*)req
{
NSLog(#"Request returned an error %#",[req error]);
}
I'm using the ASIHTTPRequest package to send some data from the iPhone to my server and then when saved on server I recieve a response (a string) containing a url that I want to load in a webview. In theory this should be simple, but in reality I can't get this work at all. Seems to be some problems with encoding of the string I guess. If I NSLog out the response it seems to be totally fine, but it just refuses to load in the webview.
This is my request (been playing around with compression and encoding):
NSURL *url = [NSURL URLWithString:#"xxx"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setFile:imageData forKey:#"photo"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[request setAllowCompressedResponse:NO];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];//NSISOLatin1StringEncoding
[request start];
And this is my responder:
- (void)requestDone:(ASIHTTPRequest *)request{
NSString *response = [request.responseString];
NSLog(#"web content: %#", response);
[webView loadSite:response];
}
The NSLog seems fine, but site just won't load. Tried sending an NSString variable like NSString *temp = #"http://www.google.se"; to the loadSite function and that works fine, so the problem is not with the loading itself.
Would greatly appriciate any pointers or help in the obj-c jungle :)
/f
Try to do the following:
NSString *response = [[request responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
It will trim all whitespaces and newlines.
so turns out that a response is automatically ended with a \n (new linebreak). this didn't show up when logging the variable, neither when writing it to a textfield but when converting it to a urlobject and logging that i found it. debugging once again pays off ;)
woups, my responder is of course:
- (void)requestDone:(ASIHTTPRequest *)request{
NSString *response = [request responseString];
NSLog(#"web content: %#", response);
[webView loadSite:response];
}