how to write xml for soap message - objective-c

I am new to web services in iphone.
I need to get data from .net server.
I fallow this tutorial
In this soap message is
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", nameInput.text
];
But i did n't have knowledge on this,
I am from java background.
In android my xml string is
xml="<MortgageGetLoanOfficerInfo><PhoneNumber>"+getDeviceTelNo()+"</PhoneNumber></MortgageGetLoanOfficerInfo>";
how can i write this in iphone.
can any pls post solution.
Thank u in advance.

You are correctly assigning your soap request in NSString. Now you have to create NSMutableURLRequest object and send the request as follow
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:webServiceUrl];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:yourSoapAction forHTTPHeaderField:#"SOAPAction"];
[req addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[[NSError alloc] init] autorelease];
NSData *webData = [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];
Now parse your webData using any of xml parser. I generally prefer NSXMLParser
Hope this helped..

try with following sample 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"
"<getLogin xmlns=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
"<email>%#</email>\n"
"<user_pwd>%#</user_pwd>\n"
"</getLogin>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", userNameField.text,passWordField.text];
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: #"your soap action" 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

AFNetworking JSON post using Objective C returns unsupported media type

I am trying to access a Microsoft CRM Web Service and I am using AFNetworking to do that. The service is configured to handle JSON request and response.
But when I do a JSON post I am getting this error:
"com.alamofire.serialization.response.error.data=<>,
NSLocalizedDescription=Request failed: unsupported media type (415)} "
What is the Mistake i have done on the code.
This is my code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Accept" ];
[manager.requestSerializer setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type" ];
NSDictionary *parameters = #{#"username": self.Username.text,
#"password": self.Password.text};
[manager POST:#"http://www.xxxxxx.com/xxxx.svc/xxxxx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Actually the server was expecting a SOAP XML request. This is the code which solved my issue:
NSString *soapMessage = [NSString stringWithFormat:#"<Authenticate xmlns=\"http://tempuri.org/\"><userName>"];
soapMessage = [soapMessage stringByAppendingString:self.loginUsername.text];
soapMessage = [soapMessage stringByAppendingString:#"</userName>"];
soapMessage = [soapMessage stringByAppendingString:#"<password>"];
soapMessage = [soapMessage stringByAppendingString:self.loginPassword.text];
soapMessage = [soapMessage stringByAppendingString:#"</password></Authenticate>"];
NSURL *url = [NSURL URLWithString:#"http://www.xxxxx.com/xxxxxx.svc/Authenticate"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: #"application/xml; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/xxxxxx/Authenticate" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
// NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(theConnection) {
NSError *WSerror;
NSURLResponse *WSresponse;
NSString *theXml;
NSData *myData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&WSresponse error:&WSerror];
theXml = [[NSString alloc]initWithBytes:[myData bytes] length:[myData length] encoding:NSUTF8StringEncoding];
NSLog(#"Data : %#, %#",theXml, myData);
}
else {
NSLog(#"theConnection is NULL");
}
This post helped me Passing parameter to WCF function using Objective c
Googling for "status 415" finds this straight from the horses mouth:
"The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."
In other words, the server doesn't support application/json.
Not sure what you are asking from the CRM but you could try this:
[request setValue:#"image/png" forHTTPHeaderField:#"Content-Type"];

How to pass 2 parameters in a method in WCF Service using Objective c?

My Parameters are
Request1 = <Request> <name>2</name> <pwd>1</pwd> </Request>
Request2 = value1
My Code is
-(void)webRequestCommnunication
{
//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
#"<?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><Method1 xmlns=\"http://tempuri.org/\"><Request1><Request> <name>2</name> <pwd>1</pwd> </Request></Request1><Request2>value1</Request2></Method1></soap:Body></soap:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://localIPaddress/servicename1/servicename2/Method1.svc"];
theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/service1/Method1" forHTTPHeaderField:#"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
webData = [[NSMutableData alloc]init];
if(theConnection)
{
NSLog(#"Connected....");
}
else
{
NSLog(#"theConnection is NULL");
}
}
I want to pass Request1, Request2 inside Method1,but it throws exception as The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Method1'. End element 'Request1' from namespace 'http://tempuri.org/' expected. Found element 'Request' from namespace 'http://tempuri.org/'. Line 1, position 276..
Can anyone suggest what i am doing wrong?
Finally Got solution!
While sending Request1 parameter the <,> symbol is not parsed in Soap Format,only the parent tag the symbol is validated and the modified code is
-(void)webRequestCommnunication
{
//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
#"<?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><Method1 xmlns=\"http://tempuri.org/\"><Request1><Request1><Request><name>2</name><pwd>1</pwd></Request></Request1><Request2>value1</Request2></Method1></soap:Body></soap:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://localIPaddress/servicename1/servicename2/Method1.svc"];
theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/service1/Method1" forHTTPHeaderField:#"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
webData = [[NSMutableData alloc]init];
if(theConnection)
{
NSLog(#"Connected....");
}
else
{
NSLog(#"theConnection is NULL");
}
}

Can't obtain a response from WCF service

i have a trouble when i try to call a web service made in WCF, this service is calling by Mac os x app.
this is the code
soapMessage = #"{name:\"Sergio\"}";
NSURL *url = [NSURL URLWithString:#"http://localhost/GSBonoServicios/GSBonoService.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest setHTTPMethod: #"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *requestData = [NSData dataWithBytes:[soapMessage UTF8String] length:[soapMessage length]];
[theRequest setHTTPBody:requestData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
The connection is successful but didn't a response, i try implementing some tutorials and I'm looking in Stackoverflow and i find many answers but nothing works, i hope you can help me.
ps. I do not speak much English, but I hope I explained, Thanks
Finally I found the solution the problem was in the service, and the code that works is
NSString *name=#"Sergio";
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: name, #"name",
nil];
NSURL *url = [NSURL URLWithString:#"http://www.myserver.com/Servicio/ServicioEjemplo.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod: #"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"applicat ion/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:kNilOptions error:&error];
[theRequest setHTTPBody:postdata];
NSLog(#"JSON summary: %#", [[NSString alloc] initWithData:postdata
encoding:NSUTF8StringEncoding]);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Thanks

Connect Webservice through XML

NSURL *url = [NSURL URLWithString:#"http://192.168.1.56/AMSP/AMSPWS.asmx"];
NSString *soapMsg=[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>"
"<PostXMLStr xmlns=\"http://tempuri.org/\">"
"<cust>%#</cust>"
"<tran>%#</tran>"
"<ret>%#</ret>"
"<ppay>%#</ppay>"
"<recp>%#</recp>"
"<sCode>%#</sCode>"
"<companyShortName>%#</companyShortName>"
"<companyCode>%#</companyCode>"
"</PostXMLStr>"
"</soap:Body>"
"</soap:Envelope>",cust,trans,RETURNS,prepayment,receipt,spcode1,companyShortName,companyCode];
NSMutableURLRequest *requests = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:5];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapMsg length]];
[requests setHTTPMethod:#"POST"];
[requests addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[requests addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[requests addValue:#"http://tempuri.org/PostXMLStr" forHTTPHeaderField:#"SOAPAction"];
[requests setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding ]];
NSURLResponse *response;
NSError *error = nil;
NSData *data =[[NSMutableData alloc]init];
data = [NSURLConnection sendSynchronousRequest:requests returningResponse:&response error:&error];
NSString *stringSoap;
stringSoap = [[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding];
NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
I am new to connect WebService through objective C. I do not know how to check XML is connected to WebService. When I print stringSoap, I am not getting any value. I do not know is this correct way to calling XML. How to check XML is connected to WebService or not. Any help will be appreciated. Thanks in advance.
As you are new, this will help u to convert SOAP service to ObjectiveC
sudzc.com
wsdl2objc

xml post as parameter for Objective C

How does one post xml as parameter with Objective C?
I've sent a username and a password in xml to the server. I've tried using ASIFormDataRequest. I've posted xml and the server've given the error that is "username or password is false". I think the server doesn't parse the posting xml. Is there any way to post xml as parameter?
NSURL *url = [NSURL URLWithString:#"url code"];
ASIFormDataRequest *request1 =[ASIFormDataRequest requestWithURL:url];
[request1 setPostValue:"xml block" forKey:#"data"];
[request1 addRequestHeader:#"Content-Type" value:#"application/xml;"];
[request1 setDelegate:self];
[request1 startSynchronous];
Yes, it is. Look at my example:
NSString *message = [[NSString alloc] initWithFormat:#"<?xml version=\"1.0\" ?>\n<parameters></parameters>"];
url = [NSURL URLWithString:#"https://https_url.com"];
request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d",[message length]];
[request addValue:#"application/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
[message release];