0

我正在尝试使用发布操作将一些数据上传到 Web 服务但是在 didReceiveData 上发生以下错误。

400 Bad Request
a 404 Not Found

你能帮我解决这个编码问题吗?

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
NSMutableData *postData = [[NSMutableData alloc] init];
NSString *boundary = [NSString stringWithString:@"------------------147378098Aadfsn1987XdaoTwq"];

// First name
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"firstname\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]];

NSData  *photo = UIImageJPEGRepresentation(takePhoto, 0.5f);
// Photo data
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"picture1\"; filename=\"photo.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
if (takePhoto != nil) {
    [postData appendData:photo];
}

NSString *postLength = [NSString stringWithFormat:@"%d", postData.length];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
4

3 回答 3

1

这些是服务器错误而不是 iPhone 错误。您发送错误的 URL 或您的服务未正确读取您的 url。

于 2012-06-02T00:34:12.000 回答
1

试试这个从 iPhone 上传照片到服务器的代码。

- (IBAction)uploadImage {
/*
 turning the image into a NSData object
 getting the image back out of the UIImageView
 setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = @"Your URL string where you want to POST Photo";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type

 You might want to generate a random boundary.. this is just the same
 as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);
}
于 2012-06-02T05:14:41.237 回答
0

您的网址可能未编码。试试这样的..

NSString* url =
[@"http://your-url-.com" stringByAddingPercentEscapesUsingEncoding:
                    NSASCIIStringEncoding];
于 2012-06-02T01:21:03.210 回答