-(NSString *)getSoap{
NSString *getEnvelopeData = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\n"
"<s:Header>\n"
"<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IHelloWorldService/GetData</a:Action>\n"
"<a:MessageID>urn:uuid:e2c3af4e-5d69-4fd8-94bd-0e0f00976dad</a:MessageID>\n"
"<a:ReplyTo>\n"
"<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>\n"
"</a:ReplyTo>\n"
"</s:Header>\n"
"<s:Body>\n"
"<GetData xmlns=\"http://tempuri.org/\">\n"
"<value>0</value>\n"
"</GetData>\n"
"</s:Body>\n"
"</s:Envelope>\n"];
return getEnvelopeData;
}
-(void)loadSoapRequest{
NSString *soapMessage = [self getSoap];
NSLog(#"soapMessage %#",soapMessage);
NSString *urlString = #"http://l-156024038.testad.com:9000/HelloWorldService.svc";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapMessage length]];
[theRequest setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
[theRequest addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest addValue:#"l-156024038.testad.com" forHTTPHeaderField:#"Host"];
[theRequest addValue:#"GetData" forHTTPHeaderField:#"SOAPAction"];
[theRequest setValue:#"Basic " forHTTPHeaderField:#"Authorization"];
// theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:45];
[theRequest setHTTPMethod:#"GET"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(#"theConnection is NULL");
}
}
//At this point connection is setup and delegate is set to self, now is time to use connection delegate methods. Four methods that we gonna use:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(#"response Value %#",response);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(#"Data Received %#",webData);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(#"Error with connection %#",error);
}
From The code above i am not able to get response from the server!
Its always calling didFailWithError delegate method!
Related
I asked a question and followed following way(suggested by Jaybit):
Pass username and password in URL for authentication
Now, I'm getting this error(stack track):
2013-02-12 11:56:11.734 Calendar[4074:c07] didReceiveData
2013-02-12 11:56:19.519 Calendar[4074:c07] receivedString:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
at System.Xml.XmlReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
--- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /> </soap:Fault></soap:Body></soap:Envelope>
(lldb)
Here is my code:
- (IBAction)btnLoginClick:(id)sender
{
//Call Calendar View
if(self.viewController == nil) {
CalendarViewController *detailView = [[CalendarViewController alloc] initWithNibName:#"CalendarViewController" bundle:nil];
self.viewController = detailView;
}
NSString *userName = [NSString stringWithFormat:#"parameterUser=%#",txtUserName];
NSString *passWord = [NSString stringWithFormat:#"parameterPass=%#",txtPassword];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://URL/service1.asmx?"]];
NSString *postString = [NSString stringWithFormat:#"username=%#&password=%#",userName, passWord];
NSLog(#"%#",userName);
NSLog(#"%#",passWord);
NSData *postData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]];
//URL Requst Object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:600];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody: postData];
appConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.appConnection start];
//tryed with Async...
/* NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"receivedString:%#",receivedString);
}];*/
[self.navigationController pushViewController:self.viewController animated:YES];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
tempdata = data;
NSLog(#"didReceiveData");
if (!self.receivedData){
self.receivedData = [NSMutableData data];
}
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"connectionDidFinishLoading");
NSString *receivedString = [[NSString alloc] initWithData:tempdata encoding:NSUTF8StringEncoding];
NSLog(#"receivedString:%#",receivedString);
}
please help.thank.
I've worked with requesting a soap URL before, the following code worked for me:
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"
"<YourServiceName xmlns=\"http://tempuri.org/\">\n"
"<username>%#</username>\n"
"<passowrd>%#</passowrd>\n"
"</YourServiceName>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", userName, passWord
];
NSURL *url = [NSURL URLWithString:#"http://URL/service1.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: [NSString stringWithFormat:#"http://tempuri.org/%#",service] forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:10];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
responseData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
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 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 am trying to parse soap web service for authentication and following is the code
currentElement = [[NSString alloc] init];
NSString* email=[NSString stringWithFormat:#"sbhushan#gmail.com"];
NSString* password=[NSString stringWithFormat:#"5Pb&Q?4t"];
NSString* soapMesasge=[NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<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/\">"
"<soap:Body>"
"<Authentication xmlns=\"http://tempuri.org/\">"
"<userId>%#</userId>"
"<password>%#</password>"
"</Authentication>"
"</soap:Body>"
"</soap:Envelope>",email,password];
NSURL* url=[NSURL URLWithString:#"http://codeexsolutions.com/HosService/ServiceHOSProvider.asmx"];
NSMutableURLRequest* theRequest=[NSMutableURLRequest requestWithURL:url];
NSString* msgLength=[NSString stringWithFormat:#"%d",[soapMesasge length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"http://tempuri.org/Authentication" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[soapMesasge dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
webData =[[NSMutableData alloc]retain];
}
else
{
// Inform the user that the connection failed.
}
and imlemented all the delegate method of parsing but do not getting call in
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
or not getting any data please help
hi Friend
your method looks OK dont know why not working , u see any data on consol?? try this or take a look on webservices may be data not come from there
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\"><soap:Body><GetClientData xmlns=\"http://tempuri.org/\"><pSearch>%#</pSearch></GetClientData></soap:Body></soap:Envelope>\n", nameInput.text];
NSURL *url = [NSURL URLWithString:#"http://192.168.1.162/iphone/service.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/GetClientData" 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");
}