soap web service parsing - iphone

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");
}

Related

error while making URL request iPhone

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");
}

"Root element is missing" Soap Request in iOS

I have SOAP webservice and I tried connect it. But SOAP response contains that "System.Xml.XmlException: Root element is missing."
My obj-c code. What's wrong?
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>"
"<Method1 xmlns=\"http://tempuri.org/\">"
"<param1>string</param1>"
"</Method1>"
"</soap:Body>"
"</soap:Envelope>"];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"web service 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: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest addValue: #"\"http://tempuri.org/Method1\"" forHTTPHeaderField:#"SOAPAction"];
[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");
}
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>"
"<Method1 xmlns=\"http://tempuri.org/\">"
"<param1>%#</param1>"
"</Method1>"
"</soap:Body>"
"</soap:Envelope>",str];
Str here is a string that you need to pass for param1.
Hope This will work for you.
you need to remove the extra quotations from:
[theRequest addValue: #"\"http://tempuri.org/Method1\"" forHTTPHeaderField:#"SOAPAction"];
it should read:
[theRequest addValue: #"http://tempuri.org/Method1" forHTTPHeaderField:#"SOAPAction"];
hope that helps.

how to create soap message

I am doing with soap request/response app. I have login page.That have user id and password. This is my soap message.
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"
"<SignOn xmlns=\"http://10.12.50.99/CUTechWebservices/CuTechWebService.asmx\">\n"
"<DeviceID>4A6B740C-0B5B-5F8C-8694-1EC0196C1C67</DeviceID>\n"
"<UserID>string</UserID>\n"
"<CrID>6</CrID>\n"
"<UserPin>string</UserPin>\n"
"</SignOn>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
As user id and password are diff for users, I want to pass the values from the textfields (user id and password) to the soap message.
How to do this?
Thanks in advance.
OK So below is some steps to follow. I am not sure exact code you can use or not. But modify it as per your need.
For Calling Request Do something like this :
NSMutableURLRequest *request = [WebAPIRequest buildSOAPRequest:#"WebserviceURL" withPostData:strRequest WebMethod:#"YourWebMethodHereForExample-ValidateUSer"];
Connection *con = [[[Connection alloc]initWithClass:#"getClientResult" withRoot:#"soap:Body" withDelegate:delegate withTag:tag]autorelease];
[[[NSURLConnection alloc]initWithRequest:request delegate:con] autorelease];
BuildDataRequest should be like this.
+(NSMutableURLRequest *)buildSOAPRequest:(NSString *)serviceUrl withPostData:(NSString *)dataString WebMethod:(NSString*)strMethod{
NSURL *url = [NSURL URLWithString:serviceUrl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [dataString length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
if (isRegion==YES)
{
isRegion = NO;
[theRequest addValue:[NSString stringWithFormat:#"%#%#",NSLocalizedString(#"SOAP Action1",nil),strMethod] forHTTPHeaderField:#"SOAPAction"];
}else
{
[theRequest addValue:[NSString stringWithFormat:#"%#%#",NSLocalizedString(#"SOAP Action",nil),strMethod] forHTTPHeaderField:#"SOAPAction"];
}
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [dataString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",dataString);
return theRequest;
}
Hope this help.

soap web service

I am parsing a soap web service for authentication but it is not returining any NSData so that I can writex xml parser for it.
Please let me know what I'm doing wrong.
Here is my code:
**
-(void)getAdvertisment
{
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>"
"<GetAdvertisement xmlns=\"http://tempuri.org/\" />"
"</soap:Body>"
"</soap:Envelope>"];**
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/GetAdvertisement" 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.
}
}
NSURLConnection is an asynchronous method - have you implemented the rest of the delegate methods needed to actually listen for and retrieve the data?
You may wish to review the documentation here
In your example above, webData will always be empty, it's up to you to populate it...

Server did not recognize the value of HTTP Header SOAPAction in iPhone?

I am try to connect webservices to my code i got error as
"Server did not recognize the value of HTTP Header SOAPAction"
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"
"<Hello xmlns=\"http://viium.com/\">\n"
"<name>%#</name>\n"
"</Hello>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", #"new"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"http://viium.com/WebService/HelloWorld.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://viium.com/Hello" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
Please guide me what iam missing?
NSURL *url = [NSURL URLWithString:#"https://servername.com/WebService/WebServicename.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapMessage length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"servername.com/Hello" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
here is the link,
http://abhicodehelp.blogspot.com/2010/12/handling-soap-with-iphone.html
this may help you..
If it's your own web-service, check whether your "servername.com/Hello" in query equals namespace in Web-Service.
In ASP.Net it defines like:
[WebService(Namespace="servername.com/Hello")]