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

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

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

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

	/**
	 * this method is 授权
	 * @return
	 * @createTime 2015年1月5日 下午4:26:09
	 * @author yangwd
	 */
	public String accesstoken(){
		String url = "https://openapi.iqiyi.com/api/iqiyi/authorize?client_id="+appKey+"&client_secret="+appSecret;
		HttpRequest request = HttpRequest.get(url);
		//针对单项证书给予忽略(注意,双向证书需要导入证书文件)
		request.trustAllCerts();
		//信任所有地址
		request.trustAllHosts();
		String response = request.body();
		JSONObject jo = JSON.parseObject(response);
		Object code = jo.get("code");
		Object accessToken = jo.getJSONObject("data").get("access_token");
		System.out.println("Response was: " + response);
		System.out.println("code was: " + code);
		System.out.println("accessToken was: " + accessToken);
		return accessToken.toString();
	}
	
	/**
	 * this method is 获取视频信息
	 * @param accessToken
	 * @param dbVideo
	 * @return
	 * @createTime 2015年1月5日 下午4:26:01
	 * @author yangwd
	 */
	public List<Video> getVideoInfo(String accessToken,List<Video> dbVideoList){
		//单个检查视频文件状态
		List<Video> listVideo = new ArrayList<Video>();
		StringBuffer fileIds = new StringBuffer();
		HttpRequest requestCheckState =null;
		Integer pageSize = 0;
		for(Video vo : dbVideoList){
			String checkStateUrl = "http://openapi.iqiyi.com/api/file/fullStatus?access_token="+accessToken+"&file_id="+vo.getFileId();
			requestCheckState = HttpRequest.get(checkStateUrl);
			String response = requestCheckState.body();
			JSONObject jResult = JSON.parseObject(response);
			String codeState = jResult.getString("code");
			if("A00000".equals(codeState)){  //视频处理完成
				fileIds.append(vo.getFileId()+",");
				pageSize++;
			}else if("A00002".equals(codeState)){ //视频审核失败
				vo.setStatus(Video.STATUS_AUDIT_REFUSE);
				listVideo.add(vo);
			}else if(!"A00001".equals(codeState)){ //A00001 视频发布中
				logger.error("定时器向爱奇艺获取视频状态时,fileId="+vo.getFileId()+"的视频状态返回码"+codeState);
			}
		}
		if(pageSize == 0){
			return listVideo;
		}
		//获取视频信息
		String videoInfoUrl = "http://openapi.iqiyi.com/api/file/videoListForExternal?access_token="+accessToken
							  +"&file_ids="+fileIds+"&page_size="+pageSize+"&page=1";
		HttpRequest requestVideoInfo = HttpRequest.get(videoInfoUrl);
		String response = requestVideoInfo.body();
		JSONObject jResult = JSON.parseObject(response);
		JSONArray array = jResult.getJSONArray("data");
		JSONObject jo = null;
		Video video = null;
		for(int i=0;i<array.size();i++){
			jo = array.getJSONObject(i);
			video = new Video();
			video.setFileId(jo.getString("fileId"));
			video.setPageUrl(jo.getString("pageUrl"));
			video.setStatus(jo.getInteger("fileStatus"));
			video.setImg(jo.getString("img"));
			listVideo.add(video);
		}
		return listVideo;
	}


你可能感兴趣的:(java程序访问http,或https最简单的方法)