HTTP 请求未设置内容长度

发布于 2024-12-06 17:28:19 字数 1698 浏览 1 评论 0原文

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

非常感谢!

I'm trying to use Google Data API to upload a CSV file with some places.

I've this piece of code:

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);

Where creacionCSV is an NSString with all the KML code.
Once executed, I've obtained this answe from server:

<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>

So I'm getting that 411 Error. I've checked the header and it's not nil nor empty. Why is not taking that header my POST?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2024-12-13 17:28:19

如何构建 tokenAutorizacion(base64 编码)?请参阅此页面: http://cocoawithlove.com/2009 /06/base64-encoding-options-on-mac-and.html

How do you build tokenAutorizacion, the base64 encoding? See this page: http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文