I am trying to make my iPhone application connect to my Webservices. I don't have much experience with this Webservices so this is why I ask here for information/help.
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">\n"
"<soap:Body>\n"
"<silent xmlns="http://tempuri.org/">"
"<action>logon</action>"
"<gameid>mygame</gameid>"
"<gpassword>gamepwd</gpassword>"
"<data>"
"<username>abc#abc.com</username>"
"<password>a</password>"
"</data>"
"</silent>"
"</soap:Body>"
"</soap:Envelope>"];
NSURL *url1 = [NSURL URLWithString:#"http://mywebsite.com/Service.svc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url1];
NSString *msgLength1 = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/ISilentManagerAPI/Service" forHTTPHeaderField:#"Soapaction"];
[theRequest addValue: msgLength1 forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
This code always returns status code 415, what am I doing wrong?
PS.
Here is link which is working fine in Android getting java.io.IOException: HTTP request failed, HTTP status: 404 in ksoap2 while passing xml data to soap1.2 android
- Try This Which Will Surely Help You :
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><Service xmlns=\"http://tempuri.org/\">"
"<xmlstring><silent><action>logon</action><gameid>mygame</gameid><gpassword>gamepwd</gpassword><data><username>abcd#abc.com</username><password>a</password></data></silent></xmlstring></Service>"
"</SOAP-ENV:Body>"
"</SOAP-ENV:Envelope>"];
NSURL *url1 = [NSURL URLWithString:#"http://mywebsite.com/Service.svc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url1];
NSString *msgLength1 = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/ISilentManagerAPI/Service" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength1 forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
Related
I am first time using soap service,
This is the data server expecting,
POST /updateLocation.asmx HTTP/1.1
Host: www.geoming.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<setLocation xmlns="http://geoming.com/">
<pLati>string</pLati>
<pLongi>string</pLongi>
<pStatusMsg>string</pStatusMsg>
<pID>string</pID>
<pSpeed>string</pSpeed>
<pStreet1>string</pStreet1>
<pStreet2>string</pStreet2>
<pCity>string</pCity>
<pState>string</pState>
<pCountry>string</pCountry>
<pDate>string</pDate>
<pTimeZone>string</pTimeZone>
</setLocation>
</soap12:Body>
</soap12:Envelope>
and in objective c section i am doing like this,
soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope/\">"
"<soap12:Body>"
"<setLocation xmlns=\"http://www.geoming.com/\">"
"<pLati>-33.868</pLati>"
"<pLongi>151.2086</pLongi>"
"<pStatusMsg>santanu test</pStatusMsg>"
"<pID>3</pID>"
"<pSpeed>12</pSpeed>"
"<pStreet1>CD - 96</pStreet1>"
"<pStreet2>Salt lake city</pStreet2>"
"<pCity>kolkata</pCity>"
"<pState>west bengal</pState>"
"<pCountry>india</pCountry>"
"<pDate>16-04-2015</pDate>"
"<pTimeZone>-5.30</pTimeZone>"
"</setLocation>"
"</soap12:Body>"
"</soap12:Envelope>"];
//Now create a request to the URL
NSURL *url = [NSURL URLWithString:#"http://www.geoming.com/updateLocation.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
//ad required headers to the request
[theRequest addValue:#"www.geoming.com" forHTTPHeaderField:#"Host"];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://www.geoming.com/setLocation" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
It's always showing "System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://www.geoming.com/setLocation. System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()".
Unable to to find the solution as i am new in ios.
Wrong SOAPAction. Instead #"http://www.geoming.com/setLocation" you need use #"http://geoming.com/setLocation"
Try my code:
static NSString* const cstrSetLocMsg = #"<?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>"
"<setLocation xmlns=\"http://geoming.com/\">"
"<pLati>-33.868</pLati>"
"<pLongi>151.2086</pLongi>"
"<pStatusMsg>santanu test</pStatusMsg>"
"<pID>3</pID>"
"<pSpeed>12</pSpeed>"
"<pStreet1>CD - 96</pStreet1>"
"<pStreet2>Salt lake city</pStreet2>"
"<pCity>kolkata</pCity>"
"<pState>west bengal</pState>"
"<pCountry>india</pCountry>"
"<pDate>16-04-2015</pDate>"
"<pTimeZone>-5.30</pTimeZone>"
"</setLocation>"
"</soap:Body>"
"</soap:Envelope>";
And create request:
+ (NSMutableURLRequest*) requestSetLoc{
NSString *_soapMsg = cstrSetLocMsg;
NSURL *url = [NSURL URLWithString:#"http://www.geoming.com/updateLocation.asmx"];
NSMutableURLRequest *req = [[[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f] autorelease];
NSString *msgLength = [NSString stringWithFormat:#"%d", [_soapMsg length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://geoming.com/setLocation" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [_soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
return req;}
It should works!
Change your string to
soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope/\">\
<soap12:Body>\
<setLocation xmlns=\"http://www.geoming.com/\">\
<pLati>-33.868</pLati>\
<pLongi>151.2086</pLongi>\
<pStatusMsg>santanu test</pStatusMsg>\
<pID>3</pID>\
<pSpeed>12</pSpeed>\
<pStreet1>CD - 96</pStreet1>\
<pStreet2>Salt lake city</pStreet2>
<pCity>kolkata</pCity>\
<pState>west bengal</pState>\
<pCountry>india</pCountry>\
<pDate>16-04-2015</pDate>\
<pTimeZone>-5.30</pTimeZone>\
</setLocation>
</soap12:Body>\
</soap12:Envelope>"];
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"];
I want to make a simple iOS application which calls a Web service.
I reffered the following link
http://www.codeproject.com/Tips/622376/iOS-Soap-Webservice-Calling-and-Parsing-the-Respon
I tried to run the sample project that i got from this link.But I am getting the response like
"soap:ClientServer did not recognize the value of HTTP Header SOAPAction: http://tempuri.org/."
I am not able to find out what the reason behind this error is.And I am very much new to web services.Can anyone help me to solve this error?And someone please tell me ,what are the basic steps to call a web service from an iOS application.Is there any special version requirements of Xcode and iOS for calling web services?Please help me ...Thanks in advance for any help...
This is the code that I am using for sending the request
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"
" <CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>%#</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>\n" ,textFieldCelcisus.text];
NSURL *url = [NSURL URLWithString:#"http://w3schools.com/webservices/tempconvert.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/" 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] ;
NSLog(#"webdata is %#",webData);
NSLog(#"Problem");
}
else
{
NSLog(#"theConnection is NULL");
}
Change this line:
[theRequest addValue: #"http://tempuri.org/" forHTTPHeaderField:#"SOAPAction"];
into:
[theRequest addValue: #"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
The server is complaining about the value for the SOAPAction. The SOAPAction's are defined in the WSDL, and are different for each operation.
Since you provided the service URL and the service is public, i just checked it.
The SOAPAction for 'CelsiusToFahrenheit' appeared to be:
http://www.w3schools.com/webservices/CelsiusToFahrenheit
Note that each operation has it's own SOAPAction, e.g.:
CelsiusToFahrenheit -> http://www.w3schools.com/webservices/CelsiusToFahrenheit
FahrenheitToCelsius -> http://www.w3schools.com/webservices/FahrenheitToCelsius
etc...
Above answer is right, only suggestion that use NSURLSession instead of NSURLConnection.
I am trying to send SOAP request but it is returning "HTTP 415 unsupported media" error.
below given is my code
NSString *soapMessage = [NSString stringWithFormat:#"<Envelope xmlns=\"http://www.w3.org/2003/05/soap-envelope\"><Body><GetCity xmlns=\"http://tempuri.org/\"><SearchString></SearchString><SearchZone></SearchZone></GetCity></Body></Envelope>"];
NSURL *url=[NSURL URLWithString:#"http://ws.abcxyz.com/abcxyz1/Service1.svc?wsdl"];
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/IService1/GetCity" 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) {
NSMutableData *responseData = [NSMutableData data];
NSLog(#"success %d", [responseData length]);
} else {
NSLog(#"failed");
//messageTextView.text=#"Failed";
}
Please let me know if i need to change anything.
HTTP 415 error- Unsupported Media Type
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.
I have written a function to get a response from web service. But now I want to send parameters to a web service. How can I modify the code below to send parameters to another web service? I want to work with the below code, I am new to IOS and dont want to mess up since I have a working code.
- (IBAction)buttonClick:(id)sender {
recordResults = FALSE;
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"
"<GetUserList xmlns=\"http://methodoor.com/checkupservice/\" />\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
//NSLog(soapMessage);
_lbl_result.text = soapMessage;
NSURL *url = [NSURL URLWithString:#"http://servicing2.rotanet.com.tr/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://methodoor.com/checkupservice/GetUserList" 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");
}
//[nameInput resignFirstResponder];
}
I see you are accessing SOAP web services. And you are already sending data in to your web service in SOAP message.
As this is a SOAP API, you only need to change the SOAP Action [theRequest addValue: #"http://methodoor.com/checkupservice/GetUserList" forHTTPHeaderField:#"SOAPAction"]; and SOAP message, which you can get from the WSDL or SVC file, to access another method of your web service.
For more info you can have a look at this link. Hope this helps.