I am sending an image to the server by converting the NSData object into an NSString object using Base64 scheme by using….
NSData *imgData=UIImagePNGRepresentation([objCP userImage]);
NSString *encodedString=[Base64Coder encodeData:imgData];
I am observing a random behavior….sometimes I get “==” in the encoded string at the end of the string and image does not uploaded.There may be some other characters also in between the string.If I do not get these characters at the end…image gets uploaded.
To overcome this…I also used this method to convert these characters into valid(assumed to be accepted)::
-(NSString *)urlEncodedVersion:(NSString *)strString
{
NSMutableString *strTemp = [[NSMutableString alloc] initWithFormat:@"%@",strString] ;
NSArray *escapeChars = [NSArray arrayWithObjects:@";",@"?",@":",@"@", @"&",@"=",@"+",@"$",@",", @"[",@"]",@"#",@"!",@"’",@"(", @")",@"*",@" ",nil];
NSArray *replaceChars = [NSArray arrayWithObjects: @"%3B",@"%3F",@"%3A",
@"%40",@"%26",@"%3D", @"%2B",@"%24",@"%2C",@"%5B",@"%5D", @"%23",@"%21",@"%27", @"%28",@"%29",@"%2A",@"%20",nil];
//NSMutableString *tempStr = [[self mutableCopy] autorelease];
for(int i = 0; i < [escapeChars count]; i++)
{
[strTemp replaceOccurrencesOfString:[escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0,[strTemp length])];
}
return strTemp;
}
but it is also not serving.
This is my whole post body::
-(void)uploadProfileInfo:(CreateProfile *)objCP
{
NSData *imgData=UIImagePNGRepresentation([objCP userImage]);
NSString *encodedString=[Base64Coder encodeData:imgData];
NSString *refinedString=[self urlEncodedVersion:encodedString];
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<soapenv:Envelope \n"
"xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
"xmlns:tem=\"http://tempuri.org/\" \n"
"xmlns:wcf=\"http://schemas.datacontract.org/2004/07/NextToMe_BusinessEntity\"> \n"
"<soapenv:Header/>\n"
"<soapenv:Body>\n"
"<tem:CreateProfile>\n"
"<tem:objUser>\n"
"<wcf:Email>%@</wcf:Email>\n"
"<wcf:Mode>%@</wcf:Mode> \n"
"<wcf:Name>%@</wcf:Name>\n"
"<wcf:ProfileImage>%@</wcf:ProfileImage>\n"
"<wcf:RequestDateTime>%@</wcf:RequestDateTime>\n"
"<wcf:Status>%@</wcf:Status>\n"
"<wcf:StatusSpecified>%@</wcf:StatusSpecified>\n"
"<wcf:UDID>%@</wcf:UDID>\n"
"</tem:objUser>\n"
"</tem:CreateProfile>\n"
"</soapenv:Body>\n"
"</soapenv:Envelope>\n",[objCP email],[objCP mode],[objCP name],encodedString,@"",[objCP status],[objCP statusSpecified],[objCP UDID]];
NSURL *url = [NSURL URLWithString:kBaseURL];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/XXXX/CreateProfile" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"The Connection is NULL");
}
}
The = characters you see at the end are padding. This is according to RFC4648 3.2. Padding of Encoded Data. The other end should be able to digest that. Apparently that is not happening. You should compare the behavior with a third party library to check whether the encoding or decoding is wrong. If you can make it work removing the padding, then good for you, but that’s a fault in the decoding library.