使用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
附截图