尝试连接以下 Web 方法时,返回“请求格式无效”:
public string myWebMethod(string test)
{
return "connected";
}
这是目标 C 代码:
NSString *seshID = @"test-session";
NSString *post = [NSString stringWithFormat:@"test=%@", seshID];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *comparisonURLString = SERVER_COMPARE_URL_STRING;
NSURL *comparisonURL = [NSURL URLWithString: comparisonURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:comparisonURL];
[request setHTTPMethod:@"POST"];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(response);
但是,如果我更改 web 方法使其不带参数:
public string myWebMethod()
并将我的 obj-c 代码中的相应行更改为:
NSString *post = [NSString stringWithFormat:@""];
它有效,响应:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">connected</string>
谁能解释为什么后者似乎有效但前者无效?谢谢。