发送PHP参数,等待响应 [英] Send Parameter for PHP, and wait for response

查看:127
本文介绍了发送PHP参数,等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建移动应用程序的登录系统,需要使用 POST / GET 方法将用户名和密码发送到PHP项目。

I'm building a login system for mobile applications, and need to send the username and password using the method POST / GET to a PHP project.

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

Well I have read some tutorials on the internet and saw that most teaches how to do this, but I need sending the post, and receive the values ​​that are generated through PHP, ie:


  • 我们发送PHP的登录名和密码

  • 如果登录是错了,PHP显示错误登录和密码的消息。

  • 如果你是对的,它会在屏幕上显示另一条消息

而且,这是我想要做的是,除了发送一个参数,我想从PHP文件接收响应,它可能在Ios中?

And, that's what I want to do, is beyond sending a parameter ,I want to receive the response from the PHP file, it is possible in Ios?

推荐答案

以下代码用 POST 方法描述简单示例。(如何通过 POST 传递数据方法

Following code is describe simple example with POST method.(How can pass data by POST method)

您可以使用以下代码段,如上所述

You can use the following code snippet, as described in this article:

这里,我简单描述如何使用POST方法。

Here, I simple describe how can use of POST method.


1。使用实际用户名和密码设置帖子字符串。

1. Set post string with actual username and password.

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

2. 使用 NSASCIIStringEncoding对帖子字符串进行编码以及您需要以NSData格式发送的帖子字符串。

2. Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.

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

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

You need to send the actual length of your data. Calculate the length of the post string.

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

3。使用<$ c $等所有属性创建Urlrequest c> HTTP 方法,http标头字段,其长度为post字符串。创建 URLRequest
对象并初始化它。

3. Create a Urlrequest with all the properties like HTTP method, http header field with length of the post string. Create URLRequest object and initialize it.

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

设置要将数据发送到该请求的网址。

Set the Url for which your going to send the data to that request.

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

现在,设置 HTTP 方法( POST或GET )。在
你的代码中写下这一行。

Now, set HTTP method (POST or GET). Write this lines as it is in your code.

[request setHTTPMethod:@"POST"];

设置 HTTP 标题字段,长度为发布数据。

Set HTTP header field with length of the post data.

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

还设置HTTP标头字段的编码值。

Also set the Encoded value for HTTP header Field.

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

使用postData设置urlrequest的 HTTPBody

Set the HTTPBody of the urlrequest with postData.

[request setHTTPBody:postData];

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

4. Now, create URLConnection object. Initialize it with the URLRequest.

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

它返回初始化的url连接并开始为url请求加载数据
。您可以检查是否网址连接
是否正确使用 if / else 语句,如下所示。

It returns the initialized url connection and begins to load the data for the url request. You can check that whether you URL connection is done properly or not using just if/else statement as below.

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

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

5. To receive the data from the HTTP request , you can use the delegate methods provided by the URLConnection Class Reference. Delegate methods are as below.

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

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

Above method is used to receive the data which we get using post method.

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

此方法,您可以使用接收错误报告,以防
连接不是服务器。

This method , you can use to receive the error report in case of connection is not made to server.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

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

The above method is used to process the data after connection has made successfully.

同时参考 这个 文档用于 POST 方法。

Also Refer This and This documentation for POST method.

以下是源代码 HTTPPost方法。

这篇关于发送PHP参数,等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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