当前位置:   article > 正文

java程序访问http,或https最简单的方法_httpijctujvcv

httpijctujvcv

首先要引入包http-request-5.6.jar 关于jar包,我已经是上传了哈。

注意:jar包调用详情说明地址:https://github.com/kevinsawicki/http-request/

下面代码其实就几句就行了,其他业务代码不必理会。

  1. /**
  2. * this method is 授权
  3. * @return
  4. * @createTime 2015年1月5日 下午4:26:09
  5. * @author yangwd
  6. */
  7. public String accesstoken(){
  8. String url = "https://openapi.iqiyi.com/api/iqiyi/authorize?client_id="+appKey+"&client_secret="+appSecret;
  9. HttpRequest request = HttpRequest.get(url);
  10. //针对单项证书给予忽略(注意,双向证书需要导入证书文件)
  11. request.trustAllCerts();
  12. //信任所有地址
  13. request.trustAllHosts();
  14. String response = request.body();
  15. JSONObject jo = JSON.parseObject(response);
  16. Object code = jo.get("code");
  17. Object accessToken = jo.getJSONObject("data").get("access_token");
  18. System.out.println("Response was: " + response);
  19. System.out.println("code was: " + code);
  20. System.out.println("accessToken was: " + accessToken);
  21. return accessToken.toString();
  22. }
  23. /**
  24. * this method is 获取视频信息
  25. * @param accessToken
  26. * @param dbVideo
  27. * @return
  28. * @createTime 2015年1月5日 下午4:26:01
  29. * @author yangwd
  30. */
  31. public List<Video> getVideoInfo(String accessToken,List<Video> dbVideoList){
  32. //单个检查视频文件状态
  33. List<Video> listVideo = new ArrayList<Video>();
  34. StringBuffer fileIds = new StringBuffer();
  35. HttpRequest requestCheckState =null;
  36. Integer pageSize = 0;
  37. for(Video vo : dbVideoList){
  38. String checkStateUrl = "http://openapi.iqiyi.com/api/file/fullStatus?access_token="+accessToken+"&file_id="+vo.getFileId();
  39. requestCheckState = HttpRequest.get(checkStateUrl);
  40. String response = requestCheckState.body();
  41. JSONObject jResult = JSON.parseObject(response);
  42. String codeState = jResult.getString("code");
  43. if("A00000".equals(codeState)){ //视频处理完成
  44. fileIds.append(vo.getFileId()+",");
  45. pageSize++;
  46. }else if("A00002".equals(codeState)){ //视频审核失败
  47. vo.setStatus(Video.STATUS_AUDIT_REFUSE);
  48. listVideo.add(vo);
  49. }else if(!"A00001".equals(codeState)){ //A00001 视频发布中
  50. logger.error("定时器向爱奇艺获取视频状态时,fileId="+vo.getFileId()+"的视频状态返回码"+codeState);
  51. }
  52. }
  53. if(pageSize == 0){
  54. return listVideo;
  55. }
  56. //获取视频信息
  57. String videoInfoUrl = "http://openapi.iqiyi.com/api/file/videoListForExternal?access_token="+accessToken
  58. +"&file_ids="+fileIds+"&page_size="+pageSize+"&page=1";
  59. HttpRequest requestVideoInfo = HttpRequest.get(videoInfoUrl);
  60. String response = requestVideoInfo.body();
  61. JSONObject jResult = JSON.parseObject(response);
  62. JSONArray array = jResult.getJSONArray("data");
  63. JSONObject jo = null;
  64. Video video = null;
  65. for(int i=0;i<array.size();i++){
  66. jo = array.getJSONObject(i);
  67. video = new Video();
  68. video.setFileId(jo.getString("fileId"));
  69. video.setPageUrl(jo.getString("pageUrl"));
  70. video.setStatus(jo.getInteger("fileStatus"));
  71. video.setImg(jo.getString("img"));
  72. listVideo.add(video);
  73. }
  74. return listVideo;
  75. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/158538
推荐阅读
相关标签
  

闽ICP备14008679号