连接http接口方法
来源:互联网 发布:informix批量生成数据 编辑:程序博客网 时间:2024/05/17 22:30
/**
* http连接方法
*
* @param commString
* @param address
* @return
*/
public String connectURL(String commString, String address) {
String rec_string = "";
URL url = null;
HttpURLConnection urlConn = null;
try {
url = new URL(address);
// 打开连接
urlConn = (HttpURLConnection) url.openConnection();
// 连接主机的超时时间(单位:毫秒)
urlConn.setConnectTimeout(30000);
// 从主机读取数据的超时时间(单位:毫秒)
urlConn.setReadTimeout(30000);
// 设定请求的方法为"POST",默认是GET
urlConn.setRequestMethod("POST");
// http正文内,因此需要设为true, 默认情况下是false;
urlConn.setDoOutput(true);
// 此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,
OutputStream out = urlConn.getOutputStream();
out.write(commString.getBytes("UTF-8"));
out.flush();
out.close();
// 读取接口的返回的流
BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
rec_string = sb.toString().trim();
rd.close();
} catch (Exception e) {
log.error(e);
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
return rec_string;
}
* http连接方法
*
* @param commString
* @param address
* @return
*/
public String connectURL(String commString, String address) {
String rec_string = "";
URL url = null;
HttpURLConnection urlConn = null;
try {
url = new URL(address);
// 打开连接
urlConn = (HttpURLConnection) url.openConnection();
// 连接主机的超时时间(单位:毫秒)
urlConn.setConnectTimeout(30000);
// 从主机读取数据的超时时间(单位:毫秒)
urlConn.setReadTimeout(30000);
// 设定请求的方法为"POST",默认是GET
urlConn.setRequestMethod("POST");
// http正文内,因此需要设为true, 默认情况下是false;
urlConn.setDoOutput(true);
// 此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,
OutputStream out = urlConn.getOutputStream();
out.write(commString.getBytes("UTF-8"));
out.flush();
out.close();
// 读取接口的返回的流
BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
rec_string = sb.toString().trim();
rd.close();
} catch (Exception e) {
log.error(e);
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
return rec_string;
}
- 连接http接口方法
- Delphi调用Http接口方法
- Html调用分布式http API接口方法
- http接口返回对象的方法
- HTTP请求接口方法(POST/GET)
- 接口 两种调用http接口的方法
- https和http协议接口post请求接口方法
- 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请求,调用http接口与方法
- JAVA发送http get/post请求,调用http接口、方法
- 各种路由器接口与连接方法
- 创建子接口连接外网方法
- 处理http连接超时的方法
- 以前看《perl24》书做的部分笔记(zz)
- Delphi2010强化的反射
- servlet页面跳转
- 《算法导论》 7-3 Stooge排序
- 如何定义一个简单的Concurrent Program
- 连接http接口方法
- ASP.NET中CKEditor+CKFinder的配置方法
- Oracle DBA日常任务——查杀死锁
- Perl二维数组
- sql server 2005中的分区函数用法(partition by 字段)
- 时间格式转换公共类1
- 嵌入C语言的测试驱动开发:为什么要调试?(转)
- oracle 导入导出
- WebKit Layout 数据结构