2009-07-30 55 views
0

如何從Cocoa Touch的服務器上傳/下載數據。這裏是我迄今爲止...使用Cocoa Touch從服務器上傳和下載數據?

-(void)uploadSchedule:(id)sender 
{ 
    NSData *content = [NSData dataWithContentsOfFile:self.dataFilePath]; 
    NSString *stuff = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding]; 

    NSURL *url = [NSURL URLWithString:@"http://thetis.lunarmania.com"]; 
    NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc]initWithURL:url]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setHTTPBody:[stuff dataUsingEncoding:NSASCIIStringEncoding]]; 

    NSLog(@"great success!"); 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // this method is called when the server has determined that it 
    // has enough information to create the NSURLResponse 

    // it can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 
    // receivedData is declared as a method instance elsewhere 

    [receivedData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // append the new data to the receivedData 
    // receivedData is declared as a method instance elsewhere 

    [receivedData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    // release the connection, and the data object 
    [connection release]; 
    // receivedData is declared as a method instance elsewhere 
    [receivedData release]; 

    // inform the user 
    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 
    UIImage *image = [[UIImage alloc] initWithData:receivedData]; 
    [cat setImage:image]; 
    [image release]; 

    // receivedData is declared as a method instance elsewhere 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
} 

-(void)connection:(NSURLConnection *)connection 
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge previousFailureCount] == 0) { 
     NSURLCredential *newCredential; 
     newCredential=[NSURLCredential credentialWithUser:@"[email protected]" 
               password:@"icanican" 
               persistence:NSURLCredentialPersistenceNone]; 
     [[challenge sender] useCredential:newCredential 
       forAuthenticationChallenge:challenge]; 
    } else { 
     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
     // inform the user that the user name and password 
     // in the preferences are incorrect 
     //[self showPreferencesCredentialsAreIncorrectPanel:self]; 
    } 
} 

我迷茫......

回答

0

這已經涵蓋在這裏:

NSURLRequest - encode url for NSURLRequest POST Body (iPhone objective-C)

接受的答案使用ASIHTTPRequest這是類似於我用過的,並且可以很容易地從HTML表單發佈/獲取。這裏有一個例子(從過去的計算器)

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://someSite.com"] autorelease]; 
[request setPostValue:@"myValue1" forKey:@"myFormField1"]; 
[request setPostValue:@"myValue2" forKey:@"myFormField2"]; 
// etc. 
[request start]; 
NSError *error = [request error]; 
if (!error) 
    NSString *response = [request responseString]; 
1

的代碼崩潰,因爲你過度釋放connection。查看Cocoa memory management rules

除此之外,您必須更具體地瞭解您遇到的問題。

順便說一句,術語是「實例變量」,而不是「方法實例」。實例變量是實例內部的變量,與方法無關。

0

如果你的文件很大,你最好用NSFilehandle來寫數據到didReceiveData裏面,而不是追加。

相關問題