0

我有以下用 Objective-C 编写的后置脚本。代码是根据 SO 上的问题修改而来的(问题在此处此处)。照片上传到服务器,但参数未正确添加。继承人的代码:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// Dictionary that holds post parameters. 
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:Name forKey:@"username"];

// the boundary string
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

// string constant for the post parameter
NSString* FileParamConstant = @"FILE1";

// the server url to which the image 
NSURL* requestURL = [NSURL URLWithString:uploadURLString];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; charset:UTF-8; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
UIImage *newImage = self.urlImageOfUser.image;
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

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

// setting the body of the post to the request
[request setHTTPBody:body];

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

// set URL
[request setURL:requestURL];

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

NSLog(@"%@", returnString);

这个最终NSLog显示添加的参数(用户名)是完全空的。有什么我想念的吗?

4

2 回答 2

0

我的建议:

NSString *stringBody = @"";
NSArray *allKeys = parameters.allKeys;
NSArray *allValues = parameters.allValues;

for (int i = 0; i < allKeys.count; i++) {
    NSString *key = allKeys[i];
    NSString *value = allValues[i];
    stringBody = [stringBody stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", key, value]];
}

完整代码:

FOUNDATION_EXPORT NSString *GetWebServiceResponseHTTP_POST(NSString *url, NSString *webServiceName, NSMutableDictionary *parameters) {
    NSString *stringBody = @"";
    NSArray *allKeys = parameters.allKeys;
    NSArray *allValues = parameters.allValues;

    for (int i = 0; i < allKeys.count; i++) {
        NSString *key = allKeys[i];
        NSString *value = allValues[i];
        stringBody = [stringBody stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", key, value]];
    }

    NSURL *completeURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, webServiceName]];
    NSData *body = [stringBody dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:completeURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"some value" forHTTPHeaderField:@"some header"];
    [request setHTTPBody:body];
    [request setValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *responseCode = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

    if (responseCode.statusCode != 200) {
        NSLog(@"Error getting %@, HTTP status code %i", url, responseCode.statusCode);
        return nil;
    }

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];    
    return responseString;
}
于 2013-08-30T14:57:05.773 回答
0

我让它工作,这是我使用的新代码:

for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

我认为唯一的区别是charset参数。

于 2013-08-26T15:23:28.313 回答