【问题标题】:how to create a request with ASIHttpRequest like the following?如何使用 ASIHttpRequest 创建请求,如下所示?
【发布时间】:2012-10-16 09:27:17
【问题描述】:

我在 YouTube Resumable Uploads 上醒来: https://developers.google.com/youtube/2.0/developers_guide_protocol_resumable_uploads#Sending_a_Resumable_Upload_API_Request

我未能使用 ASIHttpRequest 创建请求,如下所示。 如何让它发挥作用?谢谢!任何帮助将不胜感激!

(1)

POST /resumable/feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Content-Length: 0
Slug: my_file.mp4

(2)

POST /resumable/feeds/api/users/default/uploads HTTP/1.1

Host: uploads.gdata.youtube.com

Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Content-Length: 1941255
Slug: my_file.mp4
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <media:group>
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    </media:description>
    <media:category
      scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
    </media:category>
    <media:keywords>toast, wedding</media:keywords>
  </media:group>
</entry>

这是我的(2)代码,请求完成没有错误,但responseData为空。我不知道为什么!

    NSURL *uploadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"]];
    ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:uploadUrl];

    NSString *xmlStr = [NSString stringWithFormat:@"<?xml version=\"1.0\"?>\n"
                        @"<entry xmlns=\"http://www.w3.org/2005/Atom\"\n"
                        @"xmlns:media=\"http://search.yahoo.com/mrss/\"\n"
                        @"xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">\n"
                        @"<media:group>\n"

                        //Title
                        @"<media:title type=\"plain\">%@</media:title>\n"
                        //Description
                        @"<media:description type=\"plain\">\n"
                        @"%@\n"
                        @"</media:description>\n"
                        //Category
                        @"<media:category\n"
                        @"scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">Entertainment\n"
                        @"</media:category>\n"
                        //Keywords
                        @"<media:keywords>Camera,PowerCam</media:keywords>\n"

                        @"</media:group>\n"
                        @"</entry>\n",        @"PowerCam.mov",@"description" ];


    [theRequest setTimeOutSeconds:60];
    [theRequest addRequestHeader:@"Authorization"   value:[NSString stringWithFormat:@"Bearer %@",_oauth.accessToken]];
    [theRequest addRequestHeader:@"GData-Version"   value:@"2"];
    [theRequest addRequestHeader:@"X-GData-Key"     value:[NSString stringWithFormat:@"key=%@", YouTuDevkey]];
    [theRequest addRequestHeader:@"Content-Length"  value:[NSString stringWithFormat:@"%d",xmlStr.length]];
    [theRequest addRequestHeader:@"Slug"            value:@"PowerCam.mov"];  
    [theRequest addRequestHeader:@"Content-Type"    value:@"application/atom+xml; charset=UTF-8"];

    [theRequest appendPostData:[xmlStr dataUsingEncoding:NSUTF8StringEncoding]];
    [theRequest setUploadProgressDelegate:self];
    theRequest.delegate = self;
    theRequest.tag = eWSKYouTubeSharerUploadingVideoMetadata;
    self.requestTag = eWSKYouTubeSharerUploadingVideoMetadata;
    self.request = theRequest;
    [[ASIManager manager] addRequest:theRequest];
    [[ASIManager manager] startQueue];

【问题讨论】:

  • 它到底是怎么工作的?您可以发布您的ASIHTTPRequest 代码吗?
  • 问题解决了!代码工作正常。 API 响应在标头中,因此 responseData 为空:)
  • 干得好 :-) 别忘了在下面发表你的答案,然后你就可以接受了。

标签: iphone ios networking youtube asihttprequest


【解决方案1】:

有关如何使用它的信息,请参阅the ASIHTTPRequest how-to。大致上,你会有这样的东西:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"X-GData-Key" value:@"adf15ee97731bca89da876c...a8dc9"];
// add the other special headers as well
NSString *xmlBody = // create the XML data in the body
[request appendPostData:[xmlBody dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];

假设您想从后台线程执行同步请求。

【讨论】:

    【解决方案2】:

    以下示例显示了对上传视频元数据以进行可恢复上传的请求的示例 API 响应:

    HTTP/1.1 200 OK
    Location: http://uploads.gdata.youtube.com/resumableupload/AF8GKF...0Glpk0Aw
    Date: Fri, 04 Dec 2009 16:14:30 GMT
    Pragma: no-cache
    Expires: Fri, 01 Jan 1990 00:00:00 GMT
    Cache-Control: no-cache, no-store, must-revalidate
    Content-Length: 0
    Content-Type: text/html
    

    代码运行良好。 “Location” url 在标题中,而不是在正文中,因此 responseData 为空:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      相关资源
      最近更新 更多