0
任何人都可以帮我吗?我长期困扰这个问题。iOS JSON与ASPNET Web服务
我正在开发一个IOS来将JSON引入Web服务以便插入到数据库中。 但未能做到。
IOS源代码
-(IBAction)signup{
NSMutableDictionary * dic = [NSMutableDictionary dictionary];
[dic setObject:@"0" forKey:@"account_id"];
[dic setObject:@"Jin" forKey:@"first_name"];
[dic setObject:@"Lim" forKey:@"last_name"];
[dic setObject:@"1987-12-02" forKey:@"date_of_birth"];
[dic setObject:@"M" forKey:@"account_gender"];
[dic setObject:@"65" forKey:@"phone_country_id"];
[dic setObject:@"123456" forKey:@"account_phone"];
[dic setObject:@"[email protected]" forKey:@"account_login"];
[svc execMethod:dic];
}
然后
-(void) execMethod:(NSMutableDictionary *)dic{
NSURL * url = [NSURL URLWithString:@"http://test.biz/AccountInfo.asmx/insert_account"];
//Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData* requestData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
//prepare http body
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d",[requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
urlConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
}
Web服务
{WebMethod()} _
{ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)}_
Public Function insert_account(ByVal result As Account) As String
Dim account As New Account
account = result
Dim msg As String
**Processing Result***
Return "msg:'OK'"
End Function
P/S:我用{}来代替> <为web服务方法
错误的NSLog
2012-02-02 16:25:11.433 test.biz[10642:f803] System.InvalidOperationException: insert_account Web Service method name is not valid.
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
烦请告诉我,我有什么错完成,感谢您的帮助:)
你能从浏览器访问网页方法吗? – AnthonyBlake 2012-02-02 11:51:47
也是,你使用的是什么版本的.net? – AnthonyBlake 2012-02-02 11:55:24
你测试过你的网络服务吗?我的猜测是,您没有正确配置Web服务,无论是在web.config文件中还是使用注释(例如'')。我暂时把XCode项目放在一边,启动Fiddler,在那里建立请求并用它测试Web服务。 –
Codo
2012-02-02 12:27:40