HTTP Post Script不添加参数

时间:2013-08-25 05:06:48

标签: php ios objective-c parameters http-post

我有以下用Objective-C编写的后期脚本。代码是根据此处的问题修改的(问题为herehere)。照片上传到服务器,但参数未正确添加。下面是代码:

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表示添加的参数(用户名)完全为空。有什么我想念的吗?

2 个答案:

答案 0 :(得分: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参数。

答案 1 :(得分: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;
}