XML parameter for SOAP webservice request in iPhone - 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.

Related

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 upload image + xml into web server using SOAP request from iphone application

I am implementing an iPad application. In my application I need to get some information and profile pic from user and need to upload that information to a web server using SOAP request. I am done with text inputs uploading. But when it comes to image I am not aware of that. I know, to upload an image we need to convert that to NSData I did it, but how can I add that to my post string?
My code is as follows:
NSData* imageData = UIImagePNGRepresentation(myPic);
NSString* uploadValues=#"";
uploadValues = [uploadValues stringByAppendingString:[NSString stringWithFormat:#"<UserInformation><UserInfo action=\"INSERT\"><Userid>123</Userid><FirstName>abc</FirstName><LastName>xyz</LastName><Contact>00000000</Contact><Email>abc#googole.com</Email><Address>USA</Address><Image>How can I put image data here? is that work if I put data here?</Image></UserInfo></UserInformation>"];
uploadValues = [uploadValues stringByReplacingOccurrencesOfString:#"(null)" withString:#" "];
serverConnections=[ServerConnections sharedConnections];
serverConnections.delegate=self;
NSString* soapString = [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"
"<SetUserInfo xmlns=\"http://example.com/\">\n"
"<Data><![CDATA[ %s ]]></Data> \n"
"</SetUserInfo> \n"
"</soap:Body>\n"
"</soap:Envelope>\n",[uploadValues UTF8String]];
printf("\n Input string:%s",[soapString UTF8String]);
NSURL *url = [NSURL URLWithString:#"http://example.com/xyz.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapString length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http:/example.com/SetUserInfo" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];
[serverConnections startEstablishingConnectionwithServer:theRequest];
From Here
[req addValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
[req setHTTPBody:imgData]

soap response error

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"];

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.

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")]