Upload iPhone UIImage to a web service

I worked on this for a few hours today, and I'm pretty close to a solution, but clearly need help from someone who shot it. I am trying to send an image to a web service from an iPhone. I will send the code first, and then explain everything I tried:

NSData *imageData = UIImageJPEGRepresentation(barCodePic, .9); NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?><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><WriteImage xmlns=\"http://myserver/imagewebservice/\"><ImgIn>%@</ImgIn></WriteImage></soap:Body></soap:Envelope>", [NSData dataWithData:imageData] ]; NSURL *url = [NSURL URLWithString:@"http://myserver/imagewebservice/service1.asmx"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:@"http://myserver/imagewebservice/WriteImage" forHTTPHeaderField:@"SOAPAction"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; [req setHTTPMethod:@"POST"]; [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if (conn) { webData = [[NSMutableData data] retain]; } 

Firstly, this code is great for everything except the image. The web service works on my local network, and I can change the source code as desired, and if I change the "ImgIn" parameter to a string and pass the string, everything will be fine, I get the return value without problems. Thus, there are no connection problems at all, I can call and get data from this web service on this server without problems. But I need to upload the image to this web service through the ImgIn parameter, so the code above is my best shot. I also have didReceiveResponse, didReceiveData, didFailWithError etc. All are being processed. The above code fires every time a reRecieveResponse request is executed. However, didReceiveData never starts, and it looks like the web service never starts. When I debug the web service itself, it works and is fine debugged when I use the string parameter, but with the image parameter it never debugs when I call it. This is almost the same as the ImgIn parameter is too long (it is huge when I put it on the screen) and the web service is just suffocating. I read about the need to encode Base64 when using this method, but I cannot find any good references to how this is done. If this is what I am doing wrong, can you GET the code on how to do it, and not just “you need to use Base64”, I would really appreciate it, since I can hardly find anything how to implement this using an example. In addition, I was lost, it seems that I am doing everything right. Please, help!

thanks

+4
source share
1 answer

It fails because the% @ format specifier takes an object and calls a description (NSString *) to turn it into a string. - [Description of NSData] returns something like @ "<31 32 33 65 66 67>" for (ASCII) "123abc", so you are sending <31 32 33 65 66 67>, which is clearly not valid XML. Do not glue strings together (this, in particular, is vulnerable to XML embedding).

I would reset SOAP and just use

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

If you want to use SOAP, then there is probably an Objective-C SOAP library that will do the packaging for you. Otherwise, you can manually specify xs: base64Binary (I think) and use base64-encoded data. None of them are perfect, because the data is 33% more. If you're out of luck, the SOAP library will make it 20,000% bigger !

+6
source

Source: https://habr.com/ru/post/1310815/


All Articles