文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Objective-c 模拟http请求

Objective-c 模拟http请求

时间:2011-04-04  来源:{{°莪乖乖

这个是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);
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载