发送请求并获取响应 [英] Sending Request and Get Response

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

问题描述

我的服务器上运行了一个运行我的Web服务的PHP代码。它以发送整数值处理数据。我怎么能得到它?这是我的请求网址:

I have a php code running on my server that i call my web service.It processes the data in send integer value.How can i get that?Here is my request url :

  NSString *requestURL=[NSString stringWithFormat:@"%@?u=%@&  p=%@&platform=ios",url,txtUserName.text,txtPassword.text];

更新评论:我的服务器上有一个php文件。它需要3个参数并注册我的用户和返回值为1(成功,2表示重复)。我需要将请求发送到我的服务器:

Update Comment : I have a php file on my server.It takes 3 arguments and register my users and returns value like 1(for success,2 for duplicate).I need to send request to my server as :

url="http://smwebtech.com/Pandit/web_service/signup.php?u=Test&p=password&platform=ios"

如何将此请求发送到服务器并从服务器获取返回值?

How can i send this request to server and get the return value from server?

推荐答案

您可以使用NSURLConnection。您应该使用 NSURLConnectionDataDelegate 协议实现并使用 NSURLConnection 类。

You can use NSURLConnection. You should use implement the NSURLConnectionDataDelegate protocol and use the NSURLConnection class.

-(void) requestPage
{
    NSString *urlString = @"http://the.page.you.want.com";
    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f];


    responseData = [[NSMutableData alloc] init];
    connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
    delegate = target;
}


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   
    if ([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
        //If you need the response, you can use it here
    }
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [responseData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == adCheckConnection)
    {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        //You've got all the data now
        //Do something with your response string


        [responseString release];
    }

    [responseData release];
    [connection release];
}

这篇关于发送请求并获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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