user2185354
user2185354

Reputation: 519

NSURLConnection send data twice on server

I am creating an iPhone app where i need to send data to server. Using NSURLConnection I'm able to send data but my data is getting send twice. And I'm getting response only once. Can anyone suggest why is this happening

Here is my code

 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;}

Thanks

Upvotes: 0

Views: 851

Answers (2)

Eric Genet
Eric Genet

Reputation: 1260

With your code 2 requests are made.

The first one is sent when you create your NSURLConnection. The [[NSURLConnection alloc] initWitRequest: delegate:] creates the connection and initiate the request asynchronously, sending the data back to the delegate you've specified.

and the second one is made when you call

[NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];

If you want to do a synchronous call to your endpoint:

NSData  *responseData;
responseData=[NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];

if (!error)
{
    // Do your stuff with the response data
}
else
{
    NSLog(@"not connected to server with error %@", error.debugDescription);
}

A good read for URLConnection : Apple Reference

Upvotes: 2

Mani Kandan
Mani Kandan

Reputation: 629

Use the NSUrlConnection delegate to get back both the responses. U can get 2 responses if and only if your server sends 2 responses for the date you are posting.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *response = [[NSString alloc] initWithBytes:[data bytes] length:[data length]
                                                     encoding:NSUTF8StringEncoding];
}

It should work.

Upvotes: 0

Related Questions