JAVA 调用HTTP接口POST或GET实现方式
来源:互联网 发布:两会期间网络保障方案 编辑:程序博客网 时间:2024/05/19 14:00
package com.yoodb.util; import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.params.HttpMethodParams; public class HttpConnectUtil { private static String DUOSHUO_SHORTNAME = "yoodb";//多说短域名 ****.yoodb.**** private static String DUOSHUO_SECRET = "xxxxxxxxxxxxxxxxx";//多说秘钥 /** * get方式 * @param url * @author www.yoodb.com * @return */ public static String getHttp(String url) { String responseMsg = ""; HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler()); try { httpClient.executeMethod(getMethod); ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = getMethod.getResponseBodyAsStream(); int len = 0; byte[] buf = new byte[1024]; while((len=in.read(buf))!=-1){ out.write(buf, 0, len); } responseMsg = out.toString("UTF-8"); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //释放连接 getMethod.releaseConnection(); } return responseMsg; } /** * post方式 * @param url * @param code * @param type * @author www.yoodb.com * @return */ public static String postHttp(String url,String code,String type) { String responseMsg = ""; HttpClient httpClient = new HttpClient(); httpClient.getParams().setContentCharset("GBK"); PostMethod postMethod = new PostMethod(url); postMethod.addParameter(type, code); postMethod.addParameter("client_id", DUOSHUO_SHORTNAME); postMethod.addParameter("client_secret", DUOSHUO_SECRET); try { httpClient.executeMethod(postMethod); ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = postMethod.getResponseBodyAsStream(); int len = 0; byte[] buf = new byte[1024]; while((len=in.read(buf))!=-1){ out.write(buf, 0, len); } responseMsg = out.toString("UTF-8"); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { postMethod.releaseConnection(); } return responseMsg; }}
0 0
- JAVA 调用HTTP接口POST或GET实现方式
- JAVA 调用HTTP接口POST或GET实现方式
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- get/post方式调用http接口
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求、调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- JAVA发送http get/post请求,调用http接口、方法
- 如何调用http接口获取json数据及GET/POST方式调用http接口
- iOS 基于runtime 解决UIButton重复点击 发送多次请求问题
- MAC下zsh配置
- ReactNative初学笔记1.1flexbox布局
- 求出现字母个数
- HM编码器代码阅读(5)——参考帧的选择
- JAVA 调用HTTP接口POST或GET实现方式
- 七牛使用总结:图片流量优化压缩策略
- 【bash】bash脚本编程之条件判断
- Android中Service类onStartCommand的返回值问题
- Android 开发常见问题总结
- 一条短信骗走你所有财产!骗子是怎么做到的?
- Web-fragment 插件化可移植开发
- java中的protected怎么用?和private的区别?
- MySQL分库分表--全局ID生成方案