发送HTTP GET请求xcode [英] Sending an HTTP GET request xcode
问题描述
我试图将联系表单的内容发送到我的web服务。
我的代码基于一个名为StackOverFlow的主题:在Xcode中发送get方法。
我认为我遵循规则,但它仍然不起作用。我不想让你失去时间,但我开始生气。
- (IBAction)send: (id)sender {
NSString * get = [[NSString alloc] initWithFormat:@& Contact %% 5BFirstName %% 5D =%@ + First& Contact %% 5BLastName% %5D =%@ +最后&安培;联系%% 5BMail %% 5D =%@&安培;联系%% 5BPhone %% 5D =%@&安培;联系%% 5BMotivations %% 5D =%@&安培;联系%% 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-urlencodedforHTTPHeaderField:@Content-Type];
[request setHTTPBody:getData];
我的字段是:姓氏,名字,邮件,电话,Motiv,问题。
我的web服务地址是: http://xxx.zfort.net/index.php?
谢谢大家(即使你没有回应...你以前的评论都已经帮助过我)
PS:它可能是一个编码值问题? 试试这个:
p> NSString * sendingtring = [NSString StringWithFormat:@http://xxx.zfort.net/index.php?r=site/ contactAdd和放大器;联系%% 5BFirstName %% 5D =%@ +首先和放大器;联系%% 5BLastName %% 5D =%@ +尾和放大器;联系%% 5BMail %% 5D =%@&放大器;联系%% 5BPhone %% 5D =%@ &安培;联系%% 5BMotivations %% 5D =%@&安培;联系%% 5BCommentaires %% 5D =%@,prenomtxtfield.text,nomtxtfield.text,emailtxtfield.text,teltxtfield.text,pvController.mail.motiv,questiontxtview。文本];
[NSURLConnection initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:sendingtring]] delegate:self];
然后您不必执行
<$ p $ (NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)响应
$ b (NSURLConnection *)连接didReceiveData:(NSData *)数据
$ p
$ $ p $ $ b
from
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 ?
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
这篇关于发送HTTP GET请求xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!