5

我正在使用下面的代码向 Web 服务发送请求并获得响应,但在这里我一次收到两次请求,我没有在哪里出错,帮助我摆脱这个问题。提前致谢。

 NSString *poststr=[NSString stringWithFormat:@"&cname=%@&conname=%@&email=%@",companynametxt.text,contactnametxt.text,contactEmailtxt.text];
NSLog(@"poststr %@",poststr);


NSData *postData = [poststr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];


NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: @"web servicess"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"DATA%@",theConnection);
[theConnection release];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *filenameStr=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

NSLog(@"filenameStr %@",filenameStr);
4

1 回答 1

1

You are making two separate calls

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

and

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

use either one of them not both.

于 2013-05-03T09:22:51.347 回答