ios 使用 HTTP POST 上传图像和文本

2024-06-19

谢谢阅读。

我是 iOS 新手,我正在尝试使用上传图像和文本multi-part form encoding在 iOS 中。

The curl等价的是这样的:curl -F "param1=value1" -F "[email protected] /cdn-cgi/l/email-protection" "http://some.ip.address:5000/upload http://some.ip.address:5000/upload"

The curl上面的命令返回预期的正确响应JSON.

Problem:我不断收到 HTTP 400 请求,这意味着我在编写 HTTP POST 正文时做错了什么。

我做了什么:作为一些参考,我尝试过Flickr API iOS 应用程序“POST 大小太大!” https://stackoverflow.com/questions/4683559/flickr-api-ios-app-post-size-too-large and Objective C:如何使用HTTP POST上传图像和文本? https://stackoverflow.com/questions/5422028/objective-c-how-to-upload-image-and-text-using-http-post。但是,我不断收到 HTTP 400。

我尝试过ASIHttpRequest但那里有一个不同的问题(回调从未被调用)。但是,我没有对此进行进一步调查,因为我听说开发人员已停止支持该库:http://allseeing-i.com/[request_release] http://allseeing-i.com/%5Brequest_release%5D;

有人可以帮我吗?


以下是我的应用程序中用于将图像发布到我们的网络服务器的代码:

// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:[NSString stringWithString:@"1.0"] forKey:[NSString stringWithString:@"ver"]];
[_params setObject:[NSString stringWithString:@"en"] forKey:[NSString stringWithString:@"lan"]];
[_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]];
[_params setObject:[NSString stringWithFormat:@"%@",title] forKey:[NSString stringWithString:@"title"]];

// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = [NSString stringWithString:@"----------V2ymHFg03ehbqgZCaKO6jy"];

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ 
NSString* FileParamConstant = [NSString stringWithString:@"file"];

// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@""]; 

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long) [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ios 使用 HTTP POST 上传图像和文本 的相关文章

随机推荐

  • 该变量未声明或从未分配警告

    这是基类 public class BaseClass UserControl protected ListView list protected TreeView tree public BaseClass 儿童班 public part
  • router.navigate 不起作用(Angular6,延迟加载)

    我是 Angular 4 的新手 目前使用 v 6 我一直在尝试使用this router navigate 登陆 从登录组件重定向到登陆组件的功能 它无法正常工作 它将显示登录页面一秒钟 然后再次重定向回登录页面 但是 例如 如果我尝试浏
  • 在 for 循环中修改列表元素

    我有一个清单a我想更改其元素a i j 根据一个函数f 我能比天真的方式做得更好吗 for index in range i j a index f a 我所说的更好是指更接近于map f a 或者更快的东西 您可以分配给切片 a i j
  • 用于验证 ip 列表中的 ip 范围的正则表达式

    我有正则表达式用于验证 50 个 ips 逗号分隔的列表 25 0 5 2 0 4 0 9 01 0 9 0 9 3 25 0 5 2 0 4 0 9 01 0 9 0 9 1 50 列表示例 10 10 10 1 127 0 0 1 现在
  • extern 关键字对 C 函数的影响

    在C中 我没有注意到任何影响extern在函数声明之前使用关键字 起初 我认为在定义时extern int f 在单个文件中forces您可以在文件范围之外实现它 然而我发现两者 extern int f int f return 0 an
  • 使用底格里斯河从纬度/经度获取人口普查区

    我有相对较多的坐标 我想获取其人口普查区 除了 FIPS 代码 我知道我可以使用以下命令查找各个纬度 经度对call geolocator latlon 已完成here https stackoverflow com questions 5
  • 查找 Ivy 中隐藏的依赖项

    我使用 Apache Ivy IvyDE 来获取项目的依赖项 它们是
  • UITableViewCell 的 viewDidAppear

    我通常使用viewDidAppear方法在视图完成出现后在视图上执行一些 UI 操作 我在各种情况下使用了此方法 它非常有用 但是 我需要在视图上进行一些 UI 更改UITableViewCell当它完成出现后 SDK中是否有任何可用的方法
  • CSS3 中均匀间隔的导航链接占据 ul 的整个宽度

    我想创建一个水平导航链接列表 其中导航链接均匀分布并占据封闭容器的整个宽度 ul 导航链接可以有不同的宽度 第一个和最后一个链接应与链接的开头和结尾对齐 ul 分别 意味着链接不居中 如下所示 left side right side li
  • UI图像位置

    我使用以下代码在 UIView 中放置一些图像 UIImage image UIGraphicsBeginImageContext CGSizeMake 480 320 int k 0 int posY 0 for int i 0 i lt
  • 您可以为 None 指定类型参数或告诉编译器它是一个 Option[String] 吗?

    我想知道我是否可以在我的代码中写这样的东西 None String 我很惊讶没有人提到它的存在Option empty scala gt Option empty String res0 Option String None 请注意 在许多
  • 无法找到请求的工厂 com.ctc.wstx.stax.WstxInputFactory

    我正在构建 Oracle Agile PLM CustomAction Px 我在Px内部调用了一个web服务来处理一些数据 我部署后 它给出了 类未找到异常 javax xml ws Service 所以我复制了jaxws api 2 1
  • Python中如何知道文件的编码? [复制]

    这个问题在这里已经有答案了 有谁知道如何在Python中获取文件的编码 我知道您可以使用编解码器模块打开具有特定编码的文件 但您必须提前知道它 import codecs f codecs open file txt r utf 8 有没有
  • 使用 shell 脚本将行附加到 /etc/hosts 文件

    我有一个新的 Ubuntu 12 04 VPS 我正在尝试编写一个安装脚本来完成整个 LAMP 安装 我遇到问题的地方是在 etc hosts文件 我当前的主机文件如下所示 127 0 0 1 localhost Venus The fol
  • python 排列有问题

    我在排列方面遇到一些问题 当谈到Python时 我真的是一个大菜鸟 所以任何帮助将不胜感激 假设我在文本文件中有一个范围为 1 6 的列表 例如 它看起来像 1 2 3 4 5 6 我想打开所述 txt 文件并计算这 6 个数字中 N 的所
  • Angular 中有主控制器好吗?

    我不知道这是否是一个好的做法 我在路由配置中定义了一个控制器 但是因为我的HomeCtrl is in ng if他听不到的声明loginSuccess所以我做了MainCtrl它监听loginSuccess并做出适当的反应 这段代码工作得
  • 如何将 DataFrame 作为输入传递给 Spark UDF?

    我有一个数据框 我想对每一行应用一个函数 该函数依赖于其他数据帧 简化的例子 我有如下三个数据框 df sc parallelize a b 1 c d 3 toDF feat1 feat2 value df other 1 sc para
  • 蓝图初始化,我可以在第一次请求蓝图之前运行函数吗

    是否可以在第一次请求特定对象之前运行一个函数blueprint my blueprint before first request def init my blueprint print yes 目前这会产生以下错误 AttributeEr
  • Spark中如何获取map任务的ID?

    Spark中有没有办法获取map任务的ID 例如 如果每个映射任务都调用用户定义的函数 我可以从该用户定义的函数中获取该映射任务的 ID 吗 我不确定您所说的地图任务 ID 是什么意思 但您可以使用以下方式访问任务信息TaskContext
  • ios 使用 HTTP POST 上传图像和文本

    谢谢阅读 我是 iOS 新手 我正在尝试使用上传图像和文本multi part form encoding在 iOS 中 The curl等价的是这样的 curl F param1 value1 F email protected cdn