2
我正在編寫一個HTTP Post請求,但由於某些原因,參數沒有被正確添加,而且我不能爲我的生活弄清楚我做錯了什麼。這是我有:Objective-C中的HTTP發佈請求無法正常工作
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腳本能夠上傳照片,並且我將其大部分複製並粘貼到這張照片上,但不知怎的,這張照片無法正常工作。希望這只是我錯過的一個簡單的錯誤。
發生'meeting_Post'什麼?在聲明後的任何地方我都看不到它。並且是參數'「param1 = value&param2 = value2 ...」的格式? –
@MisterMister編輯了ans – iAppDeveloper
啊,謝謝編輯。它完美的工作,我沒有意識到它可以這麼簡單! –