-(void)uploadJob:(Job *)job
{
NSLog(@"Server: uploadJob: called");
NSString *preparedJob=[Database prepareJobForUploading:job];
if ([preparedJob isEqualToString:@""])
{
return;
};
NSLog(@"preparedJob: %@", preparedJob);
NSString *post = [NSString stringWithFormat:@"accessToken=%@&job=%@", self.key, preparedJob];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@upload.aspx", serverAPIPath]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"responseData: %@", [urlData description]);
NSString *responseString=[NSString stringWithFormat:@"response: %d. description: %@",[(NSHTTPURLResponse *)response statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse *)response statusCode]]];
NSString *resultString=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"response: %@", responseString);
NSLog(@"result: %@", resultString);
}
此代码编译但不工作。我从服务器返回空数据,但它不应该是空的。问题似乎是:我的postData
变量以 UTF8 编码而不是以 ASCII 发送(例如,将符号更改为 %20 等等)。我的代码有什么问题?我能做些什么来解决这个问题?
顺便说一句,preparedJob
字符串是字符串,其中包含生成的 JSON