在iOS中使用NSURLConnection的表单数据请求

时间:2012-02-27 06:19:08

标签: ios objective-c nsurlconnection

我想在iOS中使用NSURLConnection发布http表单。我在HTML表单中有两个表单字段和一个文件上载选项。当我使用NSURLConnection做同样的事情时,我没有得到回复。

NSString *urlString = @"http://url/test.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"myphoto.png\"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:filedata];
[body appendData:[[NSString stringWithFormat:@"&s=YL4e6ouKirNDgCk0xV2HKixt&hw=141246514ytdjadh"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"RETURNED:%@",returnString);

但是当我使用ASIHTTPRequest并编写以下代码时,它正在运行,我正在收到回复。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://url/test.php"]];
[request setPostValue:@"YL4e6ouKirNDgCk0xV2HKixt&hw" forKey:@"ssf"];
[request setPostValue:@"141246514ytdjadh" forKey:@"sds"];
[request setData:filedata withFileName:@"myphoto.png" andContentType:@"image/jpeg"  forKey:@"file"];
[request startSynchronous];

NSError *error = [request error];
if (!error) {
    NSString *response = [request responseString];
    NSLog(@"response:%@",response);
}

有谁可以告诉我NSURLConnection部分我做错了什么?

5 个答案:

答案 0 :(得分:13)

您没有复制该链接的示例。在该教程中,HTTPBody参数应该是NSData的实例,而不是NSString。

[request setHTTPMethod:@"POST"]; 
NSString *myString = [NSString stringWithFormat:@"value1=test3&value2=test"];
[request setHTTPBody:[myString dataUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:8)

我尝试使用此代码上传图片及其工作原理。添加了边界。

NSString *urlString = @"URL";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"Test.png\"\r\n" 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]];
[request setHTTPBody:body];

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

答案 2 :(得分:2)

这对我来说很好。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableURLRequest *req=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http:///URL/iinsert.php"]];

    NSString *myreqstr=@"name=abhii&address=knrr";
    NSData *myreqdata=[NSData dataWithBytes:[myreqstr UTF8String] length:[myreqstr length]];
    [req setHTTPMethod:@"POST"];

    [ req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [ req setHTTPBody: myreqdata ];

    //[req setValue:@"abhii" forHTTPHeaderField:@"name"];
    //[req setValue:@"kar" forHTTPHeaderField:@"address"];
    NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
    NSLog(@"%@",data);
    NSString *returnstring=[[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"%@",returnstring);




    // Do any additional setup after loading the view, typically from a nib.
}

答案 3 :(得分:1)

试试这个......

NSURL *url = [NSURL URLWithString:@"URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];


NSString *myRequestString =@"Request string";
NSLog(@"%@",myRequestString);
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];

[ request setHTTPBody: myRequestData ];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];

答案 4 :(得分:0)

你有几个

rn

在你的字符串的末尾。所有这些都应该是

\r\n

更准确地说应该是:

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"myphoto.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];