使用表格格式发布数据

时间:2015-01-13 10:18:55

标签: ios objective-c iphone

我正在尝试使用帖子请求发送表单数据。这是我的数据

assignment_id = 119; 
content = "hello text 1234"; 
"file_type" = text;
"instruction_type" = text;
name = text111;
status = yes;

并且所有数据都在NSDictionary中 这是我试图传递所有数据的代码

    -(void)callAPIWebserviceWithFormData: (NSDictionary *)dictionaryParameter stringURL:(NSString *)stringURL
{

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:stringURL]];
[request setHTTPMethod:@"POST"];


NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"assignment_id\"; \n\n %@ \r\n", [dictionaryParameter objectForKey:@"assignment_id"]] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"content\"; \n\n %@ \r\n", [dictionaryParameter objectForKey:@"content"]] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_type\"; \n\n  \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"instruction_type\"; \n\n %@ \r\n", [dictionaryParameter objectForKey:@"instruction_type"]] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"name\"; \n\n %@ \r\n", [dictionaryParameter objectForKey:@"name"]] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"; \n\n %@ \r\n", [dictionaryParameter objectForKey:@"status"]] dataUsingEncoding:NSUTF8StringEncoding]];

[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"Return String= %@",returnString);
}

它给我一些HTML错误。 请帮忙

0 个答案:

没有答案