2009-02-26 50 views

回答

4
// create the request 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] 
         cachePolicy:NSURLRequestUseProtocolCachePolicy 
        timeoutInterval:60.0]; 
// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    // Create the NSMutableData that will hold 
    // the received data 
    // receivedData is declared as a method instance elsewhere 
    receivedData=[[NSMutableData data] retain]; 
} else { 
    // inform the user that the download could not be made 
} 

See this resource

4

我建議使用All-Seeing Interactive的優秀ASIHTTPRequest源代碼:http://allseeing-i.com/ASIHTTPRequest。我正在這樣做,所以有幾個已發佈的iPhone應用程序,所以你可以確信代碼非常穩定。

這是一個圍繞CFNetwork API的包裝,它使與Web服務器通信更加單調乏味。它用Objective-C編寫,適用於Mac OS X和iPhone應用程序。

它適用於執行基本HTTP請求並與基於REST的服務(GET/POST/PUT/DELETE)交互。 ASIFormDataRequest子類可以使用multipart/form-data輕鬆提交POST數據和文件。

+0

當我學習時,很高興知道如何使用特定代碼完成工作。之後,我可能希望使用開源代碼簡化工作。不管怎麼說,還是要謝謝你。 – 2009-02-27 08:03:16