HTTP请求不设置内容长度

时间:2011-09-26 17:06:23

标签: objective-c ipad http http-headers

我正在尝试使用Google Data API上传某些地方的CSV文件。

我有这段代码:

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。 一旦执行,我就从服务器获得了这个回复:

<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错误。我检查了标题,它不是零也不是空的。为什么不把我的帖子拿到那个标题?

非常感谢!

1 个答案:

答案 0 :(得分:0)

如何构建tokenAutorizacion,base64编码?查看此页面:http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html