问题
I'm trying to send the content of a contact form to my webservice. My code is based on a StackOverFlow topic named : Sending get method in Xcode .
I think i follow the rules but it still doesn't work. I don't want u to lose your time, but i'm starting to get mad.
- (IBAction)send:(id)sender{
NSString *get = [[NSString alloc] initWithFormat:@"&Contact%%5BFirstName%%5D=%@+First&Contact%%5BLastName%%5D=%@+Last&Contact%%5BMail%%5D=%@&Contact%%5BPhone%%5D=%@&Contact%%5BMotivations%%5D=%@&Contact%%5BCommentaires%%5D=%@",prenomtxtfield.text,nomtxtfield.text,emailtxtfield.text,teltxtfield.text,pvController.mail.motiv,questiontxtview.text];
NSData *getData = [get dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getLength = [NSString stringWithFormat:@"%d", [getData length] ];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://xxx.zfort.net/index.php?r=site/contactAdd"]];
[request setHTTPMethod:@"GET"];
[request setValue:getLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:getData];
My fields are : Last Name , First Name , Mail, Phone , Motiv, Question . My webservice addr is : http://xxx.zfort.net/index.php?
Thank you all (even you're not responding... you've all already helped me in the past with your comments)
PS: it may be an issue of encoding values ?
回答1:
You should either change the method to "POST" or
- append your get arguments to the url parameter
- set content-length to 0 and set http body to nil
回答2:
Try this:
NSString * sendstring=[NSString StringWithFormat:@"http://xxx.zfort.net/index.php?r=site/contactAdd&Contact%%5BFirstName%%5D=%@+First&Contact%%5BLastName%%5D=%@+Last&Contact%%5BMail%%5D=%@&Contact%%5BPhone%%5D=%@&Contact%%5BMotivations%%5D=%@&Contact%%5BCommentaires%%5D=%@",prenomtxtfield.text,nomtxtfield.text,emailtxtfield.text,teltxtfield.text,pvController.mail.motiv,questiontxtview.text];
[NSURLConnection initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:sendstring]] delegate:self];
Then you haven to implement
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
and
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
from
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
来源:https://stackoverflow.com/questions/12249662/sending-an-http-get-request-xcode