Since I'm new in iphone I need help in learning about http Connections in Iphone.
Please guide me to some tutorials so that I could learn some http connections tricks in iphone and could communicate with websites.
Also please guide me through some Sample code for the following problem:
I want to send some "Hello world" text to the site and got a reply back from the Web the same word with adiition of s i.e. "Hello Worlds".. please brothers guide me through any sample code.. it's a part of my application that I'm developing in iphone.
You may check this. ASIHTTPRequest package is a highly recommended tool for handling traffic on a iPhone app.
http://allseeing-i.com/ASIHTTPRequest/How-to-use
The best resource is the URL Loading System Programming Guide from Apple. Using NSURLConnection section contains a number of sample codes.
i too new ,but i have sent some msg(soap xml) via http request .my sample code is...
// get host address
NSURL *url = [NSURL URLWithString:#"http://xyz.com"];
NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:url];
// find msg length, here x is my msg
NSString *msgLength = [NSString stringWithFormat:#"%d", [x length]];
// specify type of msg which u have to send to the host(mine is xml)
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
// name space which optional one
[theRequest addValue: #"http://mpack.h.org" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
//type of http method
[theRequest setHTTPMethod:#"POST"];
// msg which is need to send to server
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
// establish connection to host
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
NSLog(#"Connecting....");
}
else
{
NSLog(#"theConnection is NULL");
}
}
// delegate method for NSURLConnection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction %#",error);
[connection release];
[webData release];
}
// u ll get response here
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
}
Related
I have a WCF service:
http://parentportal.technologyorg.com/ParentPortal.svc
which I have to access and get the data. but unfortunately I am getting some HTML code.
my code is as follows
-(IBAction)doit:(id)sender{
NSString *soapMessage = [NSString stringWithFormat:#"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://parentportal.technologyorg.com/ParentPortal.svc\"><SOAP-ENV:Body><GetDictionary></GetDictionary></SOAP-ENV:Body></SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://parentportal.technologyorg.com/ParentPortal.svc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"GetStudentTerms" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",theRequest);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
webData = [NSMutableData data] ;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#"___didReceiveResponse");
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(#"Status code %d", [httpResponse statusCode]);
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"___didReceiveData");
[webData appendData:data];
NSLog(#"%#",webData);
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"___connectionDidFinishLoading");
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *xmlData = [[NSString alloc]
initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
}
I am not sure whats going wrong were if I NSLog it I am the output as some HTML code.
Can any one help me on this.
Try SudzC SOAP-based web service definition WSDL files
Code generation for Objective-C libraries for iPhone development.
Follow the steps below
I am learning parsing in iPhone, When i was searching for the tutorials, i came to know very little bit about SOAP and REST services.
Now i wanted to learn SOAP and REST services.
Do any one have theoretical knowledge of SOAP and REST?
Is there any simple tutorial available for the beginners?
Thanks a lot in advance.
see these links, alongwith comparison of both
http://www.ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest
http://greatgandhi.wordpress.com/2010/06/16/soap-vs-rest-%E2%80%93-the-best-webservice/
http://www.petefreitag.com/item/431.cfm
SOAP GUIDE
http://sudzc.com/
REST GUIDE
http://stackoverflow.com/questions/630306/iphone-rest-client
SOAP Web Service
-(IBAction) read_data
{
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">n"
"<Celsius>%#</Celsius>n"
"</CelsiusToFahrenheit>n"
"</soap:Body>\n"
"</soap:Envelope>\n",name.text
];
NSLog(#"soap Message= %#",soapMessage);
NSURL *url = [NSURL URLWithString:#"your URL "];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"theXML %#",theXML); // Here is your received Value from url
[theXML release];
}
Here is the Example of RESTful WebService
I am using Soap Web services and get response in XML and that Response i am store in NSMutableData and than i perform XML parsing i am using NSXMLParser. my problem is that some times it is works and some times not i can't determine what is the problem so anybody can help me to out of this problem.
I am using this code-
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://MYLINK......." forHTTPHeaderField:#"MY_action"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData_msg = [[NSMutableData data] retain];
}
and getting also response in-
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData_msg appendData:data];
}
but after getting response parse the xml here-
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if( xmlParser_msg)
{
[xmlParser_msg release];
xmlParser_msg=nil;
}
xmlParser_msg = [[NSXMLParser alloc] initWithData: webData_msg];
[xmlParser_msg setDelegate: self];
[xmlParser_msg setShouldResolveExternalEntities: YES];
[xmlParser_msg parse];
[connection release];
[webData_msg release];
}
than problem arise here, some times NSXMLParserDelegate methods call some times not call.
What is going wrong here? please help and sorry for any mistake in English.
The problem was I was getting HTML response through web service so xml purser hangs on that.
It was sometimes errors coming from web service side with HTML content.
May be that should be the connection (or) unsuccessful parsing.You can check that by keeping
if(xmlParser parse)
{
blahblahblah
}
else
{
unsuccesful parsing
}
I am new to iPhone app development. How can I post data and get data by accessing the asmx web service using iPhone app?
I think asmx webservices are SOAP webservices you should read my blog entry here -
http://www.makebetterthings.com/iphone/call-soap-web-service-from-iphone/
To call a SOAP service first i create a string with the SOAP request as follows.
NSString *soapMessage = #"<?xml version="1.0" encoding="utf-8"?>n"
"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n"
"<soap:Body>n"
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">n"
"<Celsius>50</Celsius>n"
"</CelsiusToFahrenheit>n"
"</soap:Body>n"
"</soap:Envelope>n";
After creating the SOAP request I create a NSMutableRequest to send this request to server.
NSURL *url = [NSURL URLWithString:#"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
After firing the request we can collect the XML response in the NSURLConnection’s delegate methods.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"%#",theXML);
[theXML release];
}
After collecting the XML response in theXML string in -(void)connectionDidFinishLoading:(NSURLConnection *)connection
we can parse this string using TBXML for any other XML parser you like.
I try to build an app for user that enter his data and this data will be post to a webserver for save in a Database. This web server returns some data back like Id or something else.
How I can receive the data the webserver returns back?
The transfer works already with NSMutableURLRequest. But I search for a sollution to read the answer from the web server and display it on a label.
Have a look at the delegate methods of NSURLConnection.
Example of how it could be done. Original source
NSString *post = #"key1=val1&key2=val2";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.someurl.com"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *aStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSLog(aStr);
// release the connection, and the data object
[receivedData release];
}