iphone 实行http post 及多线程编程实例
#import "FirstUIAppDelegate.h"
@implementation FirstUIAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
//[self postdata];
[self setupThread];
[window makeKeyAndVisible];
}
-(void)setupThread{
[NSThread detachNewThreadSelector:@selector(postdata)toTarget:self withObject:nil];
}
- (void)dealloc {
[window release];
[super dealloc];
}
-(void)postdata{
NSLog(@"TEST");
NSURL *url;
NSMutableURLRequest *urlRequest;
NSMutableData *postBody = [NSMutableData data];
url = [NSURL URLWithString:@"http://allove.org"];
urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];
NSString *udid = @"phong=15871485145&psw=23";
[postBody appendData:[udid dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES]];
[urlRequest setHTTPBody:postBody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
if(returnData)
{
NSString *result = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSLog(@"result=======>%@",result);
}
else
{
NSLog(@"Wrong Connetion!");
}
}
@end