本文介绍了Objective-C中的HTTP Post请求不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在写一个HTTP Post请求,但由于某些原因,参数没有正确添加,我不能为我的生活找出我做错了。这里是我有:
I am writing an HTTP Post request, but for some reason the parameters are not being added correctly, and I can't for the life of me figure out what I'm doing wrong. Here's what I have:
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脚本工作,将上传一张照片,我复制和粘贴大部分到这一个,但不知何故这个不工作。希望这只是我错过的一个简单错误。
The request is getting through, however all the parameters are not being added properly. I had a Post script working that would upload a photo, and I copied and pasted most of it over to this one, but somehow this one is not working. Hopefully it's just a simple error I'm missing.
推荐答案
尝试此
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中的HTTP Post请求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!