Posting a serialized object with AFNetworking fail

2019-09-11 13:02发布

问题:

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

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

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.

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.

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];
}

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

回答1:

The important parts of your code are:

NSString* urlString = [server stringByAppendingString:@"EchoXml"];    
NSURL *url = [NSURL URLWithString:urlString];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];    
NSMutableURLRequest *request = [httpClient 
    requestWithMethod:@"POST" path:urlString parameters:nil];

The actual URL that AFNetorking requests is the AFHTTPClient's base URL, with the specified path appended to it.

Your mistake is that you are specifying the same urlString again.

So, if urlString is http://your.server.com/EchoXml, then the effective URL that you're requesting is http://your.server.com/EchoXmlhttp://your.server.com/EchoXml. As you see, that doesn't work.

Fix your base URL and path to be something more appropriate. Since you didn't say what URL you are trying to access, it's hard to give much more detail. Maybe server should be the base URL, and EchoXml the path?



回答2:

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.

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.