twitpic API 未连接(多部分/表单数据)objective-c

发布于 2024-08-12 05:37:27 字数 3468 浏览 2 评论 0原文

我根据大量研究整理了这段代码。 有关 twitpic API 的信息如下:

http://twitpic.com/api.do#uploadAndPost

通过调试我可以看出字典很好,它通过所有方法,请求被提交,但随后 NSLog 方法返回 <>。

我不确定哪里可能出错,我对多部分/表单数据结构了解不多。也许我的连接做错了什么?

代码如下。

 -(void)uploadBoth {  
      //Create dictionary of post arguments
      NSArray *keys = [[NSArray alloc] initWithObjects:@"media",@"username",@"password",@"message",nil];
      NSArray *objects = [[NSArray alloc] initWithObjects:
           imageData, twitterUserSave, twitterPassSave, [NSString stringWithFormat:@"%@",messageView.text], nil];

      NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
     NSLog(@"%@", keysDict);

      //create twitpic photo post
      NSURLRequest *twitpicPost = [self twitpicRequest:keysDict withData:imageData];

      //send request, return YES if successful
      NSURLConnection *twitpicConnection = [[NSURLConnection alloc] initWithRequest: twitpicPost delegate:self];

      if (!twitpicConnection) {
       NSLog(@"Failed to submit request");
      } else {
       NSLog(@"Request submitted");
       NSData *receivedData = [[NSMutableData data] retain];
       NSLog(@"%@", receivedData); // THIS PART RETURNS <>
      }
    }

NSURLRequest 部分

-(NSURLRequest *)twitpicRequest:(NSDictionary *)postKeys withData:(NSData *)data {

 NSLog(@"got this far");

  //create the URL POST Request to twitpic
  NSURL *twitpicURL = [NSURL URLWithString:@"http://twitpic.com/api/uploadAndPost"];
  NSMutableURLRequest *twitpicPost = [NSMutableURLRequest requestWithURL:twitpicURL];
  [twitpicPost setHTTPMethod:@"POST"];

  //Add the header info
  NSString *stringBoundary = [NSString stringWithString:@"0xAbCdEfGbOuNdArY"];
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
  [twitpicPost addValue:contentType forHTTPHeaderField: @"Content-Type"];

  //create the body
  NSMutableData *postBody = [NSMutableData data];
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

  //add key values from the NSDictionary object
  NSEnumerator *keys = [postKeys keyEnumerator];
  int i;
  for (i = 0; i < [postKeys count]; i++) {
   NSString *tempKey = [keys nextObject];
   [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  }

  //add data field and file data
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[NSData dataWithData:data]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

  //add the body to the post
  [twitpicPost setHTTPBody:postBody];

 return twitpicPost;
}

I've put together this code from lots of research.
Information on the twitpic API is here:

http://twitpic.com/api.do#uploadAndPost

Through debugging i can tell that the dictionary is good, and that it gets through all the methods, the request gets submitted, but then the NSLog method returns <>.

I'm not sure where I could be going wrong, i don't know much about the multipart/form-data structure. Maybe I'm doing something wrong with my connection?

Code below.

 -(void)uploadBoth {  
      //Create dictionary of post arguments
      NSArray *keys = [[NSArray alloc] initWithObjects:@"media",@"username",@"password",@"message",nil];
      NSArray *objects = [[NSArray alloc] initWithObjects:
           imageData, twitterUserSave, twitterPassSave, [NSString stringWithFormat:@"%@",messageView.text], nil];

      NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
     NSLog(@"%@", keysDict);

      //create twitpic photo post
      NSURLRequest *twitpicPost = [self twitpicRequest:keysDict withData:imageData];

      //send request, return YES if successful
      NSURLConnection *twitpicConnection = [[NSURLConnection alloc] initWithRequest: twitpicPost delegate:self];

      if (!twitpicConnection) {
       NSLog(@"Failed to submit request");
      } else {
       NSLog(@"Request submitted");
       NSData *receivedData = [[NSMutableData data] retain];
       NSLog(@"%@", receivedData); // THIS PART RETURNS <>
      }
    }

The NSURLRequest part

-(NSURLRequest *)twitpicRequest:(NSDictionary *)postKeys withData:(NSData *)data {

 NSLog(@"got this far");

  //create the URL POST Request to twitpic
  NSURL *twitpicURL = [NSURL URLWithString:@"http://twitpic.com/api/uploadAndPost"];
  NSMutableURLRequest *twitpicPost = [NSMutableURLRequest requestWithURL:twitpicURL];
  [twitpicPost setHTTPMethod:@"POST"];

  //Add the header info
  NSString *stringBoundary = [NSString stringWithString:@"0xAbCdEfGbOuNdArY"];
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
  [twitpicPost addValue:contentType forHTTPHeaderField: @"Content-Type"];

  //create the body
  NSMutableData *postBody = [NSMutableData data];
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

  //add key values from the NSDictionary object
  NSEnumerator *keys = [postKeys keyEnumerator];
  int i;
  for (i = 0; i < [postKeys count]; i++) {
   NSString *tempKey = [keys nextObject];
   [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  }

  //add data field and file data
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[NSData dataWithData:data]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

  //add the body to the post
  [twitpicPost setHTTPBody:postBody];

 return twitpicPost;
}

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

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

发布评论

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

评论(1

假面具 2024-08-19 05:37:27

鉴于您打印数据而无需等待下载,您的输出是有意义的:

NSData *receivedData = [[NSMutableData data] retain]; // this is a new empty data object
NSLog(@"%@", receivedData); // THIS PART RETURNS <>

您应该在实现文件顶部将“receivedData”声明为“NSMutableData”引用(或作为类标头中的实例变量)。然后,您应该实现 使用 NSURLConnection

否则,您可能需要首先尝试让同步请求正常工作:

NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:twitpicPost returningResponse:&response error:&error];
NSLog(@"%@",[[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);

Your output makes sense given that you print the data without waiting for it to download:

NSData *receivedData = [[NSMutableData data] retain]; // this is a new empty data object
NSLog(@"%@", receivedData); // THIS PART RETURNS <>

You should declare "receivedData" as an "NSMutableData" reference at the top of the implementation file (or as an instance variable in the class header). Then, you should implement the delegate methods as described in listings 2-5 of the Using NSURLConnection.

Otherwise, you may want to start by trying to get a synchronous request to work:

NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:twitpicPost returningResponse:&response error:&error];
NSLog(@"%@",[[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文