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.
WebMethods are SOAP I believe, so rather than doing it manually why not use an API for handling SOAP? Such as wsdl2objc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With