Sending POST request in iPhone using ASIHTTPRequest 1.6.2
I am using ASIHTTPRequest 1.6.2 lib for all http transactions in IPhone. But i dont know, how can i post the data with ASIHTTPRequest in iPhone?
Can you please give me the code snippet which will work in in iphone? I am using the following code for this. But i am getting response code as 0. Please help me to understand where i am going wrong.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:inputX开发者_StackOverflow中文版ml forKey:@"inputXml"];
[request setPostValue:@"qftS6TJN343343V84hw=" forKey:@"key"];
[request setPostValue:@"1.2" forKey:@"version"];
[request setRequestMethod:@"POST"];
[request startAsynchronous];
You should use 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"];
Here's the how to page. It covers all types of requests such as get, form post, custom post, put, etc...
http://allseeing-i.com/ASIHTTPRequest/How-to-use
Here's how to set it up in your XCode project:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
Those instructions and snippets should work in iOS.
Following is the sample code...
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request appendPostData:[reqString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
You will get detail guidance from here.
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;
}
}
}
精彩评论