2009-04-17 40 views

回答

-2

這裏是所行的一個POST調用一些基本代碼:

//url is the appropriate url for the http POST call 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
      [theRequest setHTTPMethod:@"POST"]; 

      NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 
      if(theConnection) 
      { 
       webData = [[NSMutableData data]retain]; 
      } 
      else 
      { 
       NSLog(@"theConnection is NULL"); 
      } 

您需要實現合適的NSURLConnection的委託方法。

0

在谷歌搜索的第二個答案看起來像什麼,你可能需要:

0

//可以驅動一個同步NSURLConnection的使用sendSynchronousRequest:returningResponse:錯誤: //但直到接收到響應

其將阻止整個螺紋// thebodyData =有效載荷發送到服務器(在正確的格式) // theMimeType =有效載荷的mineType // url是適用於http POST調用的url

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
     [theRequest setHTTPMethod:@"POST"]; 

     NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 
     if(theConnection) 
     { 
      webData = [[NSMutableData data]retain]; 
      // give the details of the payload -- mine time and body content. 
      [theRequest setValue: theMimeType forHTTPHeaderField:@"Content-Type"]; 
      [theRequest setHTTPBody:theBodyData]; 

     } 
     else 
     { 
      NSLog(@"theConnection is NULL"); 
     } 

// the delegate methods templates... 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength:0]; // clear the data incase it was a redirect in between. 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webData appendData:data]; // collect the data from server as it comes in. 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [[NSAlert alertWithError:error] runModal]; // report the error 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // Once this method is invoked, "webData" contains the complete result 
} 
相關問題