2013-03-06 55 views
0

所以我試圖通過帶有參數的URL將數據發送到web服務。我有的代碼是在下面,但它從來沒有命中服務器。請求和響應爲空。我究竟做錯了什麼?Obj-C NSURLConnection不起作用

-(void) postData:(NSString *)data{ 

NSURLResponse* response; 
NSError* error = nil; 
NSString *urlString = [NSString stringWithFormat:@"http://someaddress.com/api?data=%@", data]; 

NSURL *lookupURL = [NSURL URLWithString:urlString]; 

//Create the request. 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:lookupURL]; 

NSData *request = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 

NSString *dataString = [[NSString alloc] initWithData:request encoding:NSUTF8StringEncoding]; 
NSLog(@"-----------------------------"); 
NSLog(@"Request: %@", theRequest); 
NSLog(@"req response: %@", request); 
NSLog(@"response: %@", dataString);} 
+1

請更改標題,因爲它工作 – 2013-03-06 16:36:13

+0

但是你的代碼做了一個GET沒有發佈+你不要編碼數據 – 2013-03-06 16:36:40

回答

0

你想POST一些二進制數據,但你做了一個GET請求,並嘗試把二進制文件放到url中。 (無編碼它)

樣品後:

NSURL *url = [NSURL URLWithString:@"http://server.com"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
request.HTTPMethod = @"POST"; 
request.HTTPBody = postData; 
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

還,注意同步得到的是壞的,因爲它塊:)使用異步聯網!