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.