• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 如何在 NSMutableUrlRequest 中发布文本和图像数据?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:05:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我应该发布在验证凭据和图像数据期间生成的 session ID。但是图像和 session ID 没有上传。 当我使用 ASIHTTPRequest 时它工作正常,后来当我尝试使用 NSMutableUrlRequest 和 NSURLConnection 时它似乎不起作用。

下面给出的是用于 POST 图像和 session ID 的代码

- (void)postinDataToServerNSData *)inData
{
//inData is the image data.
[inData retain];  
NSString *urlString = @"http://xyz.com/abcd/kgh.php";
NSURL* url = [NSURL URLWithString:urlString];

NSMutableData *photoData = [[NSMutableData alloc]init];

//SessionID is stored in NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString * boundary = @"photoBoundaryParm";
NSString * boundaryString = [NSString stringWithFormat"--%@\r\n", boundary];
NSString * boundaryStringFinal = [NSString stringWithFormat"--%@--\r\n", boundary];

[photoData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[photoData appendData:[[NSString stringWithFormat"Content-Disposition: form-data; name=\"SessionID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[photoData appendData:[[NSString stringWithFormat"SessionID=%@",[defaults objectForKey"SessionID"]] dataUsingEncoding:NSUTF8StringEncoding]];
[photoData appendData:[[NSString stringWithFormat"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[photoData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];
[photoData appendData:[[NSString stringWithFormat"Content-Disposition: form-data; name=\"photo\";\r\nfilename=\"myphoto.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[photoData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[photoData appendData:inData];
[photoData appendData:[[NSString stringWithFormat"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[photoData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod"OST"];
[request setHTTPBody:photoData];

NSString* requestDataLengthString = [[NSString alloc] initWithFormat"%d", [photoData length]];

[request addValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self
                        startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                      forMode:NSDefaultRunLoopMode];
[connection start];

[inData release];
inData = nil;
}

请帮忙。

我坚持了很长时间。

提前致谢。



Best Answer-推荐答案


试试这段代码,然后在 post nsstring 中传递你的值

NSString* url=[NSString stringWithFormat:url];

NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURLURLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
                                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                    timeoutInterval:60.0];
//do post request for parameter passing
[theRequest setHTTPMethod:@"OST"];

NSString *post = [NSString stringWithFormat:@"&imageData=%@",postData];



NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];


[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];

[theRequest  setURL:[NSURL URLWithString:url]];

[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

NSURLConnection  *serverConnectionObj = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

关于iphone - 如何在 NSMutableUrlRequest 中发布文本和图像数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19117693/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap