发送HTTP GET请求的xcode(Sending an HTTP GET request xco

2019-10-29 03:39发布

我试图联系表格的内容发送到我的web服务。 我的代码是基于一个名为StackOverflow的话题:在Xcode发送GET方法。

我想我遵守规则,但它仍然无法正常工作。 我不想ü失去你的时间,但我开始生气。

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

我的领域是:姓,名,邮件,电话,MOTIV,问题。 我的web地址是: http://xxx.zfort.net/index.php ?

谢谢大家(即使你没有回应......你都已经帮我在您的意见过去)

PS:这可能是编码值的问题?

Answer 1:

您应该改变方法“POST”或

  1. 附加的GET参数的URL参数
  2. 内容长度设置为0和http体设置为nil


Answer 2:

尝试这个:

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

然后,你还没实现

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html



文章来源: Sending an HTTP GET request xcode