I am working on an iPhone project which need to connect to the IIS server over HTTPS or SSL protocol. Can anyone tell me how to implement HTTPS connection in iPhone? Any advice would be appreciate.
Thank you in advance.
Ben Copsey's ASIHTTPRequest framework makes it trivially easy to issue web requests over SSL.
Modifying the site's example slightly:
NSURL *url = [NSURL URLWithString:#"https://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog (#"%#", response);
}
Just use NSURLConnection with an NSURLRequest pointing to an NUSUL with https protocol, just like that:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://mySecureServer.net"]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
//do something with response & data
}
But I suggest you have a look at the documentation of the URL loading system in Cocoa.
Jason don't forget to implement the connection:canAuthenticateAgainsProtectionSpace and connection:didReceiveAuthenticationChallenge to make sure you're handling the challenges for the Server authentication and Client Authentication:
check out this post that will help you with this https://devforums.apple.com/message/143091#143091
You probably don't have a public certificate. Can you access the https resource with firefox/safari/chrome without an error? if not - that's the problem, not your code.
Related
I am trying to get data from WebService for my Ipad App.
To do that, I am using a NSURLConnection with NSMutableURLRequest.
Web Services are published on Apache-Coyote 1.1 with Preemptive Basic Authentication, but I don't know how to send my credentials from Objective-C.
Anybody knows how can I set my user/password in Objective-C to log my clients with the Apache premptive authentication system?
Thank you.
EDIT:
In order to set the creds yourself, you'll need to be able to base64 encode the username and password and set the appropriate header. Matt Gallagher, from Cocoa With Love, has a great post on how to add a category to NSData to easily do this.
NSString* username = #"username";
NSString* password = #"password";
NSString* encodedUsername = [[username dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString* encodedPassword = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSURL* url = [NSURL URLWithString:#"http://yourUrl.com/"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
NSString* headerValue = [NSString stringWithFormat:#"Basic %#:%#", encodedUsername, encodedPassowrd];
[request addValue:#"Authorization" forHTTPHeaderField:headerValue];
[NSURLConnection connectionWithRequest:request delegate:self];
As with all use of credentials, please make sure you are doing this all over HTTPS because these credentials are essentially being passed in clear text.
Consider using the ASIHTTPRequest framework, which makes preemptive Basic authentication requests simple:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; /** preemptively present credentials **/
[request setUsername:#"username"];
[request setPassword:#"password"];
[request startSynchronous];
NSError *error = [request error];
if (!error)
NSString *response = [request responseString];
And, yes, you definitely want to do Basic authentication over SSL (i.e. via some https://... URL).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sending post data from Iphone
Hello everyone, I'm a new member of the forum and browsing the web I found what was right for me, or rather being a novice I did not understand what poker was.
What I'm trying to do is go to my iphone from a string php server, example: site.com/site.php?val1=1&val2=2 and that the server will recognize it and give me back a xml, but one thing at a time how do I send the string to the server by pressing a button IBAction?
This is what I usually do for that:
NSString *reqURL = [NSString stringWithFormat:#"http://site.com/site.php?val1=%#&val2=%#",var1,var2];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
Readup in NSURLConnection or use ASIHttpRequest (which is easier to use).
get ASIHTTPRequest from http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
and
import
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
and do following to get response
NSURL *url= [NSURL URLWithString:#"http://site.com/site.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request setPostValue:#"1" forKey:#"val1"];
request setPostValue:#"2" forKey:#"val2"];
[request startSynchronous];
NSString *retVal = nil;
NSError *error = [request error];
if (!error) {
retVal = [request responseString];
}
here you got data on retVal
I have application in which data are downloaded using wifi or 3G mostly...
Sometimes wifi goes down and unable to download data from server.
I have function of checking if internet available by
- (BOOL) connectedToNetwork
{
NSString *requestString =#"";
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:#"http://www.google.com"]];
[request setHTTPMethod: #"POST"];
[request setTimeoutInterval:10];
[request setHTTPBody: requestData];
return [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]!=nil;
}
and i got return TRUE sometimes though low connection..
How to solve this issue?
Your suggestions are very helpful to me.
Thanks in advance
for my application I use Reachability class from this Apple example. hope it helps )
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
I would also recommend ASIHTTPRequest, which can help a lot with data downloading.
you must look into apple Reachability API's
Blog tutorial :
http://www.raddonline.com/blogs/geek-journal/iphone-sdk-testing-network-reachability/
http://www.xprogress.com/post-40-iphone-internet-connection-check-wifi-3g-edge-something-like-reachability-h/
I am trying to write a code that will grab some information provided by my server (remote), for example request a url that will return data that will be represented in the application after parsing it.
I've been trying since 2 days now I googled it i found some incomplete solution but nothing really worked out for me
I am really noob in Xcode and Objective-C
Thanks
Click for the URL-Loading documentation provided by Apple.
Especially Using NSURLConnection looks interesting for you.
Edit:
Another very good and easy-to-use Framework for this task is ASIHTTP:
Click
The easiest way:
- (void)grabURL
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
Asynchronous loading is only slightly more complex:
- (void)grabURLInBackground
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
When making the following HTTP POST request:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLResponse *urlResponse = nil;
NSError *error = nil;
// execute
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
if(responseData)
{
//blah
}
I get back a valid response when connected via WiFi, but not when connected over 3G. The responseData object doesn't even get made (0x0) when coming back over 3G.
I get kCFErrorDomainCFNetwork error 303.
The response ought to be 242k of JSON.
Any help would be greatly appreciated.
Thanks.
It seemed that the problem was between the back end system and the mobile networks. Changing the header information to text format only solved the issue.