-(void)startPostRequest:(NSString *)url body:(NSString *)body flag:(BOOL)flag { if (httpConnection) { [httpConnection release]; } //如果url中包含中文,需要对它进行编码,在外部和这个方法内部编都可以 NSURL *newURL=[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; //创建请求对象 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:newURL]; //设置请求方式为post [request setHTTPMethod:@"POST"]; if (flag) { [request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"]; } else{ [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; } //设置请求体 NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; [request setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"]; httpConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; }