First of all create a response data as NSMutableData and class ".h" file
@property (nonatomic) NSMutableData *responseData;
Now Just do as follow in class ".m" file
-(void)validateUser:(NSString *)userId andPassword:(NSString *)userPassword
{
NSString *envelopeText = [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>"
"<ValidateUser xmlns=\"http://tempuri.org/\">"
"<emailId>%@</emailId>"
"<password>%@</password>"
"</ValidateUser>"
"</soap:Body>"
"</soap:Envelope>", userId, userPassword];
envelopeText = [NSString stringWithFormat:envelopeText, userId, userPassword];
NSData *envelope = [envelopeText dataUsingEncoding:NSUTF8StringEncoding];
// construct request
NSString *url = @"http://mysiteurl.com/allWebSevice.asmx";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:envelope];
[request setValue:@"text/xml;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// Remember to put text/xml;charset=utf-8 in code, otherwise no result will be fetched
[request setValue:[NSString stringWithFormat:@"%d", [envelope length]] forHTTPHeaderField:@"Content-Length"];
// fire away
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
self.responseData = [NSMutableData data];
else
NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"connection didFailWithError: %@ %@", error.localizedDescription,
[error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"DONE. Received Bytes:%d",[self.responseData length]);
NSString *theXml = [[NSString alloc] initWithBytes:[self.responseData mutableBytes] length:[self.responseData length] encoding:NSUTF8StringEncoding];
NSLog(@"The final result :%@",theXml);//Here you will get the result xml in string
}
No comments:
Post a Comment