I need to create web service in this format in objective c
An example that returns all contacts for a given user might be (where ZmF0aWdhYmxlIGdlbmVyYXR= is a Base64-encoded form of username:password, with the required colon separator):
GET /API/Contacts/username HTTP/1.1
HOST: $baseuri:port
Accept: text/xml
Authorization: Basic ZmF0aWdhYmxlIGdlbmVyYXR=
How to create web service for this format.Please help.
Use NSURLRequest.
NSURL *url = [NSURL urlWithString:#"http://foo.com/API/Contacts/username"];
NSMutableURLRequest *req = [[NSMutableURLRequest] initWithUrl:url];
[req setHTTPMethod:#"GET"];
[req setValue:#"text/xml" forHTTPHeaderField:#"Accept"];
[req setValue:#"Basic ZmF0aWdhYmxlIGdlbmVyYXR=" forHTTPHeaderField:#"Authorization"];
[[[NSURLConnection alloc] initWithRequest:req delegate:self] autorelease];
[req release];
Then, implement the delegate methods documented here to handle the responses.
Related
I used NSURLConnection to communicate with Soap webservice and it worked fine.
Now I am using ASIFormDataRequest and I am not getting is it necessary to create soap message for that? and I am not aware what is SoapAction.
Here is the code I am using
NSURL *url = [NSURL URLWithString:#"http://SERVERNAME.com/WEBSERVICENAME"];
[self setFrmdataReq:[ASIFormDataRequest requestWithURL:url]];
[frmdataReq setRequestMethod:#"POST"];
[frmdataReq addRequestHeader:#"Content-Type" value:#"text/xml; charset=utf-8"];
[frmdataReq setPostValue:txtField.text forKey:#"city"];
[frmdataReq setDelegate:self];
[frmdataReq setDidFinishSelector:#selector(requestFinished:)];
[frmdataReq setDidFailSelector:#selector(requestFailed:)];
[frmdataReq startSynchronous];
UPDATE
Soap Message I used,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\
<soap12:Body>\
<getCityTime xmlns=\"http://www.Nanonull.com/TimeService/\">\
<city>%#</city>\
</getCityTime>\
</soap12:Body>\
</soap12:Envelope>",txtField.text];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapMessage length]];
[request addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn)
{
responseData = [[NSMutableData data]retain];
NSLog(#"connection successful");
}
else
NSLog(#"connection fail.");
I didn't defined SOAPAction as I don't know anything about that.
When using ASIHTTPRequest, you need to create the SOAP request in basically the same way as you do with NSURLConnection.
The setPostValue call in ASIHTTPRequest is for create HTTP POST requests in the same format use for POST <form> tags in HTML.
For comparing ASIHTTPRequest / NSURLConnection, see things like:
ASIHTTPRequest vs NSURLConnection
and "What is ASIHTTPRequest?" on:
http://allseeing-i.com/ASIHTTPRequest/
There are many kinds of parsing methods like NSXML , JSON etc etc.
I am confused with so many methods. So please help me out to choose out of them.
Which will be the best parsing method to be followed if images have to be retrieved from and uploaded to a remote server in an iPhone application??
ASIHTTPRequest to perform asynchronous requests.
JSON Framework to parse incoming JSON objects into native data objects (NSDictionaries and NSArrays).
UIImage *myDownloadedImage = [UIImage imageWithData:[requestObject responseData]]; to turn downloaded image data into a UIImage.
Here's some sample code from one of my recent projects. I've included ASIHTTPRequest.h and JSON.h.
NSString *projurl = [NSString stringWithFormat:#"%#mobile/project_details/?project=%#", WEBAPPURL, self.projectId];
__block ASIHTTPRequest *r = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:projurl]];
[r setCompletionBlock:^{
self.projectData = [[r responseString] JSONValue];
[self.tableView reloadData];
}];
[r startAsynchronous];
You could turn right around in that completion block, pick out image URLs associated with this project, request them, handle the response inside ANOTHER completion block nested inside the first one... The beauty of ASI's new block-oriented result handling is that it can all happen in one place, and you don't have to sweat the details of a formal delegate pattern.
You could put up a "loading" UI element just before the startAsynchronous call, and remove it in the completionBlock, if you wanted.
Generally information coming from server will be in xml or JSON format.
NSXML parser parses xml data and JSON parser parses json data. But image will not be in xml or Json format. Image will be chunks of byte which you have to download from the server.
Generally , image url's can be part of xml or json data which will be parsed using appropriate parser . Once you get the url of image, you will use NSUrlConnection or ASIHttpRequest (library) to download the image.
Use XML to retrive list of images.(Store it on server or get it from web service)
Use NSXmlParser to parse and get image URL.
Use this for getting image. [NSData dataWithContentsOfURL:<#(NSURL *)url#>]
OR
Use following.
NSURL *url = [NSURL URLWithString:[fileUrl
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:#"POST"];
webData = [[NSMutableData alloc] init];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest
delegate:self startImmediately:YES];
Or SOAP code
NSURL *url = [NSURL URLWithString:#"<URL>"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content- Type"];
[theRequest addValue: #"<ADD Value Here>" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest
delegate:self startImmediately:YES];
In above if you are passing URL of file to some web service as request and it returns XML with with file data bound in it then parse it through NSXmlParser and in following method write data in file.
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
write here if you need any further assistance.
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 working in simple application in which I need to post data to the server using webservices. I post string data to the web server, but it gives an error "large amount of binary data will be truncated".
NSString *queryString = [NSString stringWithFormat:#"url/message=%#",strMessage];
queryString = [queryString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:queryString];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:0 forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"GET"];
NSURLConnection *conn =
[[NSURLConnection alloc] initWithRequest:req delegate:self];
Any ideas?
Thanks in advance.
Did you try http://allseeing-i.com/ASIHTTPRequest/ ?
You can post any kind of datas. For example to send an image :
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
[request setPostValue:#"Copsey" forKey:#"last_name"];
[request setFile:#"/Users/ben/Desktop/ben.jpg" forKey:#"photo"];
(from http://allseeing-i.com/ASIHTTPRequest/How-to-use)
Good Luck ! :-)
I'm interested in HTTPS with NSURLConnection. Is it possible to send a request, respond to a challenge and load the secure URL?
Is there anything else that can be done with NSURLConnection via HTTPS?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://monsite/mapage.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[[NSString stringWithFormat:#"score=222"] dataUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
and then implement the appropiate delegate methods for NSURLConnection.
NSUrlConnection works out of the box with HTTPS. You do not have to do anything special.