iPhone应用程序中的表单帖子不起作用

时间:2012-01-05 14:59:29

标签: iphone objective-c ios cocoa-touch http-post

我想发布价值观&在iPhone应用程序中通过HTTP文件。

要求是在发布表单时,必须使用输入框中的值进行身份验证,如果用户有效,则应该上载该文件。

但是当我尝试发布表单时,文件没有发布,操作URL也不接受发布的值。

在下面找到iPhone应用中使用的代码及其HTML等价物。有人能说出我错过的东西吗?

HTML等效

<form action="http://www.something.com/upload/" method="post" enctype="multipart/form-data" target="_blank" onsubmit="return window.confirm(&quot;You are submitting information to an external page.\nAre you sure?&quot;);">    
<h3>Test Form</h3>    
<p>File: <input name="file" type="file"></p>    
<p>Username: <input name="usr" type="text"></p>    
<p>Password: <input name="pwd" type="text"></p>    
<p><input name="send" value="Upload" type="submit"></p>    
</form>

iPhone应用程序中使用的代码

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test-upload" ofType:@"zip"];
    NSData *postData = [NSData dataWithContentsOfFile:filePath];

    NSString *urlString = @"http://www.something.com/upload/";

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

    NSString *boundary = [NSString stringWithString:@"---------------------------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=\"usr\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"usr@domain.com" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Disposition: form-data; name=\"pwd\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"testpwd" dataUsingEncoding:NSUTF8StringEncoding]];

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

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"test-upload.zip\"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:postData]];

    [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];
    NSLog(returnString);

2 个答案:

答案 0 :(得分:0)

看起来你对这一行感到有疑问:

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"test-upload.zip\"rn"] dataUsingEncoding:NSUTF8StringEncoding]];

不应该是:

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"test-upload.zip\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:0)

最后通过纠正边界来解决。我已经发布了以下整个代码

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test-upload" ofType:@"zip"];
    NSData *postData = [NSData dataWithContentsOfFile:filePath];

    NSString *urlString = @"http://www.something.com/upload/";

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

    NSString *boundary = [NSString stringWithString:@"---------------------------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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"usr\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"user@domain.com" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

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


    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"test-upload.zip\"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:postData]];

    [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];
    NSLog(returnString);