我有一个数据对象,称为 DataElement。它包含一串经过 Base64 转换的图像字节,以及几个其他字段。
我正在尝试将此发布到我的 wcf 服务并收到错误消息“预期状态代码在 (200-299) 中,得到 400。
目标是将数据+图像发布到 WCF(休息)服务,并获得修改后的图像——对我正在处理的内容进行端到端测试。
在我的 post 方法中,如果我将对象上的编码字符串留空,一切都会正常工作 - 但如果该字符串不是空的,我会收到此错误。
我的 WCF 服务甚至没有被击中,它只是轰炸了错误。这是我的发布方法......我做错了什么?
- (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];
}
编辑:抱歉,当我将其粘贴到格式时,格式变得不稳定...