2014-01-31 82 views
1

我建立了移动应用的登录系统,并需要发送的用户名和密码使用方法POST/GET的PHP项目。发送参数PHP,并等待响应

嗯,我看了网上一些教程,看到最教你如何做到这一点,但我需要发送的帖子,并接收通过PHP生成的值,即:

  • 我们发送PHP的登录名和密码
  • 如果登录错误,PHP会显示错误的登录名和密码。
  • 如果你是对的,它显示在屏幕

,并在另一个消息,那就是我想要做的,是超越发送参数,我想收到来自PHP文件的响应,它是可能在IOS?

回答

3

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

您可以使用下面的代码片段,描述in this article

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

设置后弦与实际用户名和密码。

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

2.编码使用NSASCIIStringEncoding后的字符串,你也需要在NSData的格式发送后的字符串。

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

您需要发送数据的实际长度。计算帖子字符串的长度 。

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

3.与像HTTP方法,与柱的字符串的长度的HTTP报头字段中的所有属性创建的URLRequest。创建URLRequest 对象并初始化它。

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"]; 

使用postData设置urlrequest的HTTPBody。现在

[request setHTTPBody:postData]; 

4.,创建URLConnection对象。使用URLRequest对其进行初始化。

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

它返回初始化URL连接,并开始装入数据 的URL请求。您可以检查是否URL连接 正确完成或不使用if/else声明如下。

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

5.从HTTP请求接收数据,则可以使用由URLConnection类参考提供的委托方法。 代表方法如下。

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

以上方法用于接收我们开始使用后 方法的数据。

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

这种方法,可以用来接收 连接的情况下,错误报告是不是服务器发出。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

上述方法被用来处理所述数据连接已取得 后成功。

也参照ThisThis文档POST方法。

这里是与HTTPPost Method.

2

源代码最好的例子与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; 
?> 
3
//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"); 
} 
+0

如果u得到之后的回应你已经使价值得到了利用 –