发送参数为PHP,并等待响应

我正在为移动应用程序构build一个login系统,并且需要使用POST / GET方法将用户名和密码发送到PHP项目。

那么我已经阅读了互联网上的一些教程,看到大多数教导如何做到这一点,但我需要发送post,并接收通过PHP生成的值,即:

  • 我们发送一个PHP的login名和密码
  • 如果login错误,PHP会显示错误的login名和密码。
  • 如果你是对的,它会在屏幕上显示另一条消息

而且,这就是我想要做的,超越发送参数,我想从PHP文件接收响应,这是可能的Ios?

下面的代码是用POST方法描述的简单例子( 如何通过POST方法传递数据

您可以使用以下代码片段,如本文所述 :

在这里,我简单介绍一下如何使用POST方法。

1.用实际的用户名和密码设置后缀string。

 NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"]; 

2.使用NSASCIIStringEncoding编码NSASCIIStringEncodingstring,以及需要以NSData格式发送的NSASCIIStringEncodingstring。

 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

你需要发送你的数据的实际长度。 计算后期string的长度。

 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 

3.创build一个Urlrequest,使用所有属性,如HTTP方法,带有后缀string长度的http头字段。 创buildURLRequest对象并初始化它。

 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 

设置您要将数据发送到该请求的Url。

 [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]]; 

现在,设置HTTP方法( POST或GET )。 把它写在你的代码中。

 [request setHTTPMethod:@"POST"]; 

根据发布数据的长度设置HTTP标题字段。

 [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

同时设置HTTP头字段的编码值。

 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; 

使用HTTPBody设置HTTPBody的HTTPBody。

 [request setHTTPBody:postData]; 

4.现在,创buildURLConnection对象。 使用URLRequest初始化它。

 NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

它返回初始化的url连接,并开始加载url请求的数据。 你可以使用下面的if / else语句来检查你的URL连接是否正确完成。

 if(conn) { NSLog(@”Connection Successful”) } else { NSLog(@”Connection could not be made”); } 

5.要从HTTP请求接收数据,可以使用URLConnection Class Reference提供的委托方法。 代表方法如下。

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data 

以上方法用于接收我们使用post方法得到的数据。

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

这种方法,你可以使用接收到错误报告,如果没有连接到服务器。

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 

上述方法用于连接成功后处理数据。

另请参阅文档POST 文档

这里是HTTPPost方法源代码的最好例子。

 //Create the request NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"your script name.php"]]; // create the Method "GET" or "POST" [request setHTTPMethod:@"POST"]; //Pass The String to server NSString *userUpdate =[NSString stringWithFormat:@"artist_email=%@ ",your string Name,nil]; //Check The Value what we passed NSLog(@"the data Details is =%@", userUpdate); //Convert the String to Data NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; //Apply the data to the body [request setHTTPBody:data1]; //Create the response and Error NSError *err; NSURLResponse *response; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding]; //This is for Response NSLog(@"got response==%@", resSrt); if(resSrt) { NSLog(@"got response"); } else { NSLog(@"faield to connect"); } 

在NSURLConnection委托方法的帮助下处理结果数据

  NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",@"Raja",@"12345"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:postData]; NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; if( theConnection ){ // indicator.hidden = NO; mutableData = [[NSMutableData alloc]init]; } 

你的PHP代码

 <?php $username = $_POST['username']; $password = $_POST['password']; $result=//check your condition echo $result; ?>