从iPhone到PhP的POST无法正常工作 [英] POST from iPhone to PhP not working

查看:120
本文介绍了从iPhone到PhP的POST无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次使用POST

POST信息没有从iPhone传递到PhP脚本.我在任何地方都没有错误. var_dump($ _ POST)显示为空.登录失败返回.接下来要看的任何想法,或者我在这里有明显的问题:

POST information is not getting from the iPhone to the PhP script. I get no errors anywhere. var_dump($_POST) shows empty. failed login returned. Any ideas where to look next or do I have a visible problem here:

     NSURL *url = [NSURL URLWithString:link];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSString *requestBodyString = [NSString stringWithFormat:@"txtuid=%@&txtpwd=%@", userName , password];
NSData *requestBody = [NSData dataWithBytes:[requestBodyString UTF8String]length:[requestBodyString length]];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestBody];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

在尝试了六种不同的方法之后,我从上一篇文章中获取了修改版本.

After trying it six different ways, I took a modified version from a previous post.

又一次尝试,结果相同.

And yet another attempt with the same results.

NSData *data = [[NSString stringWithFormat: @"txtuid=%@&txtpwd=%@", userName , password] dataUsingEncoding:NSUTF8StringEncoding];

// set up request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:link]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"--------------------------------------------------"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

// body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

[request setHTTPBody:body];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

推荐答案

请参见

See my answer here about how to use POST to upload an image. It posts to forms in a PHP file. Same concept for posting data and not uploading images, but this is a working example of how to post to a PHP script using Objective-C.

这篇关于从iPhone到PhP的POST无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆