接口自动化httpclient+fastjson实践

使用httpclient发送上传视频的请求

代码如下:

package examples;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class VideoUploadTest {
    @Test
    public void f() throws Exception{
        //得到一个客户端(用来发送请求的)
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个Post请求
        HttpPost httpPost = new HttpPost("http://v.polyv.net/uc/services/rest?method=uploadfile");
        //entity表示主体(body)
        //StringEntity entity = null;
        HttpEntity entity = null;      
        //httpResponse表示响应
        HttpResponse httpResponse = null;
        //headers表示消息头(response headers)
        Header[] headers = null;
        try {
            //设置主体的内容
            entity = MultipartEntityBuilder.create().
                    addTextBody("writetoken", "f56c086c-a208-4a21-badf-bf06d70fe356").
                    addTextBody("JSONRPC", "{\"title\": \"yzc\", \"tag\":\"yzc\",\"desc\":\"yzc\"}").
                    addBinaryBody("Filedata", new File("C:\\Users\\yangzc\\Desktop\\FlickAnimation.avi")).build();

            //将entity设置为post请求的主体
            httpPost.setEntity(entity);
            //发送该post请求
            httpResponse = httpClient.execute(httpPost);
            //
            headers = httpResponse.getAllHeaders();
            for(Header h:headers){
                System.out.println(h.toString());
            }
            String responseBody = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(responseBody);
            //将responseBody这个json字符串转成json对象
            JSONObject obj = JSON.parseObject(responseBody);
            //获取json对象data属性的内容(json数组)
            JSONArray data = (JSONArray)obj.get("data");
            //获取data数组里面的第一个json对象
            JSONObject video = data.getJSONObject(0);
            //获取json对象的mp4属性的内容
            System.out.println("================="+video.get("mp4"));            
        } catch (Exception e) {
            e.printStackTrace();
        }
        httpClient.close();
    }
}

控制台输出内容如下:

[RemoteTestNG] detected TestNG version 6.11.0
Server: Tengine/1.4.4
Date: Wed, 01 Aug 2018 09:36:21 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE,OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
x-server-num: 006
{"error":"0","data":[{"images_b":["http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_0_b.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_1_b.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_2_b.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_3_b.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_4_b.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_5_b.jpg"],"md5checksum":"a99f357d1f52eb8437e0572e98ebb012","tag":"yzc","mp4":"http://mpv.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_1.mp4","title":"yzc","df":3,"times":"0","mp4_1":"http://mpv.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_1.mp4","mp4_3":"http://mpv.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_3.mp4","vid":"705e4a62712f1f778f7ed287176ed37f_7","mp4_2":"http://mpv.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_2.mp4","cataid":"1","swf_link":"http://player.polyv.net/videos/705e4a62712f1f778f7ed287176ed37f_7.swf","source_filesize":1600388,"status":"10","seed":0,"flv2":"http://plvod01.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_2.flv","flv3":"http://plvod01.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_3.flv","flv1":"http://plvod01.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_1.flv","sourcefile":"","playerwidth":"600","hls":["http://hls.videocc.net/705e4a6271/7/705e4a62712f1f778f7ed287176ed37f_1.m3u8","http://hls.videocc.net/705e4a6271/7/705e4a62712f1f778f7ed287176ed37f_2.m3u8","http://hls.videocc.net/705e4a6271/7/705e4a62712f1f778f7ed287176ed37f_3.m3u8"],"default_video":"http://plvod01.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_2.flv","duration":"00:00:03","filesize":[0,0,0],"first_image":"http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_0.jpg","original_definition":"300x250","context":"yzc","images":["http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_0.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_1.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_2.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_3.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_4.jpg","http://img.videocc.net/uimage/7/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_5.jpg"],"previewVid":"9408c7l32412n1n445n4ck254143ck64n_4","playerheight":"499","ptime":"2018-08-01 17:36:21"}]}
=================http://mpv.videocc.net/705e4a6271/f/705e4a62712f1f778f7ed287176ed37f_1.mp4
PASSED: f

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

使用httpclient发送查找视频的请求

代码如下:

package day01;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;



public class VideoQueryTest {
    public static void main(String[] args)throws Exception
    {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String userId = "705e4a6271";
        String tag = "测试";
        String secretkey = "g6UuoJKst3";
        StringBuilder url = new StringBuilder();
        url.append("http://api.polyv.net/v2/video/");
        url.append(userId);
        url.append("/search?");
        long time = System.currentTimeMillis();
        url.append("ptime="+time);
        url.append("&tag="+tag);
        String str = "ptime="+time+"&tag="+tag+secretkey;
        String sign = DigestUtils.sha1Hex(str).toUpperCase();
        //System.out.println(sign);
        url.append("&sign="+sign);
        //System.out.println(url);
        HttpGet httpGet = new HttpGet(url.toString());
        HttpResponse httpResponse = null;
        Header[] headers = null;
        try {
            //发送该请求
            httpResponse = httpClient.execute(httpGet);
            headers = httpResponse.getAllHeaders();
            for(Header h:headers){
                System.out.println(h.toString());
            }
            System.out.println(EntityUtils.toString(httpResponse.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        httpClient.close();
    }
}

控制台打印内容如下:

Server: Tengine
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Sun, 29 Jul 2018 16:10:30 GMT
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Via: cache17.l2st4-2[18,0], cache2.cn497[49,0]
Timing-Allow-Origin: *
EagleId: b4a39f4215328806306934473e
{"code":200,"status":"success","message":"success","data":[{"tag":"测试","mp4":"http://mpv.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_1.mp4","title":"我的视频","df":3,"times":"1","vid":"705e4a627167304c20497ed1f7c91672_7","mp4_1":"http://mpv.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_1.mp4","mp4_2":"http://mpv.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_2.mp4","mp4_3":"http://mpv.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_3.mp4","cataid":"1","swf_link":"http://player.polyv.net/videos/705e4a627167304c20497ed1f7c91672_7.swf","status":"61","seed":0,"playerwidth":"300","duration":"00:00:03","first_image":"http://img.videocc.net/uimage/7/705e4a6271/2/705e4a627167304c20497ed1f7c91672_0.jpg","original_definition":"300x250","context":"我的视频","playerheight":"249","ptime":"2018-07-29 14:41:48","source_filesize":1600388,"filesize":[86344,192266,581892],"md5checksum":"a99f357d1f52eb8437e0572e98ebb012","hls":["http://hls.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_1.m3u8","http://hls.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_2.m3u8","http://hls.videocc.net/705e4a6271/2/705e4a627167304c20497ed1f7c91672_3.m3u8"]}],"total":1}

Process finished with exit code 0

附截图


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,451评论 25 707
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_x阅读 15,967评论 3 119
  • 范佳老师阅读 379评论 0 0
  • 昨晚女儿和她大伯做游戏,哭了,逐层询问,是大伯不相信她,我问她:你相信自己吗?她说相信,瞬间止住了哭,看到她的小脑...
    sarah4958阅读 327评论 0 3
  • 正在喝水的齐北辰忽然觉得气氛不对,转眼一看,见一堆人都朝他这边看过来。好看的眉头蹙了一下,继续喝他的水。 ...
    北北齐阅读 150评论 0 0