NSURLConnection redirected on iOS device, but not on simulator - iphone

When I make a request to a particular website, I get the XML response as desired on the simulator but I get a redirect page on the device. I think this is because it is detecting that I am using a mobile browser (similar results occur through Mobile Safari), but I'm setting the user agent string of the request to my laptop browser's UA and blanking the rest (I've also tried setting just the UA):
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setAllHTTPHeaderFields:[NSDictionary dictionaryWithObject:#"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11" forKey:#"User-Agent"]];
NSURLConnection* _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
How could the website still be detecting that I'm using a mobile browser? The same GET works through telnet with zero information, which implies that the default response is the desktop version.

Turns out Mobile Safari will automatically redirect/renegotiate http->https in the simulator, but won't do it on the device.

Related

Https request is not working in iPhone device

I have to load all the required details from server in my application. eg; I wants to load countries detail from the following URL.
https://sub.domain.com//members/phone/XML_list_countries.php
But I am getting following error. But the same request working in Simulator.
ERROR
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x3b8d10 {NSErrorFailingURLStringKey=https://secure.nymgo.com//members/softphone/XML_list_countries.php, NSErrorFailingURLKey=https://secure.nymgo.com//members/softphone/XML_list_countries.php, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x3b5090 "The request timed out."}
CODE
NSURL *url=[NSURL URLWithString:#"https://secure.nymgo.com//members/softphone/XML_list_countries.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
NSURLConnection *_connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
if(!_connection){
NSLog(#"Can not make this request.");
}
I have implemented all the required delegate methods.
NOTE: My device is working in Wifi with proxy settings.
I don't know what is going wrong. Please help me to resolve this issue.
Have you Implement :
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
Also Refer this SO post.
You can use the ASIHTTPRequest framework also.
I have tested the same code in different iPhone 3G and iPhone 4 devices. it is working fine.
NOTE: I searched lot in google. I got lot of result. One of the result is like "some iPhone 3G is not supporting for HTTPS. So I decided to test it in different devices.

Upload image from iphone to facebook without dialog

i want to send image from my iphone to facebook of my account without showing any dialog (in fbconnect there is dialog) . I want a simple way to just test whether it is uploaded in the facebook are not.
I tested with twitter api send message from my iphone and its work fine its few lines of code,
I pasted below
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://username:password#twitter.com/statuses/update.xml"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval: 60.0];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[[NSString stringWithFormat:#"status=%#", #"test message sends from iphone"] dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSLog(#"%#", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
Like wise i want to send text and photo to facebook account ? Is it possible ?
The fbconnect is more no of blocks and lines , can anyone help me ? a simple way to understand it ?
Thanks in advance
With the new OAuth Facebook API and the new facebook-ios-sdk I don't think you can do without the dialog. The whole point is that your app never sees and stores the user credentials. If you then close the app and the login dialog appears again a cookie (set/used by the UIWebView, AFAIK) logs you in without you needing to enter the credentials again.

Tracking iPhone on Yahoo Web Analytics using ASIHTTPRequest

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.

Can I log in to my site from my iPhone app?

I'm trying to make a log in or sign up feature for my web site in my iPhone app. My website is a content management system, and like any other CMS, it has log in and registration features. It also has permmissions, dependent on the user account. I think I would have to use UIWebView for this.
Are there any examples or tutorials I can examine?
Check out the documentation for NSURLRequest (and NSMutableURLRequest): you can use it to make a POST request to your login and registration pages, just like a web browser. You can write the form UI in Cocoa/Objective-C and then send the data to the server.
As far as displaying the result to the user, you'll have to figure out a way to either parse the returned HTML (bad idea) or modify your CMS to return JSON or XML to iPhone requests (better idea).
Edit: Here's some sample code, taken from an app I'm working on (it submits data to Last.fm using POST):
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *str = #"This is my example data!";
// everything below here is directly from my app:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:kLastFMClientUserAgent forHTTPHeaderField:#"User-Agent"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPShouldHandleCookies:NO];
*connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];

Spoofing the user agent of an embedded Safari browser on the iPhone?

Is there any way to spoof the user agent on Safari on the iPhone?
So for example, you would create an application on the iPhone that has the embedded Safari browser, however any website the user visits with this browser wouldn't know you were on Safari on the iPhone, it would think you are on something like Safari on a PC, or even IE/FireFox.
Thanks
Yes I think you could change this. It would require a bit of a work around to get it working.
You would need to manually manage all requests. By making your own data requests.
In this data request you can add a HTTPheader for User-Agent which will override the default headers.
NSMutableURLRequest* urlRequest = [[[NSMutableURLRequest alloc] initWithURL:requestURL] autorelease];
[urlRequest setHTTPMethod: #"POST"];
[urlRequest setHTTPBody: [nvpString dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest addValue:#"Your+User+Agent+String" forHTTPHeaderField:#"User-Agent"];
receivedData = [[NSMutableData alloc] retain];
[receivedData setLength:0];
[NSURLConnection connectionWithRequest: urlRequest delegate: self];
If you embed the Safari Web Browser in your app you can subscribe to its delegate methods. One of them will notify your application that safari would like to load a URL, this is where you catch this load and get the data your self.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
now you put your code in here to do the data load.
Once the data has loaded. Give it the data string back to the webView. I have set "baseURL:nil" but you might have to correctly set this to maybe the correct domain for this app.
[webView loadHTMLString:newString baseURL:nil]