http post - xcode - uploading an image with form parameters to a webserver -



http post - xcode - uploading an image with form parameters to a webserver -

i tring upload image 3 parameters fror form webserver unsuccessful.

my code is:

-(ibaction)uploadphoto:(id)sender{ nsstring *urlstring = @"https://urlwebservice"; nsurl *url = [nsurl urlwithstring:urlstring]; nsstring *servername = @"user"; nsstring *serverpassword = @"password"; // create plaintext string in format username:password nsstring *authstr = [nsstring stringwithformat:@"%@:%@", servername, serverpassword]; nsdata *authdata = [authstr datausingencoding:nsutf8stringencoding]; nsstring *authvalue = [nsstring stringwithformat:@"basic %@", [base64 encode:authdata ]]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl: url cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 10]; [request sethttpmethod:@"post"]; [request setvalue:authvalue forhttpheaderfield:@"authorization"]; // body nsmutabledata *postbody = [nsmutabledata data]; nsstring *stringboundary = @"0xkhtmlboundary---this_is_the_boundaryy---pqo"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",stringboundary]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; //image [postbody appenddata:[[nsstring stringwithformat:@"--%@\r\n", stringboundary] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-disposition: form-data; name=\"file\"; filename=\"item.jpg\"\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-type: image/jpg\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-transfer-encoding: binary\r\n\r\n" datausingencoding:nsutf8stringencoding]]; // image info main bundle straight nsdata object nsdata *imgdata = uiimagejpegrepresentation(photo, 0.2); // add together body [postbody appenddata:imgdata]; [postbody appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; nslog(@"message added"); // final boundary [postbody appenddata:[[nsstring stringwithformat:@"--%@--\r\n", stringboundary] datausingencoding:nsutf8stringencoding]]; //parameters body nsstring *nombre = @"parameter_name" ; nsstring *valoracion = @"2"; nsstring *precio = @"12" ; //nombre [postbody appenddata:[[nsstring stringwithformat:@"--%@\r\n", stringboundary] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-disposition: form-data; name=\"nombre\"\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[nombre datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; //val [postbody appenddata:[[nsstring stringwithformat:@"--%@\r\n", stringboundary] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-disposition: form-data; name=\"nombre\"\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[valoracion datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; //precio [postbody appenddata:[[nsstring stringwithformat:@"--%@\r\n", stringboundary] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-disposition: form-data; name=\"nombre\"\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[precio datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; // setting body of post request [request sethttpbody:postbody]; nsdata *returndata = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil]; nsstring *returnstring = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding]; nslog(@"return method: %@",returnstring); }

the equivalent java client method response is:

seek { formdatamultipart form = new formdatamultipart().field("file", new file("c:\\1943.jpg"), mediatype.multipart_form_data_type) .field("nombre", "parameter_name") .field("valoracion", "4") .field("precio", "13,45 €") webresource webresource = client.create().resource("https://webservice"); webresource.header("authorization", "basic " + base64.encodebase64string(stringutils.getbytesutf8("user:password"))); webresource.type(mediatype.multipart_form_data) .accept(mediatype.text_plain) .post(form); }

anyone know error in objective-c code?

thanks in advance

at first glance see: don't send "content-length" of post data.

this working code send image parameters on server:

nsmutableurlrequest *mypost = [nsmutableurlrequest requestwithurl:myurl]; [mypost sethttpmethod:@"post"]; nsstring *stringboundary = @"0xkhtmlboundary"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",stringboundary]; [mypost addvalue:contenttype forhttpheaderfield: @"content-type"]; nsmutabledata *postbody = [nsmutabledata data]; [postbody appenddata:[[nsstring stringwithformat:@"--%@\r\n",stringboundary] datausingencoding:nsutf8stringencoding]]; (nsstring * key in postdict) { [postbody appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"\r\n\r\n",[key datausingencoding:nsutf8stringencoding]] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[postdict[key] datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",stringboundary] datausingencoding:nsutf8stringencoding]]; } if(imagedata!=nil){ [postbody appenddata:[@"content-disposition: form-data; name=\"avatar_file\"; filename=\"test.png\"\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:[@"content-type: application/octet-stream\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [postbody appenddata:imagedata]; [postbody appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n",stringboundary] datausingencoding:nsutf8stringencoding]]; } [mypost sethttpbody:postbody]; nsstring *tmp = [nsstring stringwithformat:@"%d",[postbody length]]; [mypost setvalue:tmp forhttpheaderfield:@"content-length"]; [[nsurlconnection alloc] initwithrequest:mypost delegate:self];

but recommend utilize library https://github.com/afnetworking/afnetworking - much easier , more convenient

xcode http-post multipartform-data nsmutableurlrequest

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -