使用ASIHTTPRequest 1.6.2在iPhone中发送POST请求

时间:2011-09-28 05:48:05

标签: iphone ipad asihttprequest

我正在使用ASIHTTPRequest 1.6.2 lib来处理iPhone中的所有http事务。但我不知道,我如何在iPhone中使用ASIHTTPRequest发布数据?

你能给我一些可以在iphone中使用的代码片段吗? 我正在使用以下代码。但我得到的响应代码为0.请帮助我了解我的错误。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
 [request setPostValue:inputXml forKey:@"inputXml"];
 [request setPostValue:@"qftS6TJN343343V84hw=" forKey:@"key"];
 [request setPostValue:@"1.2" forKey:@"version"];
 [request setRequestMethod:@"POST"];
 [request startAsynchronous];

4 个答案:

答案 0 :(得分:1)

以下是如何翻页。它涵盖了所有类型的请求,如获取,表单发布,自定义发布,放置等...

http://allseeing-i.com/ASIHTTPRequest/How-to-use

以下是如何在XCode项目中进行设置:

http://allseeing-i.com/ASIHTTPRequest/Setup-instructions

这些说明和摘要应该适用于iOS。

答案 1 :(得分:1)

您应该使用ASIFormDataRequest

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

答案 2 :(得分:0)

以下是示例代码...

    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
    [request appendPostData:[reqString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request setTimeOutSeconds:60]; 
    [request startAsynchronous];

您将从here获得详细指导。

答案 3 :(得分:0)

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:]];

        NSMutableDictionary *dicData = [[NSMutableDictionary alloc] initWithCapacity:1];
        [dicData setObject:txtPassword.text forKey:@"Password"];
        [dicData setObject:txtEmailID.text forKey:@"Email"];

        [request setPostBody:[[[self parseJsonFromObject:dicData] dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]];

        [request setRequestHeaders:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"application/json", @"Content-Type", nil]];

        [request setDelegate:self];
        [request setDidFinishSelector:@selector(signUpWithEmailFinish:)];
        [request setDidFailSelector:@selector(signUpWithEmailFail:)];
        [request startAsynchronous];

       (void)signUpWithEmailFinish:(ASIHTTPRequest *)request
       {

           if (request.responseStatusCode == 200)
           {

        NSDictionary *responseMessage = [self objectFromJson:request.responseString];
        NSLog(@"ResponseMEssage=%@",responseMessage);
        if (responseMessage)
        {

            if ([responseMessage objectForKey:@"user"] == nil)
            {

                NSLog(@"duplication not allowed");
                [self animation:0];

                return;
            }

            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_LOGIN_SUCCESS object:nil];
            NSLog(@"Registration Complete");
            UserLoginPage *userLogin=[[UserLoginPage alloc]init];

            [self.navigationController pushViewController:userLogin animated:YES];
            return;
        }

    }

}