从具有RestKit的iOs发布到WCF

时间:2012-09-27 13:27:21

标签: ios wcf post restkit

我试图从Xcode(使用RestKit)发布到WCF服务的帖子。这是我的代码:

   NSDictionary *queryParams;
    queryParams = [NSDictionary dictionaryWithObjectsAndKeys:
                   @"1.0",@"oauth_version",
                   @"33333",@"oauth_nonce",
                   @"HMAC-SHA1",@"oauth_signature_method",
                   @"1323",@"oauth_timestamp",
                   @"rrr", @"oauth_consumer_key",
                   @"t672hpIerersdsfc", @"oauth_signature", nil];

    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://myServer:80/appccservice.svc"];
    objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];


    objectManager.client.baseURL = baseURL;
    objectManager.client.authenticationType = RKRequestAuthenticationTypeOAuth1;
    objectManager.client.username = @"lsantos";
    objectManager.client.password = @"clave";

    NSString *resourcePath = [@"/json/post" stringByAppendingQueryParameters:queryParams];

    [RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
    RKObjectMapping *userSerialization = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];

    [userSerialization mapKeyPath:@"name" toAttribute:@"nombreNovia"];
    [userSerialization mapKeyPath:@"tel" toAttribute:@"fecha"];


    RKObjectMapping *serialize = [userSerialization inverseMapping];


    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:serialize forClass:[User class]];

    [[RKObjectManager sharedManager].router routeClass:[User class] toResourcePath:resourcePath forMethod:RKRequestMethodPOST];

    User *data = [User new];
    data.nombreNovia = @"joaquin";
    data.fecha = @"4344";


    [[RKObjectManager sharedManager] postObject:data delegate:self];

我使用了RKLogConfigureByName并得到了这个:

请求:

URLRequest '<NSMutableURLRequest http://myServer:80/appccservice.svc/json/post?oauth_timestamp=32432304&oauth_nonce=123123&oauth_version=1.0&oauth_consumer_key=key&oauth_signature_method=HMAC-SHA1&oauth_signature=t672hasdfdsfsdsdceVBFqKc>'. HTTP Headers: {
    Accept = "application/json";
    "Accept-Encoding" = gzip;
    Authorization = "OAuth oauth_signature=\"bEmMvO3sdfsdfsdfcdc\"";
    "Content-Length" = 31;
    "Content-Type" = "application/json";
}. HTTP Body: {"tel":"4344","name":"joaquin"}.

我认为该请求的一切正常,数据在HTTP Body中。

同时在回应中:

   Connection = "Keep-Alive";
    "Content-Length" = 7;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Thu, 27 Sep 2012 12:34:08 GMT";
    "Proxy-Connection" = "Keep-Alive";
    Server = "Microsoft-IIS/7.5";
    Via = "1.1 ISASERVERSM";
    "X-Powered-By" = "ASP.NET";
}

当对象为null时,我的方法返回空,因此WCF没有看到数据。

2012-09-27 08:34:05.056 RESTPRUEBA2[6698:fb03] T restkit.network:RKResponse.m:231 Read response body: "empty"

我在didFailLoadWithError

上得到了这个错误
2012-09-27 09:11:25.500 RESTPRUEBA2[6698:fb03] error encontrado: Error Domain=JKErrorDomain Code=-1 "Expected either '[' or '{'." UserInfo=0x88d89c0 {JKAtIndexKey=7, JKLineNumberKey=1, NSLocalizedDescription=Expected either '[' or '{'.}

有趣的是,我在firefox中使用具有相同URL和数据的RestClient插件证明了我的WCF,并且一切正常。

1 个答案:

答案 0 :(得分:0)

此问题表明远程系统返回的JSON无法正确解析。 JKErrorDomain来自JSONKit,即正在使用的JSON解析器。获取您的响应正文并在http://jsonlint.com/上对其进行验证并修复解析错误。