Objective-c 模拟http请求

这个是b比较成套的网络请求方法,网站:http://allseeing-i.com/ASIHTTPRequest/How-to-use

如果用postfa方法提交则,

NSURL*url = [NSURLURLWithString:@"http://192.168.0.120:8888/web/Login.php"];

ASIFormDataRequest*request = [ASIFormDataRequestrequestWithURL:url];

//ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];

[requestsetPostValue:@"userName"forKey:@"userName"];

[requestsetPostValue:@"password"forKey:@"password"];

//[request setDelegate:self];

[requeststartSynchronous];

NSError*error = [requesterror];

if(!error) {

NSString*response = [requestresponseString];

NSLog(@"response = %@",response);

}

get方法:

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

[request startSynchronous];

NSError *error = [request error];

if (!error) {

NSString *response = [request responseString];

}

或者试用NSMutableURLRequest:

NSString *myRequestString = @"&userName=baowu&password=123456";

NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];

NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://192.168.0.120:8888/web/Login.php"]];

[ request setHTTPMethod: @"POST" ];

[ request setHTTPBody: myRequestData ];

NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];

NSString* aStr = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];

NSLog(@"aStr = %@",aStr);