Search code examples
ios5soapuiimagensdata

How to upload image + xml into web server using SOAP request from iphone application


I am implementing an iPad application. In my application I need to get some information and profile pic from user and need to upload that information to a web server using SOAP request. I am done with text inputs uploading. But when it comes to image I am not aware of that. I know, to upload an image we need to convert that to NSData I did it, but how can I add that to my post string?

My code is as follows:

NSData* imageData = UIImagePNGRepresentation(myPic);
NSString* uploadValues=@"";
uploadValues = [uploadValues stringByAppendingString:[NSString stringWithFormat:@"<UserInformation><UserInfo action=\"INSERT\"><Userid>123</Userid><FirstName>abc</FirstName><LastName>xyz</LastName><Contact>00000000</Contact><Email>[email protected]</Email><Address>USA</Address><Image>How can I put image data here? is that work if I put data here?</Image></UserInfo></UserInformation>"];
        uploadValues = [uploadValues stringByReplacingOccurrencesOfString:@"(null)" withString:@" "];
        serverConnections=[ServerConnections sharedConnections];
        serverConnections.delegate=self;

        NSString* soapString = [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/\"> \n"
                                "<soap:Body>\n"
                                "<SetUserInfo xmlns=\"http://example.com/\">\n"
                                "<Data><![CDATA[ %s ]]></Data> \n"
                                "</SetUserInfo> \n"
                                "</soap:Body>\n"
                                "</soap:Envelope>\n",[uploadValues UTF8String]];

        printf("\n Input string:%s",[soapString UTF8String]);


        NSURL *url = [NSURL URLWithString:@"http://example.com/xyz.asmx"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapString length]];

        [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [theRequest addValue: @"http:/example.com/SetUserInfo" forHTTPHeaderField:@"SOAPAction"];
        [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];
        [serverConnections startEstablishingConnectionwithServer:theRequest];

Solution

  • From Here

     [req addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
     [req setHTTPBody:imgData]