【问题标题】:HTTP Request does not set Content LengthHTTP 请求未设置内容长度
【发布时间】:2011-09-26 17:06:23
【问题描述】:

我正在尝试使用 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 错误。我检查了标题,它既不是零也不是空的。为什么我的 POST 不采用该标题?

非常感谢!

【问题讨论】:

  • 如果我在最后一行设置断点,我可以看到 self.tokenAutorizacion 不是 nil 也不是空的,但是如果我为 [request valueForHTTPHeaderField:@"Authorization"] 打印对象,它是一个 nil。那是我的问题。我不知道为什么我的请求没有任何 Authorization HTTP 标头
  • 同步请求:If authentication is required in order to download the request, the required credentials must be specified as part of the URL. If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
  • 这样对我有用。我在 URL &token=MY_TOKEN 的末尾添加了它,它起作用了。谢谢!
  • 我现在遇到问题 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];似乎没问题,但是一旦 POST 请求完成,我从服务器得到了这个答案:错误 411(需要长度)

标签: objective-c ipad http http-headers


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2012-05-01
    • 2012-04-07
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多