HTTP “POST” request in iOS

佐手、 提交于 2019-12-20 06:03:29

问题


I need to post to this url: https://api.platform.com/media. I am really new to HTTP request and I need to send a request that contains an image and 3 other parameters. I have the values that I need but I don't know where to begin or how it works.... Headers, Content-Length and some other wired values.

These are the values:

media[user_profile_id]
media[channels_list][]
media[file]

回答1:


Create an instance of NSURLRequest. Set the method property to POST, and set the body data.

The data needs to be a single instance of NSData, so you will have to keep appending whatever data you want to send, each field labeled and separated by ampersands like so:

id=12345&channels=1,2,3&image=123abcdef

Remember, you need to transform all strings to binary, using NSString's dataUsingEncoding: method. If you are using UIImage for the image, it has a similar method.

The server of course needs to know how to parse the data, strings being strings and the image being an image.

Finally, create an instance of NSURLConnection, set your current object as delegate and implement the delegate protocol to receive the response.




回答2:


Lookup NSMutableURLRequest. You can use that with NSURLConnection.

This post should give you a better idea of how to do it.



来源:https://stackoverflow.com/questions/9348746/http-post-request-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!