【问题标题】:http post to upload file to google sites on iphonehttp post 将文件上传到 iphone 上的谷歌网站
【发布时间】:2012-03-08 12:26:42
【问题描述】:

我正在尝试将 kml 文件上传到 Google 协作平台。 api在这里-http://code.google.com/apis/sites/docs/1.0/developers_guide_protocol.html#UploadingFiles,他们给出了这个例子-

POST /feeds/content/domainName/siteName HTTP/1.1
Host: sites.google.com
GData-Version: 1.4
Authorization: <your authorization header here>
Content-Length: 7221984
Content-Type: multipart/related; boundary=END_OF_PART
--END_OF_PART
Content-Type: application/atom+xml

<entry xmlns="http://www.w3.org/2005/Atom">
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/sites/2008#attachment" label="attachment"/>
<link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml"
href="https://sites.google.com/feeds/content/domainName/siteName/PARENT_ENTRY_ID"/>
<title>PDF File</title>
<summary>HR packet</summary>
</entry>

--END_OF_PART
Content-Type: application/pdf

... pdf contents here ...

--END_OF_PART--

我正在尝试使用 NSMutableURLRequest 来执行这篇文章。这是我的代码-

NSString *boundary=@"END_OF_PART";    
NSMutableString *bodyString = [[NSMutableString alloc]initWithCapacity:50];
[bodyString appendFormat:@"--%@\n",boundary];
[bodyString appendString:@"Content-Type: application/atom+xml\n"];
[bodyString appendString:@"<entry xmlns=\"http://www.w3.org/2005/Atom\">\n"];
[bodyString appendString:@"<category scheme=\"http://schemas.google.com/g/2005#kind\"\n"];
[bodyString appendString:@"term=\"http://schemas.google.com/sites/2008#attachment\" label=\"attachment\"/>\n"];
[bodyString appendString:@"<link rel=\"http://schemas.google.com/sites/2008#parent\" type=\"application/atom+xml\"\n"];
[bodyString appendString:@"href=\"https://sites.google.com/feeds/content/itourlocal/kmlfiles\"/>\n"];
[bodyString appendFormat:@"<title>%@.kml</title>\n",theNewTour.tourName];
[bodyString appendString:@"</entry>\n"];
[bodyString appendFormat:@"--%@\n",boundary];
[bodyString appendString:@"Content-Type: application/kml\n"];
NSString *kmlString=[KMLExporter kmlForPoints:self.theNewTour.locations andRoutes:polylines];
[bodyString appendFormat:@"%@\n",kmlString];
[bodyString appendFormat:@"--%@--",boundary];
NSData *postData = [bodyString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
NSString *postLength=[NSString stringWithFormat:@"%d",[postData length]];    
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  
[request setURL:[NSURL URLWithString:@"https://sites.google.com/feeds/content/itourlocal/kmlfiles"]];  
[request setHTTPMethod:@"POST"];  
[request addValue:@"multipart/related; boundary=\"END_OF_PART\"" forHTTPHeaderField: @"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];  
[request setValue:@"sites.google.com" forHTTPHeaderField:@"Host"];
[request setValue:@"1.4" forHTTPHeaderField:@"GData-Version"];
[request setValue:[NSString stringWithFormat:@"GoogleLogin auth=%@",authString] forHTTPHeaderField:@"Authorization"];
NSLog(@"%@",authString);
[request setHTTPBody:postData];  
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request];

这是正文字符串在转换为数据之前的整个文本-

--END_OF_PART
Content-Type: application/atom+xml
<entry xmlns="http://www.w3.org/2005/Atom">
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/sites/2008#attachment" label="attachment"/>
<link rel="http://schemas.google.com/sites/2008#parent" type="application/atom+xml"
href="https://sites.google.com/feeds/content/itourlocal/kmlfiles"/>
<title>bar test.kml</title>
</entry>
--END_OF_PART
Content-Type: application/kml
<kml> <Document> <Placemark> <name>Mezzanine</name> <address>444 Jessie Street, San Francisco</address> <description>(null)</description> <Point> <coordinates>-122.408028,37.782402,0</coordinates> </Point> </Placemark><Placemark> <name>1015 Folsom</name> <address>1015 Folsom Street, San Francisco</address> <description>(null)</description> <Point> <coordinates>-122.405678,37.778072,0</coordinates> </Point> </Placemark><Style id='roadStyle'> <LineStyle> <color>7fcf0064</color> <width>6</width> </LineStyle></Style><Placemark>  <styleURL>#roadStyle</styleURL>  <LineString> <coordinates>-122.407997,37.782379 -122.409248,37.781399 -122.409248,37.781399 -122.408707,37.780979 -122.408157,37.780540 -122.407700,37.780170 -122.407158,37.779739 -122.406631,37.779308 -122.406158,37.778938 -122.405617,37.778500 -122.405617,37.778500 -122.406036,37.778172 -122.406036,37.778172 -122.405800,37.777981 </coordinates> </LineString> </Placemark></Document> </kml>
--END_OF_PART--

此请求返回“Multipart 必须有 Atom 和媒体部分”

我尝试了许多不同的变体,但不断出错。有什么建议可以解决这个问题吗?谢谢。

【问题讨论】:

    标签: iphone http post nsurlconnection google-api


    【解决方案1】:

    想通了- 我需要在内容类型行之后添加第二个换行符。不知道为什么,但它现在可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多