php - 通过 Objective-C HTTP Post 上传照片

标签 php objective-c http ios5

我正在发送一张构建 HTTP POST 的图片,如下所示:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];


NSString *boundary = @"801f6dd4cd17";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=------------------------------%@", boundary];


NSMutableDictionary *headerFieldsDict = [[ NSMutableDictionary alloc] init];
[ headerFieldsDict setObject:contentType forKey:@"Content-Type"];

[request setAllHTTPHeaderFields:headerFieldsDict];

NSMutableData *body = [NSMutableData data];

//---------- FIRST IMG
UIImage* imgLittle = [realImgButton imageForState:UIControlStateNormal];//thumImg.image;
NSData *imageData = UIImageJPEGRepresentation(imgLittle, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"------------------------------%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"img.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

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

[request setHTTPBody:body];

但是服务器拒绝上传图片(它给出了一个软件错误,似乎 HTTP post 没有被很好地解析),我所掌握的关于服务器的唯一信息是这个 php 客户端代码运行良好:

$data['file'] = "@".$file;
$header[] = "Expect: ";
print_r($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );

//curl_setopt($ch, CURLOPT_URL, 'http://xxxxxx:yyy/');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
$url = strip_tags($xml->dict->string[3]->asXML());
return $url;

有什么想法吗?或者我在 obj-c 代码上哪里错了?我尝试分析生成的 HTTP post 语句,它似乎与 php 代码生成的语句相同。

最佳答案

您可能想使用 -addValue:forHTTPHeaderField: 而不是这样做:

NSMutableDictionary *headerFieldsDict = [[ NSMutableDictionary alloc] init];
[ headerFieldsDict setObject:contentType forKey:@"Content-Type"];

[request setAllHTTPHeaderFields:headerFieldsDict];

您真正需要做的就是向请求添加一个 header ,但实际上您使用上面的代码清除了所有现有 header 。

关于php - 通过 Objective-C HTTP Post 上传照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12829248/

相关文章:

javascript - 我需要一个系统来发布 JavaScript 游戏的高分,但它必须很难被愚弄

http - 通过 http 获取邮件

php - Python 相当于 PHP 的 strip_tags?

php - 来自文件类型、文件大小和文件时间的警告

php - 对保持原始时间不变 MySQL 的行进行更新查询

ios - 在文本字段中显示 XML 元素的数量

objective-c - 核心数据&TableView&PDFView : How does it all come together?

php - mysql 查询不显示结果

iOS presentViewController 不调用 viewDidLoad

php - 什么时候发送 HTTP 状态码?