This is my xml
NSString *Message = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<query>\n"
"<IdCategory>1088</IdCategory>\n"
"</query>\n"];
NSURL *url = [NSURL URLWithString:#"http://bvd.vndsupport.com/_ws/api/categories/index.asp"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [Message length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [Message dataUsingEncoding:NSUTF8StringEncoding]];
NSString *returnString = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
I have simply pass this category id and get the result but returnString always display all category details. I didn't get an exact result. Is this the correct way of passing xml data to webservice?
go and search for 'wsdl2objc' it does it all for you if you wanted to do soap requests.
This code will work perfectly. the problem is webservice page
Related
I have used the SOAP webservice in my app. I have sent the SOAP request to server and the getting the resulst in the XML formats. I am using NSXML Parser for parsing the data.
I want to get the data "true", when i have tried to parse the content application get crashed, I couldn't parsed that data.
<ServiceFinalResult>
<Response xmlns="http://aaaa.com/test/schemas/services/myaccounts/data/_1_0">
true
</Response>
</ServiceFinalResult>
So please help me out.
try this code.
NSString *soapMessage = #"<?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"
"<name>testName</name>\n"
"</soap:Body>\n"
"</soap:Envelope>\n";
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];
I am implementing an iPad application. In my application I need to get some information and profile pic from user and need to upload that information to a web server using SOAP request. I am done with text inputs uploading. But when it comes to image I am not aware of that. I know, to upload an image we need to convert that to NSData I did it, but how can I add that to my post string?
My code is as follows:
NSData* imageData = UIImagePNGRepresentation(myPic);
NSString* uploadValues=#"";
uploadValues = [uploadValues stringByAppendingString:[NSString stringWithFormat:#"<UserInformation><UserInfo action=\"INSERT\"><Userid>123</Userid><FirstName>abc</FirstName><LastName>xyz</LastName><Contact>00000000</Contact><Email>abc#googole.com</Email><Address>USA</Address><Image>How can I put image data here? is that work if I put data here?</Image></UserInfo></UserInformation>"];
uploadValues = [uploadValues stringByReplacingOccurrencesOfString:#"(null)" withString:#" "];
serverConnections=[ServerConnections sharedConnections];
serverConnections.delegate=self;
NSString* soapString = [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"
"<SetUserInfo xmlns=\"http://example.com/\">\n"
"<Data><![CDATA[ %s ]]></Data> \n"
"</SetUserInfo> \n"
"</soap:Body>\n"
"</soap:Envelope>\n",[uploadValues UTF8String]];
printf("\n Input string:%s",[soapString UTF8String]);
NSURL *url = [NSURL URLWithString:#"http://example.com/xyz.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapString length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http:/example.com/SetUserInfo" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];
[serverConnections startEstablishingConnectionwithServer:theRequest];
From Here
[req addValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
[req setHTTPBody:imgData]
I want to access a webservice, want to pass 2 parameters.
When I run the code below, this error is shown:
#countryname not supplied
I already pass 2 parameters as txtcity and txtcountry.
-(IBAction)FindWords:(id)sender
{
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>"
"<GetWeather xmlns=\"http://www.webserviceX.NET/\">"
"<CityName>%#</CityName>"
"<CountryName>%#</CountryName>"
"</GetWeather>"
"</soap:Body>"
"</soap:Envelope>", txtCity.text,txtCounrty.text];
//---print it to the Debugger Console for verification---
NSLog(#"%#",soapMsg);
NSURL *url = [NSURL URLWithString:
#"http://www.webservicex.net/globalweather.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
NSLog(#"WebData....%#",soapMsg);
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://www.webserviceX.NET/GetWeather" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//---start animating--
[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(conn)
{
webData = [[NSMutableData data] retain];
NSLog(#"WebDatanew....%#",webData);
}
}
just include ASIHTTPRequest.Its much more easier to use.
http://allseeing-i.com/ASIHTTPRequest/
the web service takes three parameters:
rw_app_id: The unique identifier for the app. If you’ve been following along with the previous tutorial, there should be only one entry so far, App ID #1.
code: The code to attempt to redeem. This should be a string that’s entered by the user.
device_id: The device ID that is attempting to redeem this code. We can get this with an easy API call.
use:- ASIHTTPRequest
change your soapMsg format :
NSString *soapMsg = [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"
"<GetWeather xmlns=\"http://www.webserviceX.NET\">\n"
"<CityName>%#</CityName>"
"<CountryName>%#</CountryName>\n"
"</GetWeather>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",txtCity.text,txtCounrty.text];
Use ASIHTTP Request and JSON Library to communicate with your web service. JSON is way better than XML feeds and its very easy to handle.
Download JSON library form Here
Here is the Documentation for ASIHTTP request :- http://allseeing-i.com/ASIHTTPRequest/
Hope this will help you. Thank you
May be this can help you. It contain one parameter
-(void)serverconnection{
NSString *CountryName=#"India";
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>"
"< GetWeather xmlns=\"http://tempuri.org/\">"
"<CountryName>%#</CountryName>"
"</GetWeather >"
"</soap:Body>"
"</soap:Envelope>",CountryName];
NSURL *myNSUObj=[NSURL URLWithString:#"http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry"];
// NSURLRequest *myNSURequestObj=[NSURLRequest requestWithURL:myNSUObj];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myNSUObj];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/GetWeather" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
myNSUConnectionObj=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
NSLog(#"Data =%#",myNSUConnectionObj);
if(myNSUConnectionObj)
{
NSLog(#"successful connection");
myNSMDataFromServer=[[NSMutableData alloc]init];
}
}
Your webservice is different but may be this can give you idea.
I am doing with soap request/response app. I have login page.That have user id and password. This is my soap message.
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"
"<SignOn xmlns=\"http://10.12.50.99/CUTechWebservices/CuTechWebService.asmx\">\n"
"<DeviceID>4A6B740C-0B5B-5F8C-8694-1EC0196C1C67</DeviceID>\n"
"<UserID>string</UserID>\n"
"<CrID>6</CrID>\n"
"<UserPin>string</UserPin>\n"
"</SignOn>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
As user id and password are diff for users, I want to pass the values from the textfields (user id and password) to the soap message.
How to do this?
Thanks in advance.
OK So below is some steps to follow. I am not sure exact code you can use or not. But modify it as per your need.
For Calling Request Do something like this :
NSMutableURLRequest *request = [WebAPIRequest buildSOAPRequest:#"WebserviceURL" withPostData:strRequest WebMethod:#"YourWebMethodHereForExample-ValidateUSer"];
Connection *con = [[[Connection alloc]initWithClass:#"getClientResult" withRoot:#"soap:Body" withDelegate:delegate withTag:tag]autorelease];
[[[NSURLConnection alloc]initWithRequest:request delegate:con] autorelease];
BuildDataRequest should be like this.
+(NSMutableURLRequest *)buildSOAPRequest:(NSString *)serviceUrl withPostData:(NSString *)dataString WebMethod:(NSString*)strMethod{
NSURL *url = [NSURL URLWithString:serviceUrl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [dataString length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
if (isRegion==YES)
{
isRegion = NO;
[theRequest addValue:[NSString stringWithFormat:#"%#%#",NSLocalizedString(#"SOAP Action1",nil),strMethod] forHTTPHeaderField:#"SOAPAction"];
}else
{
[theRequest addValue:[NSString stringWithFormat:#"%#%#",NSLocalizedString(#"SOAP Action",nil),strMethod] forHTTPHeaderField:#"SOAPAction"];
}
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [dataString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",dataString);
return theRequest;
}
Hope this help.
i am trying to parse soap web service for authentication and following is the code
currentElement = [[NSString alloc] init];
NSString* email=[NSString stringWithFormat:#"sbhushan#gmail.com"];
NSString* password=[NSString stringWithFormat:#"5Pb&Q?4t"];
NSString* soapMesasge=[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>"
"<Authentication xmlns=\"http://tempuri.org/\">"
"<userId>%#</userId>"
"<password>%#</password>"
"</Authentication>"
"</soap:Body>"
"</soap:Envelope>",email,password];
NSURL* url=[NSURL URLWithString:#"http://codeexsolutions.com/HosService/ServiceHOSProvider.asmx"];
NSMutableURLRequest* theRequest=[NSMutableURLRequest requestWithURL:url];
NSString* msgLength=[NSString stringWithFormat:#"%d",[soapMesasge length]];
[theRequest addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue:#"http://tempuri.org/Authentication" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:[soapMesasge dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
webData =[[NSMutableData alloc]retain];
}
else
{
// Inform the user that the connection failed.
}
and imlemented all the delegate method of parsing but do not getting call in
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
or not getting any data please help
hi Friend
your method looks OK dont know why not working , u see any data on consol?? try this or take a look on webservices may be data not come from there
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><GetClientData xmlns=\"http://tempuri.org/\"><pSearch>%#</pSearch></GetClientData></soap:Body></soap:Envelope>\n", nameInput.text];
NSURL *url = [NSURL URLWithString:#"http://192.168.1.162/iphone/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://tempuri.org/GetClientData" 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");
}