我是一名 .Net 软件开发人员。我为用户验证创建了一个 WCF Rest API 以下是它的签名。
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/Validate", Method = "POST")]
UserListResponse ValidateUser(Login obj);
它在提琴手中运行良好,具有以下请求正文
{"Username":"admin",Password:"111"}
我的问题是,无法从 Iphone 使用 http post 方法访问它,但它在 Android Http post 方法中运行良好。
以下是我的代码:
NSError *theError = nil;
NSArray *keys = [NSArray arrayWithObjects:@"Username", @"Password", nil];
NSArray *objects = [NSArray arrayWithObjects:@"admin", @"111", nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *myJSONString =[jsonDictionary JSONRepresentation];
NSData *myJSONData =[myJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"myJSONString :%@", myJSONString);
NSLog(@"myJSONData :%@", myJSONData);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate"]];
[request setHTTPBody:myJSONData];
[request setHTTPMethod:@"POST"];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSLog(@"response : %@", theResponse);
NSLog(@"error : %@", theError);
NSLog(@"data : %@", data);
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string: %@", string);
NSDictionary *jsonDictionaryResponse = [string JSONValue];
NSLog(@"dic: %@", jsonDictionaryResponse);
以下是我的回复错误:
2012-07-06 11:49:04.201 ShopMonkey [524:207] -JSONRepresentation 失败。错误跟踪是:(空)
2012-07-06 11:49:04.201 ShopMonkey[524:207] myJSONString:{“密码”:“111”,“用户名”:“管理员”}
2012-07-06 11:49:04.201 ShopMonkey[524:207] myJSONData:<7b225061 7373776f 7264223a 22313131 222c2255 7365726e 616d6522 3a226164 6d696e22 7d>
2012-07-06 11:51:13.312 ShopMonkey[524:207] 回复:(空)
2012-07-06 11:51:13.312 ShopMonkey[524:207] 错误:错误域=NSURLErrorDomain 代码=-1005“网络连接丢失。” UserInfo=0x6041370 {NSErrorFailingURLStringKey=http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate, NSErrorFailingURLKey=http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate, NSLocalizedDescription=网络连接丢失。, NSUnderlyingError =0x6043a90 "网络连接丢失。"}
2012-07-06 11:51:13.312 ShopMonkey[524:207] 数据:(空)
2012-07-06 11:51:13.313 ShopMonkey[524:207] 字符串:
2012-07-06 11:51:13.313 ShopMonkey [524:207] -JSONValue 失败。错误跟踪是:(
"Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x6041b90 {NSLocalizedDescription=Unexpected
字符串结束}"
)
2012-07-06 11:51:13.313 ShopMonkey[524:207] dic: (null)
谁能指出我出了什么问题。
在此先感谢维杰
查看我的错误详细信息
错误详情:
2012-07-06 16:54:37.416 ShopMonkey[1895:40b]-[CJSONSerializer serializeDictionary:]:无法识别的选择器发送到实例 0x4c03740
2012-07-06 16:54:37.417 ShopMonkey[1895:40b] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[CJSONSerializer serializeDictionary:]:无法识别的选择器发送到实例 0x4c03740”
*在第一次抛出时调用堆栈:
(
0 核心基础 0x00ec2be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x010175c2 objc_exception_throw + 47
2核心基础0x00ec46fb-[NSObject(NSObject)不识别选择器:]+187
3 CoreFoundation 0x00e34366转发+966
4 核心基础 0x00e33f22 _CF_forwarding_prep_0 + 50
5 ShopMonkey 0x000030c5 -[ShopMonkeyViewController signinbtnclick] + 425
6 UIKit 0x0034fa6e-[UIApplication sendAction:to:from:forEvent:] + 119
7 UIKit 0x003de1b5-[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x003e0647-[UIControl(内部)_sendActionsForEvents:withEvent:] + 527
9 UIKit 0x003df1f4-[UIControl touchesEnded:withEvent:] + 458
10 UIKit 0x003740d1-[UIWindow_sendTouchesForEvent:] + 567
11 UIKit 0x0035537a -[UIApplication 发送事件:] + 447
12 UIKit 0x0035a732 _UIApplicationHandleEvent + 7576
13 图形服务 0x0177ca36 PurpleEventCallback + 1550
14 核心基础 0x00ea4064 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52
15 核心基础 0x00e046f7 __CFRunLoopDoSource1 + 215
16 核心基础 0x00e01983 __CFRunLoopRun + 979
17 核心基础 0x00e01240 CFRunLoopRunSpecific + 208
18 核心基础 0x00e01161 CFRunLoopRunInMode + 97
19 图形服务 0x0177b268 GSEventRunModal + 217
20 图形服务 0x0177b32d GSEventRun + 115
21 UIKit 0x0035e42e UIApplicationMain + 1160
22 商店猴子 0x000026d8 主 + 102
23 ShopMonkey 0x00002669 开始 + 53
)
在抛出“NSException”实例后调用终止