hutool的httpUtil的使用(访问第三方接口)

该代码示例展示了如何使用Java进行HTTPPOST请求,包括以表单和JSON形式传递参数,以及对接口进行签名认证。同时,提供了成功和失败的文件下载案例,涉及到时间戳、签名生成以及文件保存。

对接第三方接口获取数据

以下仅为自己项目中所写并能够跑通 有问题留言 如若不对 请指出告知一下

//传参形式:map  基本类型 string
public static String creatPost(String url,Map param,String contentType) throws Exception{
		//时间戳
		long timeSpan = System.currentTimeMillis()/1000;
		System.out.println("时间戳="+timeSpan);
		//组装TOKEN
		String signature  = SecureUtil.md5(APPTOKEN + APPSECRET + timeSpan).toLowerCase();
		System.out.println("组装signature="+signature);
		System.out.println("组装signature的长度="+signature.length());
		
		//发起请求
		HttpResponse responsse = HttpUtil.createRequest(Method.POST, url)
		.header("x-qys-accesstoken",APPTOKEN)
		.header("x-qys-timestamp", timeSpan+"")
		.header("x-qys-signature", signature)
		.header("Content-Type",contentType)
		.form(param) --以表单形式的参数拼接  
		.execute();
		System.out.println("--------");
		String result = responsse.body();
		System.out.println("响应结果="+result);
		System.out.println("--------");
		return  "SUCCESS";
	}
	//contentType = "multipart/form-data" 
	//如果是post请求  contentType = "multipart/form-data"  建议使用.form进行传参
	//如果是get请求  建议使用.body进行传参  形式 url+"?"+"param1=value1&param2=value2"
	
//传参形式:json   需要在body(json.toString())  并不支持JSONObejct对象
public static String creatRequestPost(String url, JSONObject json,String contentType ) throws Exception{
		//时间戳
		long timeSpan = System.currentTimeMillis()/1000;
		System.out.println("时间戳="+timeSpan);
		//组装TOKEN
		String signature  = SecureUtil.md5(APPTOKEN + APPSECRET + timeSpan).toLowerCase();
		System.out.println("组装signature="+signature);
		System.out.println("组装signature的长度="+signature.length());
		
		//发起请求   
		HttpResponse responsse = HttpRequest.post(url)
        .header("x-qys-accesstoken",APPTOKEN)
		.header("x-qys-timestamp", timeSpan+"")
		.header("x-qys-signature", signature)
		.header("Content-Type",contentType)
        .body(json.toString())
        .execute();
		//输出响应结果
        System.out.println("--------");
		String result = responsse.body();
		System.out.println("响应结果="+result);
		System.out.println("--------");           
		return "SUCCESS";
	}

对接第三方接口获取文件—成功代码

//下载文件是get请求    此方法是可以成功跑通
public static String DownAndReadFile(String url,String param) throws Exception {
		//时间戳
		long timeSpan = System.currentTimeMillis()/1000;
		System.out.println("时间戳="+timeSpan);
		//组装TOKEN
		String signature  = SecureUtil.md5(APPTOKEN + APPSECRET + timeSpan).toLowerCase();
		System.out.println("组装signature="+signature);
		System.out.println("组装signature的长度="+signature.length());
		//拼接url
		url = url+"?"+param;
	    System.out.println("访问路径为:"+url);
		RestTemplate restTemplate = new RestTemplate();
	    HttpHeaders headers = new HttpHeaders();
	    headers.add("x-qys-accesstoken",APPTOKEN );
	    headers.add("x-qys-timestamp", timeSpan+"");
	    headers.add("x-qys-signature", signature);
	    HttpEntity<String> requestEntity = new HttpEntity<>(headers);
	    ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, byte[].class);
	    byte[] bytes = entity.getBody();
	    System.out.println(bytes);
	    //保存地址
	    String dirPath = "F:\\code";
	    //检查指定目录,用户没有指定目录 抛出异常提示用户
	    if(dirPath==null||dirPath.length()==0)throw new Exception("指定路径目录不能为空");
	    //创建file文件对象
	    File savePath = new File(dirPath);
	    //判断文件目录是否存在,不存在即创建目录
	    if (!savePath.exists()) {
	        savePath.mkdir();
	    }
	    //获取文件名
	    File file = new File(savePath+"//下载测试.pdf");
	    //创建新文件
	    if(file!=null && !file.exists()){
	        file.createNewFile();
	    }
	    //获取文件名
	    OutputStream output = new FileOutputStream(file);
	    output.write(bytes);
        //资源关闭
        output.close();
	    return "SUCCESS";
	}

对接第三方接口获取文件—失败代码 hutool工具类并没有成功 只是下载下来了 但是打不开(需要输入密码)或者出现文件损坏

		//时间戳
		long timeSpan = System.currentTimeMillis()/1000;
		System.out.println("时间戳="+timeSpan);
		//组装TOKEN
		String signature  = SecureUtil.md5(APPTOKEN + APPSECRET + timeSpan).toLowerCase();
		System.out.println("组装signature="+signature);
		System.out.println("组装signature的长度="+signature.length());
	    //保存地址
	    String dirPath = "F:\\code";
	    //检查指定目录,用户没有指定目录 抛出异常提示用户
	    if(dirPath==null||dirPath.length()==0)throw new Exception("指定路径目录不能为空");
	    //创建file文件对象
	    File savePath = new File(dirPath);
	    //判断文件目录是否存在,不存在即创建目录
	    if (!savePath.exists()) {
	        savePath.mkdir();
	    }
	    //获取文件名
	    File file = new File(savePath+"//测试.pdf");
	    //创建新文件
	    if(file!=null && !file.exists()){
	        file.createNewFile();
	    }
	    //输出流
	    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file,false));
	    //发起请求
	    url = url+"?"+param;
	    System.out.println("访问路径为:"+url);
	    URL urlLink = new URL(url);
	    //获取链接
	    HttpURLConnection connection = (HttpURLConnection) urlLink.openConnection();
	    connection.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
	    connection.setRequestProperty("x-qys-accesstoken",APPTOKEN);
		connection.setRequestProperty("x-qys-timestamp", timeSpan+"");
		connection.setRequestProperty("Authorization", AUTHOR);
		connection.setRequestProperty("x-qys-signature", signature);
		connection.connect();
	    //获取输入流,读取文件
	    InputStreamReader in = new InputStreamReader(connection.getInputStream());
	    char[] buffer = new char[1024];
	    int length;
	    //读取文件
	    while((length=in.read(buffer))!= -1){
	        //写出
	        out.write(buffer, 0, length);
	    }
	    out.flush();
	    in.close();
	    out.close();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值