新浪微博--狀態發布及圖片的上傳

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   width   

#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 = [@"multipart/form-data" stringByAppendingFormat:@";boundary=%@",boundry];        [request setValue:value forHTTPHeaderField:@"Content-Type"];        NSMutableData *bodyData = [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 = [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 = [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];            }        }

效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.