【问题标题】:HTTP Post Request in Objective-C Not WorkingObjective-C 中的 HTTP 发布请求不起作用
【发布时间】:2013-08-28 06:21:01
【问题描述】:

我正在编写一个 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 脚本,我将其中的大部分内容复制并粘贴到这张照片上,但不知何故,这张照片不起作用。希望这只是我遗漏的一个简单错误。

【问题讨论】:

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


    【解决方案1】:

    试试这个

     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];
    

    【讨论】:

    • meeting_Post 会发生什么?我没有看到它在声明后的任何地方使用。以及参数的格式是“param1=value&param2=value2...”吗?
    • 啊,感谢您的编辑。效果很好,没想到这么简单!
    • NSString *Post =[[NSString alloc] initWithFormat:@"param1=%@&param2=%@",value1,value2];
    • @iAppDeveloper 我有一个图像存储在 UIImage 中。你知道如何将它附加到 POST 请求中吗??
    猜你喜欢
    • 2018-08-17
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多