我正在尝试与这个简单的 C# webMethod 进行通信:
[WebMethod()]
public static string login(string userName)
{
if (userName == "test"){
return "validated";
}
return "error";
}
我正在尝试使用以下目标 C 代码连接到它:
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);
而不是打印出“已验证”或“错误”,我得到:
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>
谁能告诉我 Web 方法或 Objective C 代码是否有明显问题?谢谢。