Unable to access SOAP service built on WCF on iPhone - ios

I am trying to access a WCF service from a iPhone app but I am getting following error:
AFHTTPRequestOperation error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0x8c75b90 {NSErrorFailingURLKey=http://xxx.xxx.xx.xxx/accessserviceshost/servicecontroller/holdingslistservice.svc, AFNetworkingOperationFailingURLResponseErrorKey= { URL: http://xxx.xxx.xx.xxx/accessserviceshost/servicecontroller/holdingslistservice.svc } { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 736;
"Content-Type" = "text/xml; charset=utf-8";
Date = "Tue, 25 Mar 2014 10:14:58 GMT";
Server = "Microsoft-IIS/6.0";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: internal server error (500)}
Following is my code to access the service:
NSURL *baseURL = [NSURL URLWithString:#"http://xxx.xxx.xx.xxx/accessserviceshost/servicecontroller/holdingslistservice.svc"];
NSString *soapBody = [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><HoldingsListInput xmlns=\"http://tempuri.org/\">"
"<Account_Id>1000</Account_Id>"
"<Allow_Global_Processing_Fl></Allow_Global_Processing_Fl>"
"<Asset_Class_Cd></Asset_Class_Cd>"
"<Cash_Balances_Cd>Y</Cash_Balances_Cd>"
"<Display_Cd>TA</Display_Cd>"
"<Industry_Class_Cd>30</Industry_Class_Cd>"
"<Invested_Income_Portfolio_Cd>Y</Invested_Income_Portfolio_Cd>"
"<Local_Fl>Y</Local_Fl>"
"<Positions_As_Of_Cd>TD</Positions_As_Of_Cd>"
"<Principal_Portfolio_Cd>Y</Principal_Portfolio_Cd>"
"<Security_Tp>48</Security_Tp>"
"<Show_Position_Instructions_Fl>Y</Show_Position_Instructions_Fl>"
"<Sort_Cd>MA</Sort_Cd>"
"<Unique_Assets_Only_Cd>Y</Unique_Assets_Only_Cd>"
"<Very_Liquid_Asset_Classes_Cd>X</Very_Liquid_Asset_Classes_Cd>"
"</HoldingsListInput></soap:Body></soap:Envelope>"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request addValue:#"http://tempuri.org/RetrieveHoldingList" forHTTPHeaderField:#"SOAPAction"];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%#", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"%s: AFHTTPRequestOperation error: %#", __FUNCTION__, error);
}];
[operation start];
I have checked service with same input on an asp.net page and it works there but I am not able to run it on the iPhone app.

-Try This way . IT might help. Edit your Soap Request string in this manner , may be problem lies in that .
NSString *soapRequest =[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"
"<Login xmlns=\"http://tempuri.org/\">\n"
"<UID>%#</UID>\n"
"<Password>%#</Password>\n"
"</Login>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",#"Test",#"Test"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"http://tempuri.org/RetrieveHoldingList" forHTTPHeaderField:#"SOAPAction"];
NSString *msgLength = [NSString stringWithFormat:#"%lu",(unsigned long)[soapRequest length]];
[request addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];

SOAPUI came to my rescue. There was a flaw in the JASON. I used SOAPUI to build request and other parameters need for it and it worked fine for me.

Related

SOAP in iOS Application

I'm trying to develop an app that uses data from a SOAP service. is the first time I use a SOAP service. After reading several tutorials I thought I would be able to set the request to the service in the right way but I always get errors in the response. What's wrong ?!
this is the code for the request
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://microsoft.com/webservices/\">\n"
"<SOAP-ENV:Body>\n"
"<ns1:ListaComuni>\n"
"<Nazione>italia</Nazione>\n"
"</ns1:ListaComuni>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>\n"
];
NSURL *url = [NSURL URLWithString:#"http://www.xxxxx.xxx/XXXXX.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: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
When i receive the answer
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"RISPOSTA %#",theXML);
}
The log:
RISPOSTA <?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><ListaComuniResponse xmlns="http://microsoft.com/webservices/"><ListaComuniResult><anyType xsi:type="xsd:string">Object reference not set to an instance of an object.</anyType></ListaComuniResult></ListaComuniResponse></soap:Body></soap:Envelope>
I tested the XML request on http://wsdlbrowser.com and it all works.
What's wrong ?!
Thanks
Ok...
i found the error...the correct request is:
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>"
"<ListaComuni xmlns=\"http://microsoft.com/webservices/\">"
"<Nazione>%#</Nazione>\n"
"</ListaComuni>"
"</soap:Body>"
"</soap:Envelope>", nameInput.text
];

How to decode Gzip Compressed string from .net Webservice?

I got compressed response string from the .net web service in XML format. How to decode the response string?
My code is shown below.
NSString *mysUrl=#"myurl";
NSString *soapMsg1 = [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"
"<CompressString xmlns=\"http://tempuri.org/\">"
"<text>%#</text>"
"</CompressString>"
"</soap:Body>\n"
"</soap:Envelope>",#"abcdefghijklmonpqrstuvwxyz"];
NSLog(#"%#",soapMsg1);
NSURL *url1 = [NSURL URLWithString:mysUrl];
NSMutableURLRequest *req1 = [NSMutableURLRequest requestWithURL:url1];
NSString *msgLength1 = [NSString stringWithFormat:#"%d", [soapMsg1 length]];
[req1 addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSLog(#"lenght of the message is.......%#",msgLength1);
[req1 addValue:#"http://tempuri.org/CompressString" forHTTPHeaderField:#"SOAPAction"];
[req1 addValue:msgLength1 forHTTPHeaderField:#"Content-Length"];
NSLog(#"lenght of the message is.......%#",req1);
[req1 setHTTPMethod:#"POST"];
[req1 setHTTPBody: [soapMsg1 dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSError *error;
//getting the data
[req1 addValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
NSData *newData = [NSURLConnection sendSynchronousRequest:req1 returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSLog(#"\n\n\n Compression string response %# \n\n\n",responseString);

Expected status code in (200-299), got 500

I'm fetching data from server. Server side is maintaining from client. I'm just parsing the data and get using AFNetworking. But may be some SOAPACtion having error.Server did not recognize the value of HTTP Header SOAPAction: http://test.server.com/Role
HTTP POST:
POST /service/services.asmx/Role HTTP/1.1
Host: server.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
SyncResetData=string
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"
"<Sync_Reset_Role xmlns=\"http://test.server.com/\">\n"
"<SyncResetData><![CDATA[%#]]></SyncResetData>\n"
"</Sync_Reset_Role>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",string];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#/services.asmx?wsdl/Role",SERVICE_ENDPOINT]];
NSLog(#"url is getResetRoleContentLength %#",url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *strLength = [NSString stringWithFormat:#"%d",[soapMessage length]];
[request addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue: [NSString stringWithFormat:#"http://test.server.com/Role"] forHTTPHeaderField:#"SOAPAction"];
[request addValue: strLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation2, id responseObject2) {
} failure:^(AFHTTPRequestOperation *operation2, NSError *error) {
NSLog(#"Error: %#", error);
if([delegate respondsToSelector:#selector(requestFailed)]){
[delegate performSelector:#selector(requestFailed)];
}
}];
Error:
Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x6995430 {NSLocalizedRecoverySuggestion=<?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><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://test.server.com/Role.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x693c870>, NSErrorFailingURLKey=http://test.server.com/test/services.asmx?wsdl/Role, NSLocalizedDescription=Expected status code in (200-299), got 500, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://test.server.com/test/services.asmx?wsdl/Role>}

SOAP Request to the server in ios

i am facing a problem while requesting server in SOAP format. when i am pasting the Soap envelope in the the browser(just for testing the web services) then it gives correct response. But when i am requesting from app then it give me irrelevant response.
I searched a lot on but every where i found the same technique for soap request.
Here is my code what i am using.
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"
"<ProfileLogin xmlns=\"http://tempuri.org/\">"
"<strLoginRequest>"
"<email>abc#gmail.com.au</email>"
"<password>123456</password>"
"</strLoginRequest>"
" </ProfileLogin>"
"</soap:Body>"
"</soap:Envelope>"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"http://www.abc.com.au/mobileapp.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest setValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest setValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setValue:#"http://tempuri.org/ProfileLogin" 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");
}
and getting response
<ProfileReply><Success>False</Success><Error>Data at the root level is invalid. Line 1, position 1.</Error></ProfileReply>
please some buddy let me know it is my fault of mycode or from server side.
Try
[theRequest setValue:#"application/soap+xml;charset = utf-8" forHTTPHeaderField:#"Content-Type"];
try this
[theRequest setValue:#"ProfileLogin" forHTTPHeaderField:#"SOAPAction"];
instead of
[theRequest setValue:#"http://tempuri.org/ProfileLogin" forHTTPHeaderField:#"SOAPAction"];

Fault string error while accessing web service in iOS

I am accessing xml web service in my iPhone application. But i am not able to get the appropriate data from the server. I am getting following error in connectionDidFinishLoading delegate method of NSURLConnection.
soap:ServerServer
was unable to process request. ---> Object reference not set to an
instance of an object.
Following is my code :
NSString *deviceID = [OpenUDID value];
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:Header>"
"<AuthHeader xmlns=\"http://tempuri.org/\">"
"<Username>admin</Username>"
"<Password>admin</Password>"
"</AuthHeader>"
"</soap:Header>"
"<soap:Body>"
"<WsGetAllDrugDetailsXML xmlns=\"http://tempuri.org/\">"
"<UserId>%d</UserId>"
"<IMEI>%#</IMEI>"
"</WsGetAllDrugDetailsXML>"
"</soap:Body>"
"</soap:Envelope>",[Appdelegate.UserID intValue],deviceID];
//---print it to the Debugger Console for verification---
NSLog(#"%#", soapMsg);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:Appdelegate.wsURL];
//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://tempuri.org/WsGetAllDrugDetailsXML" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
I'm not able to predict what is going wrong from my side. Or it is the error from server side that i'm not receiving the data.
Please help me.Thanks in Advance!
Your Content-Length value is not correct:
The length must be in bytes. NSString's length method returns the number of UTF-16 code units.
You may fix it as follows:
NSData* bodyData = [soapMsg dataUsingEncoding:NSUTF8StringEncoding];
[req addValue:[NSString stringWithFormat:#"%d",(int)[bodyData length]] forHTTPHeaderField:#"Content-Length"];
[req setHTTPBody:bodyData];

Resources