Note that there are some explanatory texts on larger screens.

plurals
  1. POSending an HTTP POST request on iOS
    primarykey
    data
    text
    <p>I'm trying to send an HTTP Post with the iOS application that I'm developing but the push never reaches the server although I do get a code 200 as response (from the urlconnection). I never get a response from the server nor does the server detect my posts (the server does detect posts coming from android)</p> <p>I do use ARC but have set pd and urlConnection as strong.</p> <p>This is my code for sending the request</p> <pre><code> NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",dk.baseURL,@"daantest"]]]; [request setHTTPMethod:@"POST"]; [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; NSString *sendString = @"&lt;data&gt;&lt;item&gt;Item 1&lt;/item&gt;&lt;item&gt;Item 2&lt;/item&gt;&lt;/data&gt;"; [request setValue:[NSString stringWithFormat:@"%d", [sendString length]] forHTTPHeaderField:@"Content-length"]; [request setHTTPBody:[sendString dataUsingEncoding:NSUTF8StringEncoding]]; PushDelegate *pushd = [[PushDelegate alloc] init]; pd = pushd; urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:pd]; [urlConnection start]; </code></pre> <p>this is my code for the delegate</p> <pre><code>#import "PushDelegate.h" @implementation PushDelegate @synthesize data; -(id) init { if(self = [super init]) { data = [[NSMutableData alloc]init]; [data setLength:0]; } return self; } - (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten { NSLog(@"didwriteData push"); } - (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes { NSLog(@"connectionDidResumeDownloading push"); } - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL { NSLog(@"didfinish push @push %@",data); } - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite { NSLog(@"did send body"); } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.data setLength:0]; NSHTTPURLResponse *resp= (NSHTTPURLResponse *) response; NSLog(@"got response with status @push %d",[resp statusCode]); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { [self.data appendData:d]; NSLog(@"recieved data @push %@", data); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]; NSLog(@"didfinishLoading%@",responseText); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error ", @"") message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil] show]; NSLog(@"failed &amp;push"); } // Handle basic authentication challenge if needed - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"credentials requested"); NSString *username = @"username"; NSString *password = @"password"; NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } @end </code></pre> <p>The console always prints the following lines and the following lines only:</p> <pre><code>2013-04-01 20:35:04.341 ApprenticeXM[3423:907] did send body 2013-04-01 20:35:04.481 ApprenticeXM[3423:907] got response with status @push 200 2013-04-01 20:35:04.484 ApprenticeXM[3423:907] didfinish push @push &lt;&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload