iPhone发布到PHP失败

iPhone发布到PHP失败,php,ios,iphone,cocoa-touch,http-post,Php,Ios,Iphone,Cocoa Touch,Http Post,我这里有这个代码: NSString *post = [NSString stringWithFormat:@"deviceIdentifier=%@&deviceToken=%@",deviceIdentifier,deviceToken]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; NSString *postLength = [NSStrin

我这里有这个代码:

NSString *post = [NSString stringWithFormat:@"deviceIdentifier=%@&deviceToken=%@",deviceIdentifier,deviceToken]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"http://website.com/RegisterScript.php"]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"MyApp-V1.0" forHTTPHeaderField:@"User-Agent"];
[request setHTTPBody:postData]; 
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *response = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding]; 
它应该将数据发送到我的PHP服务器,以便将设备注册到我们的数据库中,但是对于我的一些用户,没有发送POST数据。有人告诉我这句话:

NSData*postData=[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]


可能是导致问题的原因。有没有想过为什么这个脚本有时不会发送POST数据?在服务器上,我得到了发送失败的数据包跟踪,内容长度为0,因此根本没有发送数据

我在做一个类似的函数,唯一不同的是你认为是错误的那条线。试着做

NSMutableData *postData = [NSMutableData data];
[postData appendData:[[NSString stringWithFormat::@"deviceIdentifier=%@&deviceToken=%@",deviceIdentifier,deviceToken]dataUsingEncoding:NSUTF8StringEncoding]];
stringByUrlEncoding基本上用其字符编码的安全字符串替换任何保留字符(我认为这是最好的解释方法,我的想法是空的)

希望这有助于测试您的数据:
NSLog([[NSString alloc]initWithData:postData编码:NSAsciiStringEncoding])

另外,您可以从属性获取应用程序版本:

NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]; [request setValue:[NSString stringWithFormat:@"My App %@", appVersion] forHTTPHeaderField:@"User-Agent"];

查看您在哪里使用stringByURLEncoding?我确实用你上面的代码替换了我的代码,看起来好像它在工作!:我把它用在变量上,对不起,我以为我把它放进去了。所以您的将是[deviceIdentifier stringByUrlEncoding],[deviceToken stringByUrlEncoding]。解释为什么要使用它以及代码