2010-09-03 46 views
3

我很努力發送POST數據到服務器並接收正確的響應。我開始使用setHTTPBody,但當我沒有得到某些POST數據的正確服務器響應時,移動到了setHTTPBodyStream,並且我讀了setHTTPBody在早期的iOS版本中發生了內存泄漏。問題是setHTTPBodyStream導致錯誤 - 「操作無法完成,連接重置由對等」。「操作無法完成,連接被同級重置。」與iOS上的NSURLConnection iOS

下面的代碼:

NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"secret but correct url goes here"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]; 
     [request setHTTPMethod: @"POST"]; 
     [request setHTTPBodyStream: [NSInputStream inputStreamWithData: [[NSString stringWithFormat:@"username=%@&password=%@&score=%i",[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)ogl_view->username, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease],[(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)text_field.text, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease],ogl_view->score[0]] dataUsingEncoding:NSUnicodeStringEncoding]]]; 
     NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate:self]; 
     if (connection){ 
      received_data = [[NSMutableData data] retain]; 
      ogl_view->section = CURLING_PROCESS_CREDENTIALS; 
     }else{ 
      ogl_view->section = CURLING_LOGIN_OR_REGISTER; 
      UIAlertView *connection_alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Can't connect to server" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil]; 
      [connection_alert show]; 
      [connection_alert release]; 
     } 

我可以驗證服務器是罰款和它的作品時,我嘗試了網絡瀏覽器。

有人能讓我走上正確的軌道,正確地使用HTTP和POST方法發送數據嗎?

謝謝你的幫助。

回答

0

嘗試使用這個(用自己的價值觀):

NSString* post = @"username=myUser&password=myPass&score=20"; //customize this 
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    NSURL *url = [NSURL URLWithString:@"http://my.request.com"]; //customize this 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 
    [request setTimeoutInterval:timeout]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

然後您就需要實現NSURLConnectionDelegate方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)remoteData; 
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection; 
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; 

最後,記得要釋放NSURLConnection的。

編輯:沒有看到你寫了setHTTPBody leaks ...從未見過這種情況。無論如何,這裏有一些代碼應該可以工作......

+0

在[github](https://github.com/caxaria/RemoteConnector)中獲得了一個示例項目,其中有幾個類用於執行請求。希望能幫助到你。 – 2011-08-16 12:16:36

相關問題