Sending an HTTP GET request xcode

2019-08-26 18:10发布

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 ?

2条回答
Fickle 薄情
2楼-- · 2019-08-26 18:58

You should either change the method to "POST" or

  1. append your get arguments to the url parameter
  2. set content-length to 0 and set http body to nil
查看更多
相关推荐>>
3楼-- · 2019-08-26 19:06

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

查看更多
登录 后发表回答