使用AFNetworking 2上传图像以及其他参数

时间:2014-10-16 00:39:05

标签: ios xml file-upload base64 afnetworking-2

我正在尝试将图片从我的应用程序发送到服务器。这是一个带有.net Web服务的Windows服务器。

我正在尝试呼叫的服务收到三个参数:一个数字标识符(用户ID)告诉服务器哪个用户正在发送新图片,图像作为base64编码字符串,以及图像格式,它总是一个.png。

我的上传功能是:

- (void)updateProfilePic:(UIImage *)profilePic forUser:(User *)user
{
    //Encode the image in the requested format
    NSData *imageData = UIImagePNGRepresentation(profilePic);
    NSString *encodedString = [imageData base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];

    //Build the URL string
    NSURL *url = [NSURL URLWithString:BaseURL];

    NSString *soapBody = [NSString stringWithFormat:
                          @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                          "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                          "<soap:Body>\n"
                          "<UpdateUserProfilePic_iOS xmlns=\"http://mycompany.com/webservices/>\n"
                          "<userId>%d</userId>\n"
                          "<imgbinStr>%@</imgbinStr>\n"
                          "<imgFormat>.png</imgFormat>\n"
                          "</UpdateUserProfilePic_iOS>\n"
                          "</soap:Body>\n"
                          "</soap:Envelope>",
                          (int)user.userId, encodedString];
    NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapBody length]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:@"\"http://mycompany.com/webservices/UpdateUserProfilePic_iOS\"" forHTTPHeaderField:@"SOAPAction"];
    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        //parse NSXMLParser object here if request successful
        if ([responseObject isKindOfClass:[NSXMLParser class]]) {
            XMLParser *xml = [[XMLParser alloc] init];
            NSString *responseString = [xml parseResponse:responseObject];
            NSLog(@"Update picture response: %@", responseString);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        NSLog(@"Error: %@", error);
    }];
    [operation start];
}

到目前为止,响应是一个错误。但是,错误是400,这让我觉得问题与我格式化请求的方式有关:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7999aa20 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x799858c0> { URL: http://myserverIP/UserProfileSvc.asmx } { status code: 400, headers {
    "Cache-Control" = private;
    "Content-Length" = 0;
    "Content-Type" = "text/xml; charset=utf-8";
    Date = "Thu, 16 Oct 2014 00:03:53 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://myserverIP/UserProfileSvc.asmx, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<>}

我不认为问题出在服务器端。我可能错误地格式化了一些东西,但我不知道那可能是什么。提前致谢

0 个答案:

没有答案