Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C# webMethod in Objective C

I'm trying to communicate with this simple C# webMethod:

[WebMethod()]
public static string login(string userName)
{
     if (userName == "test"){
         return "validated";
     }
     return "error";
}

I'm trying to connect to it with the following Objective C code:

NSString *userName = @"test";
NSString *post = [NSString stringWithFormat:@"userName=%@", userName];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSString *serverURLString = @"http://myURL.aspx/login";
NSURL *serverURL = [NSURL URLWithString:serverURLString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
[request setHTTPMethod:@"POST"];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"result = %@", result);

rather than printing out "validated" or "error", I get:

result = <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="login" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"   value="/wEPDwULLTE2MTY2ODcyMjlkZFwRG2UySOwu+02xaz+VP6mmD60UBRkXvHCHxGohOt9d" />
<div>    
</div>
</form>
</body>
</html>

Can anyone tell me if there is something obviously wrong with either the web method or the Objective C code? Thanks.

like image 326
Robert Avatar asked Jan 19 '26 06:01

Robert


1 Answers

WebMethods are SOAP I believe, so rather than doing it manually why not use an API for handling SOAP? Such as wsdl2objc.

like image 168
Michael Lloyd Lee mlk Avatar answered Jan 20 '26 20:01

Michael Lloyd Lee mlk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!