0

我正在尝试将联系表的内容发送到我的网络服务。我的代码基于 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:这可能是编码值的问题?

4

2 回答 2

0

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

  1. 将您的 get 参数附加到 url 参数
  2. 将 content-length 设置为 0 并将 http body 设置为 nil
于 2012-09-03T14:44:04.557 回答
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

于 2012-09-04T02:06:24.787 回答