soap response error - iphone

I am doing soap request/response application. While building soap response, I am getting "Server did not recognize the value of HTTP Header SOAPAction".
What does that mean? And how to solve this?
Here is the code.
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>10827</UserID>\n"
"<CrID>6</CrID>\n"
"<UserPin>777777!</UserPin>\n"
"</SignOn>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSLog(#"%#",soapMessage);
//Soap request.
NSURL *url = [NSURL URLWithString:#"https://mobile.areafinancial.com/cutechservice/cutechwebservice.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://10.12.50.99/CUTechWebservices/CuTechWebService.asmx" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

In SOAP 1.2 SOAPAction header was replaced with "optional" action attribute of Content-Type header field.
Try it with following header:
Content-Type: application/soap+xml; charset=utf-8; action="https://mobile.areafinancial.com/cutechservice/cutechwebservice.asmx?op=SignOn"
without the SOAPAction: header field.
See also http://www.coderanch.com/t/224524/Web-Services/java/SOAPAction-header

Try to remove below line and see what you get. This header you passed in request. You should pass same header as in the web service.
[theRequest addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];

Related

Working with Web services in iPhone

I'm using SOAP in an iPhone app, and I got this error:
<faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/CelsiusToFahrenheit.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
This is my code:
NSString *soapFormat = [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",txt1.text];
NSURL *locationOfWebService = [NSURL URLWithString:#"http://www.w3schools.com/webservices/tempconvert.asmx"];
NSLog(#"web url = %#",locationOfWebService);
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapFormat length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
Any help will be appreciated, thanks.
just went through the url http://www.w3schools.com/webservices/tempconvert.asmx for location of web service, there i found that in while using SOAP 1.1 soap action was http://www.w3schools.com/webservices/CelsiusToFahrenheit
and not http://tempuri.org/CelsiusToFahrenheit as you have written for your soacp action field.
This is the exact format for sending request to this particular web service ,
POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.w3schools.com/webservices/CelsiusToFahrenheit"
I believe you are setting wrong value of soap action. because soap action mentioned is different from what you have written. (FYI, soap action is used to identify one particular web service or web method, from a large no of web methods , which are present on same location)
Try this.It will work
NSString *soapFormat = [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://www.w3schools.com/webservices/\">\n"
"<Celsius>%#</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",#"30"];
NSURL *locationOfWebService = [NSURL URLWithString:#"http://www.w3schools.com/webservices/tempconvert.asmx"];
NSLog(#"web url = %#",locationOfWebService);
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapFormat length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
//Try This One It Returns integer value
NSString *soapFormat = [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://www.w3schools.com/webservices/\">\n"
"<Celsius>%#</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",#"50"];
NSURL *locationOfWebService = [NSURL URLWithString:#"http://www.w3schools.com/webservices/tempconvert.asmx"];
NSLog(#"web url = %#",locationOfWebService);
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapFormat length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSData *data_reply;
NSError *err;
data_reply = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&err];
NSString *aStr = [[NSString alloc] initWithData:data_reply encoding:NSASCIIStringEncoding];
NSLog(#"aStr:%#",aStr);
O/P:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/"><CelsiusToFahrenheitResult>122</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>
check this answer it returns 122 integer value.......

soap web service not working in iphone

i am trying the following code, soap web service.It is not working .
please help me out.
Link
Service
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"
"<request xmlns=\"http://searchupc.com/GenerateBarcode\">\n"
"<auth>5ggpf54TRghbnIvqS2XVGQQ0q6qCNuJ</auth>\n"
"<method>FetchProductByUPC</method>"
"<params>"
"<upc>026274920257</upc>"
"</params>"
"</request>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"http://www.simpleupc.com/api/"];
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://searchupc.com/GenerateBarcode/" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"SOAP CONNECTED qqq%#",theRequest);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Check http://sudzc.com/ . it will generate xcode project for you.
1: Type the web address of the WSDL to convert (if Protected, then enter username and password )
2: Choose the type of code bundle to create (Objective c) and download.
Just drag and drop the 'Generated' folder to your project and access all the SOAP messages.
Try this. its really easy and simple.
I've found wsdlToObjc pretty good. It generates client side ObjC bindings for you from WSDL. You can download it at http://code.google.com/p/wsdl2objc/. The link http://www.priyaontech.com/2012/10/soap-based-webservices-integration-in-an-ios-app/ explains its usage with an example project.

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.

Error while making SOAP request call in iPhone app

I am trying to make a soap call. It's a very basic call with "Welcome User" output. The return value is in xml format, and I am getting the following error. However I am not sure what this error means.
The following code shows the soap request and post request that I have made:
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"
"<WelcomeXML xmlns=\"http://www.somewebsite.com/phpwebservice/index.php\">\n"
"<name>"
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<message>\n"
"<name>Hitesh</name>\n"
"</message>\n"
"</name>"
"</WelcomeXML>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSLog(#"%#",soapMessage);
NSURL *url = [NSURL URLWithString:#"http://www.somewebsite.com/phpwebservice/index.php"];
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://www.somewebsite.com/phpwebservice/index.php/WelcomeXML" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
Following is the error that I am getting:
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode><faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">error in msg parsing:
XML error parsing SOAP payload on line 5: Reserved XML Name</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
You have an error in the SOAP request on line 5:
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
is dubbled. What for?

XML parameter for SOAP webservice request in iPhone

I want to send a SOAP request to a webservice method. Now I want to send the parameter as an XML input for that request. How can I do it in iPhone? Right now I am sending the request in following way:
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"
"<GetCustomerInfoXML xmlns=\"http://qa2.alliancetek.com/phpwebservice\">\n"
"<id><customer><id>1</id></customer></id>"
"</GetCustomerInfoXML>"
"</soap:Body>\n"
"</soap:Envelope>";
NSLog(#"%#",soapMessage);
NSURL *url = [NSURL URLWithString:#"http://qa2.alliancetek.com/phpwebservice/index.php"];
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://qa2.alliancetek.com/phpwebservice/index.php//GetCustomerInfoXML" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
But I want to send the parameters in XML format.
Wrap the parameters in the CDATA tag, <![CDATA["parameters"]]>. This will tell the server not to parse it. Then that data is sent to the service which can parse it as regular xml.