I have one user of my iPhone application who is complaining it does not work when using his mobile data connection (but works fine via Wifi). My application make a request to a third party REST API and I use the ASIHTTPRequest library for this.
One parameter of my HTTP request is a username (which is an email address), so I encode the username using the following code:
-(NSString *) encodeString:(NSString *) string
{
NSString * returnString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef) string,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 );
return [returnString autorelease];
}
This then gets built into a URL as follows:
// Setup the url we need to make the request to the sharkscope API
NSString * urlString = [NSString stringWithFormat: #"%SERVICEADDRESSHERE?Username=%#&Password=%#",
encodedUserName, m_md5Key];
NSURL * url = [NSURL URLWithString:urlString];
request = [[ASIHTTPRequest requestWithURL:url] retain];
What get's sent out of my application is a request with the following Username:
Username=Fred.Bloggs%40wanadoo.fr
For this one user, what is received at the server when using his mobile data connection is:
Username=Fred.Bloogs%2540wanadoo.fr
So clearly the % character is being encoded somewhere between me making the call to requestWithURL and it arriving at the server. I can't understand the reason because I have hundreds of other users who can run the requests fine via their data connection.
Do I even need to encode the values passed into requestWithURL?
Does anyone have any idea at what stage the % character is being re-encoded. I'm guessing it must be in ASIHTTP library, but can't work out what circumstances would trigger it in this case.
The corruption is probably happening because of a proxy or transparent proxy on the 3G connection, put in place by the carrier.
Possibly workounds:
Use POST instead of GET
Use https instead of http
Use http on a non-standard port
To prove this theory, you could try asking him to enter the same / similar request in Safari on iOS when he's using the 3g connection.
Related
I am trying to Parse Google Maps api with SBJSon but before parsing when I use the below code gives me the Access Denied errors in the HTML this error comes only when I run on the Device and If I run the same in the simulator ..it gives me the google maps api string..what should be the error for Device running.
NSString *String = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
NSLog(#"string %#",string);
error
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD>....
full Html error is similar of
Setting Proxy Username & password using Objective-C - iPhone
The network your device is connected to does not allow access to the URL. Since you are able to access the URL from your MAC, connect your device to the same network as your MAC. It should work.
Alternate solution, first access some URL from the device Safari browser. If asked for authentication, provide correct username and password. Once connected, do not close the tab or Safari app. Run your application and the URL should now work.
EDIT
If you still face authentication problem, may be you are better off implementing asynchronous downloader which implements NSURLConnectionDelegate. Provide the authentication parameters in method,
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
The API you are using stringWithContentsOfURL:encoding:error: will not give you much flexibility.
Hope that helps!
Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing to the same Quicktime movie. The commented out URL doesn't work; the other one works fine.
I've monitored both via Fiddler and I can't see any issues in headers/etc.
Basically I'm trying to figure out a way to play an Azure hosted media file with some sort of security; either via pass through WCF service. Any one have this figured out? I'm pulling my hair out.
//NSString *moviePath = [[NSString alloc] initWithString:#"http://www.nov8rix.com/Services/CPipeline.svc/Media/42"];
NSString *moviePath = [[NSString alloc] initWithString:#"http://nov8rixstorage.blob.core.windows.net/searchpad/tutorial_portrait.mov"];
NSURL *url = [NSURL URLWithString:moviePath];
[url retain];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:mp];
[mp release];
[url release];
Update: This is the error I'm receiving:
NSConcreteNotification 0x892e540 {name = MPMoviePlayerPlaybackDidFinishNotification; object = <MPMoviePlayerController: 0x8921570>; userInfo = {
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 1;
error = "Error Domain=MediaPlayerErrorDomain Code=-12939 \"The server is not correctly configured.\" UserInfo=0x892ecb0 {NSLocalizedDescription=The server is not correctly configured.}";
A brief search on the web mentions that my problem may be that my media connection doesn't support byte Range Requests. This is probably true. Is there a way to allow Range Requests with WCF?
Yay I finally got this to work! The reason it was not working was because my WCF services didn't support Range Requests. Apparently iPhone Movie streaming requires range requests.
I changed my implementation to make direct requests to the Azure blob storage via SAS URLs. Works so far!
Thanks for posting your answer. I had the same error with my MPMoviePlayerViewController, and my problem was fixed too when I added support for Range Requests to my php mp4 file streamer. Streaming media to the iPHone requires some specific headers, else you get the "The server is not correctly configured" error.
I found some great hints for php Range Requests support here:
fread, (PHP 4, PHP 5), fread — Binary-safe file read
I am planning to make an iPhone search app. the user types in the search string. the string will be searched by some search engines like Google, Live, Yahoo ...
I need to get the search result in the XML format. Is there any way to do this. Help needed. Please.
Thanks and regards,
Shibin
A RESTful search request to Google AJAX returns a response in JSON format. JSON is a like a very highly stripped-down version of XML.
Google doesn't make its SOAP interface available any longer, so I don't know if you'll be able to get XML from them, at least through a public interface. Luckily for you, JSON responses are trivial to request and to parse on the iPhone.
You can issue the request with ASIHTTPRequest and parse the JSON-formatted response on an iPhone with json-framework.
For example, to create and submit a search request that is based on the example on the Google AJAX page, you could use ASIHTTPRequest's -requestWithURL and -startSynchronous methods:
NSURL *searchURL = [NSURL URLWithString:#"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton"];
ASIHTTPRequest *googleRequest = [ASIHTTPRequest requestWithURL:searchURL];
[googleRequest addRequestHeader:#"Referer" value:[self deviceIPAddress]];
[googleRequest startSynchronous];
You would build the NSURL instance based on your search terms, escaping the request parameters.
If I followed Google's example to the letter, I would also add an API key to this URL. Google asks that you use an API key for REST searches. You should sign up for an API key over here and add it to your requests.
You should also specify the referer IP address in the request header, which in this case would be the local IP address of the iPhone, e.g.:
- (NSString *) deviceIPAddress {
char iphoneIP[255];
strcpy(iphoneIP,"127.0.0.1"); // if everything fails
NSHost *myHost = [NSHost currentHost];
if (myHost) {
NSString *address = [myHost address];
if (address)
strcpy(iphoneIP, [address cStringUsingEncoding:NSUTF8StringEncoding]);
}
return [NSString stringWithFormat:#"%s",iphoneIP];
}
There are also asynchronous request methods which are detailed in the ASIHTTPRequest documentation. You would use those to keep the iPhone UI from getting tied up while the search request is made.
In any case, once you have Google's JSON-formatted response in hand, you can use the json-framework SBJSON parser object to parse the response into an NSDictionary object:
NSError *requestError = [googleRequest error];
if (!requestError) {
SBJSON *jsonParser = [[SBJSON alloc] init];
NSString *googleResponse = [googleRequest responseString];
NSDictionary *searchResults = [jsonParser objectWithString:googleResponse error:nil];
[jsonParser release];
// do stuff with searchResults...
}
There are different web service API's available. I would recommend that you use those.
Google Search API: http://code.google.com/intl/sv-SE/apis/ajaxsearch/web.html
Google JS API's often return JSON. But that's easy to work with too. You should easily be able to transform the JSON to XML if needed.
I'm trying to track an event in my app using Yahoo Web Analytics. The code I am using looks like
ASIHTTPRequest *yahooTrack = [ASIHTTPRequest requestWithURL:
[NSURL URLWithString:#"http://s.analytics.yahoo.com/p.pl?a=xxxxxxxxxxxxx&js=no&b=yyyyyyyyyyyy&cf6=zzzzzzzzzzz"]];
yahooTrack.didFinishSelector = #selector(statisticsFinished:);
yahooTrack.delegate = self;
[yahooTrack startAsynchronous];
Then the statisticsFinished looks like:
NSLog(#"Cookies: %#", request.requestCookies);
NSLog(#"Redircount: %d", [request redirectCount]);
NSLog(#"Responsecode %d %#\nMsg: %#", request.responseStatusCode,
request.responseStatusMessage, [request responseString]);
And all the information I get back looks correct. Cookies are set, redirectcount is 1 the first time (as it redirects to s.analytics.yahoo.com/itr.pl?.... a normal browser does). Then the redirectcount is 0 for subsequent request until the app is restarted and session cleared. The responseString returns GIF89a.
Even if the data looks correct, Yahoo still won't track. As soon as I call the tracking url directly in my browser it works as expected.
I realize Flurry is a better option, but I'm forced to use Yahoo in this case. Also, using a UIWebView probably would work, but I'm against putting in a webview just for tracking purposes.
Is there any difference in how ASIHTTPRequest and Safari would handle a call to a simple URL as this? Or do you see anything else that could explain why the tracking isn't working?
I finally found the problem. ASIHTTPRequest creates a user-agent based on your applications name, and requests from this user agent is ignored by Yahoo somehow (bug?). As stated in the documentation, you can override the user-agent as follows:
[request addRequestHeader:#"User-Agent" value:#"My-User-Agent-1.0"];
I used the user-agent string of Safari on iPhone, and it worked immediately! BTW; the same problem applies for Android, and the same fix works.
I have a cocoa class set up that I want to use to connect to a RESTful web service I'm building. I have decided to use HTTP Basic Authentication on my PHP backend like so…
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
//Stuff that users will see if they click 'Cancel'
exit;
}
else {
//Validation Code
echo "You entered info.";
}
?>
At this point I'm using a synchronous NSURLConnection, which I understand the Apple documentation states has less support for Authentication.
But is it even possible at all? I can do cookie authentication very easily sans NSURLProtectionSpaces or NSURLCredentials or any of the authentication classes. Also, are there any resources where I can read more about the Cocoa Authentication classes?
Thanks.
UPDATE: mikeabdullahuk
The code you supplied (the second example) is almost identical to what I had written. I have done some more investigating, and discovered that the NSURLConnection is returning an error…
Error Domain=NSURLErrorDomain Code=-1012 UserInfo=0x1a5170 "Operation could not be completed. (NSURLErrorDomain error -1012.)"
The code corresponds to NSURLErrorUserCancelledAuthentication. So apparently my code is not accessing the NSURLCredentialStorage and instead is canceling the authentication. Could this have anything to do with the PHP HTTP Authentication functions? I'm quite confused at this point.
A synchronous NSURLConnection will absolutely work with NSURLCredentialStorage. Here's how things usually work:
NSURLConnection requests the page from the server
The server replies with a 401 response
NSURLConnection looks to see what credentials it can glean from the URL
If the URL did not provide full credentials (username and password), NSURLConnection will also consult NSURLCredentialStorage to fill in the gaps
If full credentials have still not been determined, NSURLConnection will send the -connection:didReceiveAuthenticationChallenge: delegate method asking for credentials
If the NSURLConnection now finally has full credentials, it retries the original request including authorization data.
By using the synchronous connection method, you only lose out on step 5, the ability to provide custom authentication. So, you can either pre-provide authentication credentials in the URL, or place them in NSURLCredentialStorage before sending the request. e.g.
NSURLRequest *request =
[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://user:pass#example.com"]];
[NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
or:
NSURLCredential *credential = [NSURLCredential credentialWithUser:#"user"
password:#"pass"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:#"example.com"
port:0
protocol:#"http"
realm:nil
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
[protectionSpace release];
NSURLRequest *request =
[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com"]];
[NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
In a situation where a 401 or other authentication challenge is unacceptable/impossible, I sometimes use a dummy CFHTTPMessage to generate the authetication line, then copy that back into the NSURLRequest:
// assume NSString *username and *password exist and NSURLRequest *urlRequest
// exists and is fully configured except for HTTP Basic Authentication..
CFHTTPMessageRef dummyRequest =
CFHTTPMessageCreateRequest(
kCFAllocatorDefault,
CFSTR("GET"),
(CFURLRef)[urlRequest URL],
kCFHTTPVersion1_1);
CFHTTPMessageAddAuthentication(
dummyRequest,
nil,
(CFStringRef)username,
(CFStringRef)password,
kCFHTTPAuthenticationSchemeBasic,
FALSE);
authorizationString =
(NSString *)CFHTTPMessageCopyHeaderFieldValue(
dummyRequest,
CFSTR("Authorization"));
CFRelease(dummyRequest);
[urlRequest setValue:authorizationString forHTTPHeaderField:#"Authorization"];
This may seem completely a bizarre way to do it but it is tolerant of situations where the username/password aren't URL clean and where NSURLRequest refuses to consult the NSURLCredentialStorage because the server isn't actually sending a HTTP 401 (for example it sends a regular page instead).
I would note mikeabdullahuk's answer is good but also if you use NSURLCredentialPersistencePermanent instead of per session it will store the credentials in the users keychain so next time you can check NSURLCredentialStorage for a non nil value for the default credentials for a protection space and if you get a non nil value you can just pass the credentials in. I am using this method right now for a delicious.com client I am writing and it works very well in my tests.
Set your credential as the default credential for the protectionspace:
// Permananent, session, whatever.
NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence: NSURLCredentialPersistencePermanent];
// Make sure that if the server you are accessing presents a realm, you set it here.
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:#"blah.com" port:0 protocol:#"http" realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
// Store it
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
At this point, any subsequent NSURLConnection that is challenged using a protection space that matches what you set will use this credential