发送HTTP GET请求xcode

时间:2012-09-03 14:20:56

标签: xcode http get request

我正在尝试将联系表单的内容发送到我的网络服务。 我的代码基于一个名为的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];

我的字段是:姓氏,名字,邮件,电话,动机,问题。 我的网络服务地址是:http://xxx.zfort.net/index.php

谢谢大家(即使你没有回复......过去你的评论已经帮助过我了)

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

2 个答案:

答案 0 :(得分:0)

您应该将方法更改为“POST”或

  1. 将您的get参数附加到url参数
  2. 将content-length设置为0并将http body设置为nil

答案 1 :(得分:0)

试试这个:

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