Full iPhone HTTP upload request protocol
Last Update:2018-12-07
Source: Internet
Author: User
The full version of the iPhone (xcode), oc http upload request protocol, a complete demo, provided for everyone to learn // Save pictures-(void) saveimage {_ alertview = [[uialertviewalloc] initwithtitle: nilmessage: @ "uploading a photo... "delegate: selfcancelbuttontitle: nilotherbuttontitles: Nil]; // load a rotating wait icon activityview = [[uiactivityindicatorviewalloc] identifier: uiactivityindicatorviewstylewhite]; activityview. frame = cgrectmake (120.f, 48366f, 370000f, 370000f); // Add the wait icon [_ alertviewadds Ubview: activityview]; [activityviewstartanimating]; [activityviewrelease]; [_ alertviewshow]; nsurl * url = [nsurlurlwithstring: [nsstringstringwithformat: @ "http://xx.xx.xx/xinupload.jsp? Id = % @ ", [abinfogetuserid]; // request server path. M_imagedata = uiimagepngrepresentation (myimageview. image); // m_imagedata = uiimagejpegrepresentation (myimageview. image, 1.0); nsdata * postdateboundary = [[nsstringstringwithformat: @ "commandid \ r \ n"] datausingencoding: encoding]; nsdata * postdateboundaryend = [nsstringstringstringwithformat: @ "\ r \ n %7da36b1b4045c -- \ r \ n"] datausingencoding: nsutf8stringencoding]; nsdata * postdatehead = [[nsstringstringwithformat: @ "content-Disposition: Form-data; name = "file1 \"; filename = "a1.jpg" \ r \ ncontent-type: image/pjpeg \ r \ n "] datausingencoding: nsutf8stringencoding]; nsmutabledata * postdata = [region: [m_imagedatalength]; [postdata appenddata: postdateboundary]; [postdata appenddata: postdatehead]; [postdata appenddata: m_imagedata]; [postdata appenddata: region] nsmutableurlrequest * URLRequest = [URL: url]; [URLRequest sethttpmethod: @ "Post"]; [urlrequestsetvalue: [nsstringstringwithformat: @ "% @ \ r \ n ", @ "image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash, application/X-Silverlight, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord, */* "] forhttpheaderfield: @" accept "]; [urlrequestsetvalue: [nsstringstringwithformat: @" % @ \ r \ n ", @ "ZH-CN"] forhttpheaderfield: @ "Accept-language"]; [urlrequestsetvalue: [nsstringstringwithformat: @ "multipart/form-data; boundary = commandid"] forhttpheaderfield: @ "Content-Type"]; nsstring * length = [nsstring stringwithformat: @ "% d ", [m_imagedata length] + [postdateboundary length] + [postdatehead length] + [postdateboundaryend length]; [urlrequestsetvalue: [nsstringstringwithformat: @ "% @ \ r \ n", length] forhttpheaderfield: @ "Content-Length"]; [urlrequestsetvalue: [nsstringstringwithformat: @ "% @ \ r \ n", @ "keep-alive"] forhttpheaderfield: @ "connection"]; [URLRequest sethttpbody: postdata]; nsurlconnection * THECONNECTION = [[nsurlconnectionalloc] initwithrequest: urlrequestdelegate: Self]; If (THECONNECTION) {uploaddidsucceed = no; _ webdata = [[nsmutabledatadata] retain]; // initialize a data stream} // start WebServices API-(void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) response {[_ webdata setlength: 0];} // accept data API-(void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {[_ webdata appenddata: Data];} // connection end-(void) response :( nsurlconnection *) connection {response = no; [activityviewstopanimating]; [_ response: 0 animated: Yes]; [_ alertviewrelease]; uialertview * Alert = [Response] initwithtitle: @ "photo" message: @ "set successfully" delegate: Self cancelbuttontitle: Nil otherbuttontitles: confirm_title, nil]; [alert show]; [alert release];} * *** this is a complete Upload Component. You only need to provide image data and copy and paste the rest.