2011-05-12 67 views
0

我要上傳到互聯網資源2個值HTTP GET方法在Xcode

我創建了一個PHP函數 的 「http://資源/頂/藝術家= VALUE1 &標題= VALUE2」

但我有一點點失去了如何開發這個在Xcode的側

非常感謝您的回答

回答

0

有幾種方法來獲取一個URL,一些更容易,一些更難實現。作爲一個快速啓動,對於沒有經驗的開發商,這oneliner應該做的:

NSString *s=[NSString stringWithContentsOfURL:[NSURL urlWithString:@"http://url/..."]]; 

要做到這一點的正確方法(檢查錯誤,而不是阻止你的程序),看看NSURLConnection的,或如ASIHTTPRequest捆綁了很多功能。

1

你可以把這個作爲一個例子

NSString *post=[NSString stringWithFormat:@"userId=%@&password=%@&device=%@&amount=%@",[Login retuserid],[passwordfield text],[Login retdevid],[amountfield text]]; 
     NSLog(@"post string is :%@",post); 
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

     NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
     [request setURL:[NSURL URLWithString:@"http://203.217.146.44:81/POS/api.php?fn=cashRequest"]]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 

     NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
     NSString *replyString = [[NSString alloc] initWithBytes:[serverReply bytes] length:[serverReply length] encoding: NSASCIIStringEncoding]; 
     NSLog(@"reply string is :%@",replyString);