i am sending a soap request. But no data is being returned. The web service does not return any error. I am unable to figure out what is wrong. Does it have something to do with the UTF8 Encoding? Please help. Below is my code:
NSString *xmlString = #"<ServiceCalls xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><ServiceCall><Phone_No>033542390843</Phone_No></ServiceCall></ServiceCalls>";
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"
"<GetVehiclesByPhone_ServiceCall_Proc xmlns=\"http://tempuri.org/\">\n"
"<xmlStr>%#</xmlStr>\n"
"<m_LoginId>Username</m_LoginId>\n"
"<m_pass>Password</m_pass>\n"
"</GetVehiclesByPhone_ServiceCall_Proc>\n"
"</soap:Body>\n"
"</soap:Envelope>", xmlString];
NSLog(#"%#", soapMessage);
NSURL *url = [NSURL URLWithString:#"http://103.9.23.21/TPL_Web_Services/TPLInterface.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/GetVehiclesByPhone_ServiceCall_Proc" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSError *error;
NSData *webData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
if (webData != nil) {
NSString *data = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#", data);
if (error == nil) {
if ([data isEqualToString:#"True"] || [data isEqualToString:#"true"]) {
[self showSuccessfulAlert];
[self storeDefaults];
[self.dataLayer notifySplashRemoved];
}
else {
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Invalid Data!" message:#"The phone no. you entered is not registered." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];*/
sendSMS = YES;
}
}
else {
sendSMS = YES;
}
}
else {
sendSMS = YES;
}
You need to set the Content-Length properly: the content length is the length of the body data in bytes, not the length of the NSString, which is the number of UTF-16 code units:
NSData* body = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSString* bodyLength = [NSString stringWithFormat:#"%d", [body count]];
[theRequest addValue: bodyLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPBody:body];
...
Related
I have an array that contains some index inside. When i log it out it looks like this:
item pass: (
80340025,
80319340,
80251277
)
My array is declared as NSAAray
Then I need to make a soap call to insert this each index of this array into the soap message. The question is how?
Here is my soap message.
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>"
" <IncidentQuery xmlns=\"https://www.monitoredsecurity.com/\">"
"<IncidentNumber>%d</IncidentNumber>"
"<MaxSignatures></MaxSignatures>"
"</IncidentQuery>"
"</soap:Body>"
"</soap:Envelope>", item_pass];
NSURL *url = [NSURL URLWithString:#"https://api.monitoredsecurity.com/SWS/incidents.asmx"];
NSMutableURLRequest *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: #"https://www.monitoredsecurity.com/IncidentQuery" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[connection start];
if(connection)
{
_webResponseData = [NSMutableData data] ;
}
else
{
NSLog(#"Connection is NULL");
}
do like this
-(void)SoapCallingMethod
{
for(int i=0;i<array.count;i++)
{
[self yourSoapMethod:[yourArray objectAtIndex:i]];
}
}
-(void)yourSoapMethod:(NSInteger )indexNumber
{
yourSoapMessage = [NSString stringWithFormat:"%d",indexNumber];
// Rest Steps
}
I am trying to post some values to my web service which is working for android version nicely and for iOS it works for about 12 requests then it stops sending. What am i doing wrong?
I am sending about 1 per second.
-(void) PostDataToServer:(NSMutableArray*) value
{
NSString *xValue = [NSString stringWithFormat:#"%#", [value objectAtIndex:0]];
NSString *yValue = [NSString stringWithFormat:#"%#", [value objectAtIndex:1]];
NSString *jsonRequest = [NSString stringWithFormat:#"{\"person\":\"%#\",\"x\":\"%#\",\"y\":\"%#\",\"z\":\"%#\"}",#"1",xValue ,yValue ,#"0.0"];
NSLog(#"Request: %#", jsonRequest);
NSURL *url = [NSURL URLWithString:#"mywebserviceURL"];
request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
NSData *urlData= connection= [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(urlData);
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);
I have implemented SOAP Service in iOS.
I am using the following code for sending the request.
NSString *sSOAPMessage = #"<?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>"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
"<Celsius>50</Celsius>"
"</CelsiusToFahrenheit>"
"</soap:Body>"
"</soap:Envelope>";
NSURL *sRequestURL = [NSURL URLWithString:#"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:#"%d", [sSOAPMessage length]];
[myRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[myRequest addValue: #"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[myRequest addValue: sMessageLength forHTTPHeaderField:#"Content-Length"];
[myRequest setHTTPMethod:#"POST"];
[myRequest setHTTPBody: [sSOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];
if( theConnection ) {
webData = [[NSMutableData data] retain];
}else {
NSLog(#"Some error occurred in Connection");
}
And I am getting following response. Not getting any value i.e. Celsius or Fahrenheit value in response.
Check it works now:
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://www.w3schools.com/webservices/\"> <Celsius>25.5</Celsius> </CelsiusToFahrenheit>"
"</soap:Body>\n"
"</soap:Envelope>\n"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit"];
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://www.w3schools.com/webservices/CelsiusToFahrenheit" 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");
}
Then get the response as:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *data=[[NSString alloc]initWithFormat:#"%#",webData ];
// NSLog(#"DONE. Received Bytes: %d, \n %#", [webData length],data);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"Response is: %#",theXML);
}
Afterwords you need to parse it.
Iam sending JSON Object request to the server but server returns Status Code 405. how to solve this problem. please any one help me.
My code :
+(NSData *)GpBySalesDetailed:(NSMutableDictionary *)spDetailedDict{
NSLog(#"spDetailedDict:%#",spDetailedDict);
NSString *dataString = [spDetailedDict JSONRepresentation];
NSLog(#"%#dataString",dataString);
return [dataString dataUsingEncoding:NSUTF8StringEncoding];
}
-(void)requestWithUrl:(NSURL *)url WithJsonData:(NSData *)JsonData
{
NSMutableURLRequest *urlRequest=[[NSMutableURLRequest alloc]initWithURL:#"http://srbisolutions.com/SmartReportService.svc/GpBySalesPersonDetailed];
if (JsonData != nil) {
[urlRequest setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:JsonData];
}
else
{
[urlRequest setHTTPMethod:#"GET"];
}
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
[conn start];
}
HTTP Code 405 means "Method not allowed", it does not accept a post request for this particular URI. Either the server must be configured to accept POST requests or it should offer another URI.
try this
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:YOURURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0 ];
NSLog(#"final request is %#",request);
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
//Here postData is a Dictionary with key values in web services format use ur own dic
[request setHTTPBody:[[self convertToJSON:postData] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentLength = [NSString stringWithFormat:#"%d",[[request HTTPBody] length]];
[request setValue:contentLength forHTTPHeaderField:#"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
self.responseData = [NSMutableData data];
}
//============JSON CONVERSION========
-(NSString *)convertToJSON:(id)requestParameters
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestParameters options:NSJSONWritingPrettyPrinted error:nil];
NSLog(#"JSON DATA LENGTH = %d", [jsonData length]);
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"JSON STR LENGTH = %d", [jsonString length]);
return jsonString;
}
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"yourURL"]];
[theRequest setHTTPMethod:#"POST"];
NSDictionary *jsonRequest =
[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#//add your objects
]
forKeys:[NSArray arrayWithObjects:
title,
link,
nil]];
NString *jsonBody = [jsonRequest JSONRepresentation];
NSLog(#"The request is %#",jsonBody);
NSData *bodyData = [jsonBody dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPBody:bodyData];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
// create the connection with the request
// and start loading the data
theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];