在AFNetworking失败的情况下发布序列化对象

问题描述:

我有一个数据对象,称为DataElement。它包含一个由Base64转换后的图像字节的字符串,以及其他几个字段。

I have a a data object, called DataElement. It contains a string of Base64 converted image bytes, along with a couple of other fields.

我试图将其发布到我的wcf服务中并收到错误消息 Expected (200-299)中的状态代码,则得到400。

I am trying to post this to my wcf service and am getting an error 'Expected status code in (200-299), got 400.

目标是将数据+图片发布到WCF(剩余)服务,并获得修改后的图片

The goal is to post data + an image to the WCF (rest) service, and get a modified image back- an end to end test of what I am working on.

在我的post方法中,如果我在对象上保留编码字符串为空,则一切正常,但是该字符串不是空字符串,我会收到此错误。

In my post method, if I leave the encoded string empty on the object everything works just fine- but if that string is anything other than empty I get this error.

我的WCF服务甚至都没有被击中,它只是炸向错误。这是我的发布方法...我在做什么错了?

My WCF service isn't even being hit, it just bombs right to the error. Here is my post method... what am I doing wrong?

- (void)postDataToServer:(NSString*)server dataElement:(DataElement*)dataElement asJson:(BOOL)useJson
{
NSString *urlString = [[NSString alloc] init];
NSData *encodedData;

urlString = [[server copy] stringByAppendingString:@"EchoXml"];
encodedData = [self encodeDataElementAsXml:dataElement];

NSURL *url = [NSURL URLWithString:urlString];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:urlString parameters:nil];
[request setHTTPBody:encodedData];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    [_responseTextView setText:[NSString stringWithFormat:@"Successfully uploaded file to %@", urlString]];

    NSObject *httpResponseObject;

    httpResponseObject = [self parseResponseAsXml:responseObject];

    if ([httpResponseObject isKindOfClass:[DataElement class]])
    {
        DataElement *dataElement = (DataElement *)httpResponseObject;
        _responseTextView.text = dataElement.DataText;
        if (dataElement.DataImageBase64 != nil)
        {
            UIImage *dataImage = [self getImageFromString:dataElement.DataImageBase64];
            self.responseImageView.image = dataImage;
        }
    }

    NSLog(@"Successfully uploaded file to %@", urlString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // It goes here immediately
    [_responseTextView setText:[NSString stringWithFormat:@"Error: %@", error]];
    NSLog(@"Error: %@", error);
}];

[operation start];
}

编辑:对不起,当我将其粘贴到...中时,格式就很奇怪...

Sorry the formatting got wonky when I pasted it in...

我知道回答我自己的问题的方式很糟糕-但我找到并解决了这个问题。最重要的是,我上面使用的代码很好-也许不是最佳的(正如Kurt所指出的),但是它确实应该做的。

I know its bad form to answer my own question- but I found and fixed the problem. Bottom line, the code I was using above is fine- maybe not optimal (as Kurt pointed out) but it does what it is supposed to do.

问题出在在我的WCF服务上-默认情况下,REST服务请求的上传限制为65k。我将服务重新配置为允许上传大文件,并且一切都很好。

The problem was on on my WCF service- REST service requests by default have a 65k upload limit. I reconfigured the service to allow large file uploads and everything is good.