gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:25:47 24 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

8年前关闭。




Improve this question




我正在为移动应用构建一个登录系统,需要使用的方法发送用户名和密码发布/获取 到一个 PHP 项目。

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

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

  • 而且,这就是我想要做的,除了发送参数之外,我想接收来自 PHP 文件的响应,在 Ios 中可以吗?

    最佳答案

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

    您可以使用以下代码片段,如 in this article 所述:

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

    1. Set post string with actual username and password.

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

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

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

    [request setHTTPMethod:@"POST"];

    Set HTTP header field with length of the post data.

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

    Also set the Encoded value for HTTP header Field.

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

    Set the HTTPBody of the urlrequest with postData.

    [request setHTTPBody:postData];

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

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

    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. 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

    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.



    另请参阅 This This 文档 对于 POST方法。

    这是最好的例子,源代码为 HTTPPost Method.

    关于ios - 为 PHP 发送参数,并等待响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480590/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com