iphone - 如何停止从 iPhone 向服务器发送 HTTP 消息

标签 iphone ios http file-upload

我正在使用 HTTP 发布将视频发布到服务器。

如何在发布时停止此过程。 我正在获取写入的字节,如果我想通过单击按钮停止发布,我该如何实现

查了很多,还是没搞清楚

我正在上传类中添加我的代码

 @implementation UploadClass


 -(void)uploadVideoToServer:(NSDictionary *)bits file:(NSData *)file {

appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;



 NSString *urlString =@"http://sampleurl.com/upload_video";
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];



  NSString *boundary = @"---------------------------14737809831466499882746641449";
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
 [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
 NSMutableData *postbody = [NSMutableData data];
 [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


//posting userid 

 [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%@", appDelegate.userid] dataUsingEncoding:NSUTF8StringEncoding]];
 [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//帖子标题

[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video_title\"\r\n\r\n%@", sharedclass.vdoTitle] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];



//posting video data

[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video\"; filename=\"%@\"\r\n", @"a.mov"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:file]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[request setHTTPBody:postbody];


conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
    webData = [[NSMutableData data] retain];
}    


}




 -(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)  response 

    {
    [webData setLength: 0];

     }


-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 

   {
[webData appendData:data];

    }




 -(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
    {
[webData release];
[conn release];
     }

  -(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

   {

    appDelegate.isVideoUploading=@"YES";
     NSDictionary *uploadStatus=[[NSDictionary alloc]initWithObjectsAndKeys:
                            [NSString stringWithFormat:@"%i",totalBytesWritten],@"bytesWritten",
                            [NSString stringWithFormat:@"%i",totalBytesExpectedToWrite],@"totalBytes",
                            nil];



 if (totalBytesWritten/totalBytesExpectedToWrite == 1) {

  }
   [uploadStatus release];
  }



 -(void) connectionDidFinishLoading:(NSURLConnection *) connection 
    {

      NSString *jsonString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding] ;

NSString *theXML = [[NSString alloc]
                    initWithBytes: [webData mutableBytes] 
                    length:[webData length] 
                    encoding:NSUTF8StringEncoding];
//---shows the XML---

[theXML release];   
if (xmlParser)
   {
     [xmlParser release];
   }    
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];

    [conn release];
[webData release];
   }

  -(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict 
    {
if( [elementName isEqualToString:@"status"])
    {
    if (!postStatus)
    {
        postStatus = [[NSMutableString alloc] init];
    }
    postStatusFound = YES;
   }
   }

  -(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
  {
    if (postStatusFound)
  {
 postStatusFound=NO;
    [postStatus appendString: string];
   }    
  }


 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
 {

  sharedclass = [SharedClass sharedInstance];
   if ([elementName isEqualToString:@"status"])
{   
  if([postStatus isEqualToString:@"true" ]){
        appDelegate.isVideoUploading=@"NO";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOADFINISHED"  object:@"true"];
        NSLog(@"upload finished;");
        sharedclass.cameraBtn=YES;

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    }else{
        appDelegate.isVideoUploading=@"NO";
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOADFINISHED"  object:@"false"];

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];


    }

      }
   }

最佳答案

NSURLConnection 声明为类属性,您可以在类中的任何位置或单击按钮时使用取消操作

-(IBAction)cancelPostOperation:(id)sender{
    [self.conn cancel];
}

关于iphone - 如何停止从 iPhone 向服务器发送 HTTP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15427951/

相关文章:

iphone - MKMapView 拦截/劫持 iPhone 触摸事件

iphone - 如何覆盖 UISearchBar 中的取消按钮

ios - Facebook iOS SDK 4.0.1 "You must upgrade this app to Graph API v2.x"

ios - 如何跳过传入 [NSString stringWithFormat :] 的数据

ios - 使用构造函数来初始化实例变量子类 PFObject

node.js - 为什么多部分不生成关闭事件

iphone - 我必须延长哪一个类?

java - 正确的 HTTP 客户端方法

javascript - 具有 Access-Control-Allow-Origin : * 的服务器

iphone - 阵列清理方法错误