objective-c - Objective-C 中的 HTTP Post 请求不起作用

标签 objective-c cocoa-touch post parameters http-post

我正在编写一个 HTTP Post 请求,但由于某种原因,参数没有正确添加,而且我一生都无法弄清楚我做错了什么。这是我所拥有的:

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:@"text; 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:subject forKey:@"subject"];
[_params setObject:message forKey:@"message"];
[_params setObject:[[UIDevice currentDevice] systemName] forKey:@"device"];

// add params 
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]];
}

// the server url
NSURL* requestURL = [NSURL URLWithString:CIVCManifest.contactFeed];

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

// setting the body of the post to the reqeust
[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];

请求正在通过,但是所有参数都没有正确添加。我有一个可以上传照片的 Post 脚本,我将其中的大部分内容复制并粘贴到了这个脚本上,但不知何故,这个脚本不起作用。希望这只是我遗漏的一个简单错误。

最佳答案

试试这个

 NSString *Post = [[NSString alloc] initWithFormat:@"Post Parameters"];
    NSURL *Url = [NSURL URLWithString:@"Url"];

    NSData *PostData = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [PostData length]];

    NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] init];
    [Request setURL:Url];
    [Request setHTTPMethod:@"POST"];
    [Request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [Request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [Request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [Request setHTTPBody:PostData];

关于objective-c - Objective-C 中的 HTTP Post 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480689/

相关文章:

iphone - 为什么我的 UILabel 没有居中?

python - 如何在 Django 中创建预定帖子?

post - 如何在 gitlab 中触发特定作业

php - Twilio 调用转移

objective-c - iOS5 中的 Youtube - 点击完成按钮

ios - 如何检测 iPhone 5c 的颜色?

ios - iPhone UITextField - 更改占位符字体和文本颜色

ios - UIDatePicker 最小和最大日期不变

ios - 使用 NSJSONSerialization 将 NSString 转换为 JSON 不起作用

objective-c - UIPopoverController 取得控制权,不会放手