iOS manual Post

Source: Internet
Author: User

Nsurl *url = [Nsurl urlwithstring:@ "http://example.com/form/"];
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
[Request sethttpmethod:@ "POST"];

NSString *boundary = @ "your_boundary_string";
NSString *contenttype = [NSString stringwithformat:@ "multipart/form-data; boundary=%@", boundary];
[Request Addvalue:contenttype forhttpheaderfield:@ "Content-type"];

Nsmutabledata *body = [Nsmutabledata data];

[Body appenddata:[[nsstring stringwithformat:@ "\r\n--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]];
[Body appenddata:[[nsstring stringwithformat:@ "content-disposition:form-data; name=\" photo\ "; filename=\"%@.jpg\ "\ r\n ", Self.message.photoKey] datausingencoding:nsutf8stringencoding];
[Body appenddata:[@ "content-type:application/octet-stream\r\n\r\n" datausingencoding:nsutf8stringencoding]];
[Body Appenddata:[nsdata Datawithdata:imagedata];

[Body appenddata:[[nsstring stringwithformat:@ "\r\n--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]];
[Body appenddata:[[nsstring stringwithformat:@ "content-disposition:form-data; name=\" message\ "\r\n\r\n%@", Self.message.message] datausingencoding:nsutf8stringencoding];

[Body appenddata:[[nsstring stringwithformat:@ "\r\n--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]];
[Body appenddata:[[nsstring stringwithformat:@ "content-disposition:form-data; name=\" User\ "\r\n\r\n%d", 1] Datausingencoding:nsutf8stringencoding]];

[Body appenddata:[[nsstring stringwithformat:@ "\r\n--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]];

[Request Sethttpbody:body];
[Request Sethttpbody:body];

Nsurlresponse *response;
Nserror *error;

nsdata* data=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.