登录站点

用户名

密码

关于iphone开发-http网络连接

已有 51 次阅读  2010-03-08 17:43   标签iphone  网络  开发 


NSString* urlString=[[NSString alloc] initWithFormat:@"http://gokei.jp/myName.php?name=%@",myName.text];

NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

NSData *response = [NSURLConnection sendSynchronousRequest:requestObj returningResponse: nil error: nil];

NSString * theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

NSLog(@"response: %@", theString);//搞出来看看


response是NSData类型,可以是图片可以是视频,比如是图片要保存可以这样

[response writeToFile:pictureSavePath atomically:YES];

非NSString类型都不用编码,直接保存即可。


如果要表示页面,即只是在iphone上显示返回内容,可以把请求直接丢给浏览器

以上面为例

UIWebView *myBrowser=[[UIWebView alloc] initWithFrame:.......];

[self addSubview: myBrowser];

[myBrowser loadRequest:requestObj];


要捕捉连接必须继承UIWebViewDelegate

然后初始化以后加入

myBrowser.delegate = self;


之后所有在myBrowser里的行为都可以用

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {}

抓到,通过处理request 来完成,比如下载文件。

NSURLRequest默认是多线程。

分享 举报