发送请求到服务器iOS

时间:2012-07-20 12:10:49

标签: ios asihttprequest

我正在试图弄清楚如何将数据发送到服务器,但目前没有任何进展。

我所知道的

在服务器上我有一些php脚本,它会在响应中返回数据

例如使用此网址:http://test.com/mobile_api/register

此脚本获取下一个参数:

id
name
info
time

所以我需要的字符串如下所示

http://test.com/mobile_api/register?id=1000&name=alex&info=1.0&time=10000

发送此信息的最佳方式是服务器上的字符串

现在我正在尝试使用ASIHTTPRequest。任何人都可以发送一个例子,说明如何使用我的参数创建正确的请求。

1 个答案:

答案 0 :(得分:1)

此示例代码可以帮助您

-(void)sendRequest
{
    int userId=10, time=10000;
    NSString *name = @"ABC";
    float info = 1.0;
    NSString *urlString = [NSString stringWithFormat:@"http://test.com/mobile_api/register?id=%d&name=%@&info=%f&time=%d",userId,name,info,time];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
    //You need to add ASIHTTPRequestDelegate in header(.h) file
    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];

    // Use when fetching binary data
    NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
}