简体   繁体   English

将请求发送到服务器iOS

[英]Send request to server iOS

I am trying to figure out how to send data to the server but currently without any progress. 我试图弄清楚如何将数据发送到服务器,但目前没有任何进展。

What I know 我知道的

On the server I have got some php script that return me data in response 在服务器上,我有一些php脚本可以作为响应返回我的数据

for example with this URL: http://test.com/mobile_api/register 例如使用以下URL: http://test.com/mobile_api/register : http://test.com/mobile_api/register

this script get next parameters: 该脚本获取下一个参数:

id
name
info
time

so the string which I need looking like below 所以我需要的字符串如下所示

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

What is best way to send this is string on the server 最好的发送方式是服务器上的字符串

Now I'm trying to use ASIHTTPRequest . 现在,我正在尝试使用ASIHTTPRequest Can anybody send an example how to create correct request with my parameters. 任何人都可以发送示例如何使用我的参数创建正确的请求。

This sample code should help you 此示例代码应为您提供帮助

-(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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM