gpt4 book ai didi

iphone - 如何使用 Objective-C iPhone SDK 发送文件以及一些 POST 变量?

转载 作者:行者123 更新时间:2023-12-03 20:37:02 25 4
gpt4 key购买 nike

我想知道{insert_title_here}吗?

我使用了这个方法,但没有成功:

    //string data
NSString *post = @"message=helloWorld";
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

//file data
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:@"ImageFile.png"];
NSData *imageData = [[NSData alloc] initWithContentsOfFile:fullPathToFile];

//request
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:@"http://www.example.com/"];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//POST body
NSMutableData *postbody = [NSMutableData data];

//append string data
[postbody appendData:postData];

//append file
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"iconFile\"; filename=\"ImageFile.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:postbody];

//set content length
NSString *postLength = [NSString stringWithFormat:@"%d", [postbody length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

//send and receive
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

有什么建议吗?

最佳答案

试试这个..非常容易理解和实现...

- (void)simpleJsonParsingPostMetod
{

#warning set webservice url and parse POST method in JSON
//-- Temp Initialized variables
NSString *first_name;
NSString *image_name;
NSData *imageData;

//-- Convert string into URL
NSString *urlString = [NSString stringWithFormat:@"demo.com/your_server_db_name/service/link"];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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

//-- Append data into posr url using following method
NSMutableData *body = [NSMutableData data];


//-- For Sending text

//-- "firstname" is keyword form service
//-- "first_name" is the text which we have to send
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"firstname"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",first_name] dataUsingEncoding:NSUTF8StringEncoding]];


//-- For sending image into service if needed (send image as imagedata)

//-- "image_name" is file name of the image (we can set custom name)
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"%@\"\r\n",image_name] 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]];


//-- Sending data into server through URL
[request setHTTPBody:body];

//-- Getting response form server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//-- JSON Parsing with response data
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);
}

您可以直接在此处下载示例代码https://github.com/Tech-Dev-Mobile/Json-Sample

关于iphone - 如何使用 Objective-C iPhone SDK 发送文件以及一些 POST 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2229002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com