Two processing methods (synchronous and asynchronous) for post connection in iphone Network (zz)

Source: Internet
Author: User

Method 1: direct return.
-(Void) UpadaPost :( NSString *) strcontext URL :( NSString *) urlstr {

NSLog (urlstr );
NSLog (strcontext );
Assert (strcontext! = NULL );
Assert (urlstr! = NULL );

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

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

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init] autorelease];
[Request setURL: [NSURL URLWithString: urlstr];
[Request setHTTPMethod: @ "POST"];
[Request setValue: postLength forHTTPHeaderField: @ "Content-Length"];
[Request setValue: @ "application/x-www-form-urlencoded" forHTTPHeaderField: @ "Content-Type"];
[Request setHTTPBody: postData];

NSURLResponse * respone;
NSError * error;
NSData * myReturn = [NSURLConnection sendSynchronousRequest: request returningResponse: & respone
Error: error];
NSLog (@ "% @", [[NSString alloc] initWithData: myReturn encoding: NSUTF8StringEncoding]);
}

Second, event proxy


-(Void) UpadaPost :( NSString *) strcontext URL :( NSString *) urlstr {

NSLog (urlstr );
NSLog (strcontext );
Assert (strcontext! = NULL );
Assert (urlstr! = NULL );

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

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

NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init] autorelease];
[Request setURL: [NSURL URLWithString: urlstr];
[Request setHTTPMethod: @ "POST"];
[Request setValue: postLength forHTTPHeaderField: @ "Content-Length"];
[Request setValue: @ "application/x-www-form-urlencoded" forHTTPHeaderField: @ "Content-Type"];
[Request setHTTPBody: postData];

NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest: request delegate: self];
If (conn)
{
NSLog (@ "Connection success ");
[UIApplication sharedApplication]. networkActivityIndicatorVisible = YES;
[Conn retain];

}
Else
{
// Inform the user that the download cocould not be made
}

}

 

// Triggered when the response is received
-(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {
// Note that only NSURLResponse objects can be converted to NSHTTPURLResponse objects.
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
If ([response respondsToSelector: @ selector (allHeaderFields)]) {
NSDictionary * dictionary = [httpResponse allHeaderFields];
NSLog ([dictionary description]);
NSLog (@ "% d", [response statusCode]);

}
}


// Forward errors to the delegate.
// Link Error
-(Void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {
// [Self initiate mselecw.mainthread: @ selector (httpConnectEnd) withObject: nil waitUntilDone: NO];
NSLog (@ "% @", [error localizedDescription]);

}

// Called when a chunk of data has been downloaded.
// Each time data is received, it is called.
-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {
// Process the downloaded chunk of data.

NSLog (@ "% d", [data length]);
// NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);

// [Self initialize mselecw.mainthread: @ selector (updateProgress) withObject: nil waitUntilDone: NO];


}
// The receiving ends.
-(Void) connectionDidFinishLoading :( NSURLConnection *) connection {

NSLog (@ "% @", connection );
// NSLog (@ "% lld", received _);

// [Self initiate mselecw.mainthread: @ selector (httpConnectEnd) withObject: nil waitUntilDone: NO];
// Set the condition which ends the run loop.
}


This is an excerpt from the column of the advanced coders.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.