2011-09-26 176 views
0

我試圖使用Google Data API上傳包含某些地方的CSV文件。HTTP請求未設置內容長度

我有這段代碼:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:direccion]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:direccion]]; 

NSData *postData = [creacionCSV dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"2.0" forHTTPHeaderField:@"GData-Version"]; 
[request setValue:@"application/vnd.google-earth.kml+xml" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"Mapa de prueba" forHTTPHeaderField:@"Slug"]; 
[request setHTTPBody:postData]; 
[request setHTTPMethod:@"POST"]; 

NSData *datosRecibidos; 
datosRecibidos = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *token = [[NSString alloc] initWithData:datosRecibidos encoding:NSASCIIStringEncoding]; 
NSLog(@"%@",token); 

哪裏creacionCSV是與所有的KML代碼一個NSString。 一旦執行,我已經從服務器獲得此answe:

<html lang=en> 
    <meta charset=utf-8> 
    <title>Error 411 (Length Required)!!1</title> 
    <style> [...] </style> 
    <a href=//www.google.com/ id=g><img src=//www.google.com/images/logo_sm.gif alt=Google></a> 
    <p><b>411.</b> <ins>That’s an error.</ins> 
    <p>POST requests require a <code>Content-length</code> header. 
    <ins>That’s all we know.</ins> 

所以,我收到411錯誤。我檢查了標題,它不是零也不是空的。爲什麼不把這個頭部我的POST?

非常感謝!

+0

如果我在最後一行設置了一個斷點,我可以看到self.tokenAutorizacion不是零也不是空的,但是如果我爲[request valueForHTTPHeaderField:@「Authorization」]打印對象,那麼它就是零。這是我的問題。我不知道爲什麼我的請求沒有使用任何授權HTTP標頭 – Alex

+1

對於同步請求:'如果需要進行身份驗證才能下載請求,則必須將所需憑據指定爲URL的一部分。如果身份驗證失敗或缺少憑證,則連接將嘗試繼續,而無需憑據。「# – Nekto

+0

它對我來說就是這樣工作的。我在URL&token = MY_TOKEN的末尾添加了它,並且它工作正常。謝謝! – Alex

回答