2011-09-26 178 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

回答