How to call a WCF wsHttpBinding reliable-session enabled web service from iOS objective-C code.. I Wrote a general code for accessing the service, but I am getting this error "The action http://tempuri.org/MyService/AuthneticateUser is not supported by this endpoint. Only WS-ReliableMessaging February 2005 messages are processed by this endpoint".
Here my code
<NSString *stringAuthenticateUsr=#"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">
<s:Header>
<a:Action s:mustUnderstand=\"1\">tempuri.org/MyService/AuthneticateUser
</… xmlns:h=\"tempuri.org\">1</h:ApplicationID>
<h:DeviceID xmlns:h=\"tempuri.org\">1</h:DeviceID>
<h:Password xmlns:h=\"tempuri.org\">Password1</h:Password>
<a:MessageID>urn:uuid:041b‌​65a7-4ab2-4da9-ba3b-05d5cccfe074</a:MessageID>
<a:ReplyTo><a:Address>w3.org/2005/08/addressing/anonymous</a:Address>
</… s:mustUnderstand=\"1\">MySvcUrl
</a:To>
</s:Header>
<s:Body />
</s:Envelope>";
NSURL *url = [NSURL URLWithString: MySvcUrl];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0];
[theRequest setCachePolicy:NSURLCacheStorageNotAllowed];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[stringAuthenticateUsr length]];
[theRequest addValue: #"application/soap+xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/MyService/AuthneticateUser" forHTTPHeaderField:#"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPShouldHandleCookies:YES];
[theRequest setHTTPBody: [stringAuthenticateUsr dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* response;
NSError* error = nil;
NSData* resultData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *result = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
NSLog(#"result--%#",result);
Related
I'm new to SOAP API.
I'm using the following code to access my API.
NSString *stringURL=[NSString stringWithFormat:#"http://tennispro.co.in/services.asmx"];
// Allocate and initialize an NSURLConnection with the given request and delegate. The asynchronous URL load process is started as part of this method.
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>"
"<ReturnSyncMasterData xmlns=\"http://tempuri.org/\">"
"<datetime>%#</datetime>"
"</ReturnSyncMasterData>"
"</soap:Body>"
"</soap:Envelope>",#""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
[request addValue:#"text/html; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"2159" forHTTPHeaderField:#"Content-Length"];
[request addValue:#"private, max-age=0" forHTTPHeaderField:#"Cache-Control"];
[request addValue:#"gzip" forHTTPHeaderField:#"Content-Encoding"];
[request addValue:#"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:#"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:#"POST"];
theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData alloc] init];
}
But in response I get HTML instead of XML. I want to access ReturnSyncMasterData soap action, but I'm unable to call it.
set content type to text/xml
[request addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"2159" forHTTPHeaderField:#"Content-Length"];
[request addValue:#"private, max-age=0" forHTTPHeaderField:#"Cache-Control"];
[request addValue:#"gzip" forHTTPHeaderField:#"Content-Encoding"];
[request addValue:#"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:#"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:#"POST"];
theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData alloc] init];
}
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 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]];
I am calling a web service in iOS. For this, I need to set the header in NSMutableURLRequest object. My service takes two string parameters and returns data in JSON format.
What are the the fields that I need to set in the header (Both while using GET and POST) using the setValue:forHTTPHeaderField:.
We do not need to use setHTTPBody: while using GET.. right???
I had the same question and I resolved (using some code from Iducool and Ankit Mehta) like this...
NSURL *theURL = [NSURL URLWithString:#"yourURL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
//Specify method of request(Get or Post)
[theRequest setHTTPMethod:#"GET"];
//Pass some default parameter(like content-type etc.)
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
//Now pass your own parameter
[theRequest setValue:yourValue forHTTPHeaderField:theNameOfThePropertyValue];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
//Now you can create a NSDictionary with NSJSONSerialization
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError];
NSLog(#"url to send request= %#",theURL);
NSLog(#"%#",dataDictionaryResponse);
Look at this code which i am using to call a web service and thats working for me
+(NSString *)http_post_method_changed:(NSString *)url content:(NSString *)jsonContent{
NSURL *theURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
NSData *requestData = [jsonContent dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
[theRequest setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPBody: requestData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *data=[[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding];
NSLog(#"url to send request= %#",url);
NSLog(#"response1111:-%#",data);
return data;
}
Both while using GET and POST We do not need to use setHTTPBody?
setHTTPBody only make sense when you are using POST request. GET don't need it.
For Header parameters do something like below
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
//Specify method of request(Get or Post)
[theRequest setHTTPMethod:#"GET"];
//Pass some default parameter(like content-type etc.)
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
//Now pass your own parameter
[theRequest setValue:yourObj forHTTPHeaderField:#"your parameter name"];
setHTTPBody is used when HTTP method POST is being used.
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:data];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Here the data is nothing but NSData of JSON data that need's to be sent.
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.