|
原文: Creating and running requests Creating a synchronous request The simplest way to use ASIHTTPRequest. Sending the startSynchronous message will execute the request in the same thread, and return control when it has completed (successfully or otherwise). Check for problems by inspecting the error property. To get the response as a string, call the responseString method. Don’t use this for binary data - use responseData to get an NSData object, or, for larger files, set your request to download to a file with the downloadDestinationPath property. - (IBAction)grabURL:(id)sender { 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]; } } In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application’s user interface will lock up and become unusable for the duration of the request. 译文: 创建和运行请求 创建一个同步请求 这是最简单的用法,发送 startSynchronous 消息将在相同线程中执行请求,不管是否成功,完成后返回控制。 查看error属性以检测问题。 要以字符串形式得到响应,就调用responseString方法。这个方法不适合二进制数据 - 你应该使用responseData 得到NSData对象,或者如果有更大的文件,你可以设置downloadDestinationPath将请求下载到文件。 - (IBAction)grabURL:(id)sender { 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]; } } 注意:一般的,你应该优先使用异步请求,如果你在主线程中使用ASIHTTPRequest的同步方法,程序的ui在请求过程中将被锁定而无法响应。 |






