首页 > 代码库 > 新浪微博--状态发布及图片的上传

新浪微博--状态发布及图片的上传

#pragma mark 发微博界面:sendStatus-(void)sendStatus{        if ([self.textView.text isEqualToString:@""]) {        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您没输入任何东西" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [alter show];    }else if([self textLength:self.textView.text] > 140)    {        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入文字大于140" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [alter show];    }           if (self.imageView) {                // HTTP协议之multipart/form-data请求分析        NSString *status = self.textView.text ;        NSData *dataImg = UIImageJPEGRepresentation(self.imageView.image, 0.5);        NSString *accesstoken = [AccountTool shareAccountTool].currentAcount.accessToken;                NSString *urlStr = @"https://upload.api.weibo.com/2/statuses/upload.json";        NSURL *url = [NSURL URLWithString:urlStr];        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];        [request setHTTPMethod:@"POST"];        //Content-Disposition:form-data;access_token=%@;status=%@;pic=%@                NSString *nextRow = @"\r\n";        NSString *boundry = [NSString stringWithFormat:@"------%@",@((int)[[NSDate date] timeIntervalSince1970]).description];        NSString *start = [@"--" stringByAppendingString:boundry];        NSString *end = [@"--" stringByAppendingFormat:@"%@--",boundry];        NSString *acctokenType = @"Content-Disposition: form-data; name=\"access_token\"";                NSString *statusType = @"Content-Disposition: form-data; name=\"status\"";        NSString *imageType = @"Content-Disposition: form-data; name=\"pic\"; filename=\"shell.png\"";        NSString *value = http://www.mamicode.com/[@"multipart/form-data" stringByAppendingFormat:@";boundary=%@",boundry];        [request setValue:value forHTTPHeaderField:@"Content-Type"];        NSMutableData *bodyData =http://www.mamicode.com/ [NSMutableData data];                // accessToken        [bodyData appendData:[start dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[acctokenType dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[accesstoken dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];                // status        [bodyData appendData:[start dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[statusType dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[status dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];                // pic        [bodyData appendData:[start dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[imageType dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[@"Content-Type:image/png" dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];        [bodyData appendData:dataImg];        [bodyData appendData:[nextRow dataUsingEncoding:NSUTF8StringEncoding]];                [bodyData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];//        //        NSString *bodyString = [NSString stringWithFormat:@"Content-Disposition:form-data;access_token=%@;status=%@;pic=%@",[AccountTool shareAccountTool].currentAcount.accessToken,status,dataImg];//        NSData *data = http://www.mamicode.com/[bodyString dataUsingEncoding:NSUTF8StringEncoding];        [request setHTTPBody:bodyData];                AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {            NSLog(@"发送成功 = %@",JSON);            //        MBProgressHUD *hud = [MBProgressHUD ]                        // 发送成功 取消发送页            [self dismissViewControllerAnimated:YES completion:nil];                    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {            NSLog(@"error  == %@",error);        }];        [op start];                    }else{#warning 发送为微博为"POST"请求        NSString *status = self.textView.text ;        NSString *urlStr = @"https://api.weibo.com/2/statuses/update.json";        NSURL *url = [NSURL URLWithString:urlStr];        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];        [request setHTTPMethod:@"POST"];        NSString *bodyString = [NSString stringWithFormat:@"%@=%@&status=%@",kAccessToken,[AccountTool shareAccountTool].currentAcount.accessToken,status];        NSData *data =http://www.mamicode.com/ [bodyString dataUsingEncoding:NSUTF8StringEncoding];        [request setHTTPBody:data];                AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {//            NSLog(@"发送成功 = %@",JSON);                        // 发送成功 取消发送页            [self dismissViewControllerAnimated:YES completion:nil];                    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {            NSLog(@"error  == %@",error);        }];        [op start];            }        }

效果如图: