我正在創建一個iPhone應用程序,我需要將數據發送到服務器。使用NSURLConnection,我可以發送數據,但我的數據發送兩次。我只得到一次迴應。 任何人都可以說明爲什麼會出現這種情況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;}
感謝
我們怎麼知道不知道代碼? – Raptor
根據您發佈的代碼,只有一個請求已經完成,所以您得到了一次響應。你想要問什麼?闡述。 –
是隻做出一個請求,但數據在服務器上發佈兩次。 – user2185354