我正在创建一个 iPhone 应用程序,我需要将数据发送到服务器。使用 NSURLConnection 我可以发送数据,但我的数据被发送了两次。而且我只收到一次回复。谁能建议为什么会这样
这是我的代码
NSURL *url=[NSURL URLWithString:[APIServiceURL geturl]];
NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength=[NSString stringWithFormat:@"%d",[soapMsg1 length]];
[req addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req addValue:@"http://tempuri.org/InsertPostComment" forHTTPHeaderField:@"SOAPAction"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg1 dataUsingEncoding:NSUTF8StringEncoding]];
// Response
NSHTTPURLResponse *urlResponse=nil;
NSError *error;
connection =nil;
connection=[[NSURLConnection alloc]initWithRequest:req delegate:self];
NSData *responseData;
;
if (connection)
{
responseData=[NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];
}
else
{
NSLog(@"not connected to server");
}
if ([responseData length]>0)
{
NSString *responseString=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseString %@",responseString);
responseString =nil;}
谢谢