调用接口获取接口返回信息的几种工具方法
2013-07-10 11:18
507 查看
返回值为String类型
/**
*
* @param urlStr 接口地址
* @param paras 请求参数
* @param type 调接口传参类型
* @return
* @throws Exception
*/
public static String invokeFareInterface(String urlStr, String paras, String type) throws Exception{
OutputStream os = null;
StringBuffer buffer = null;
InputStreamReader reader = null;
// GZIPInputStream in = null;
try {
int c = 0;
buffer = new StringBuffer();
if(urlStr!=null && !"".equals(urlStr)){
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(60000); // 15s超时
con.setDoOutput(true);
if(type!=null && "post".equals(type)){
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
}else{}
os = con.getOutputStream();
os.write(paras.getBytes());
os.flush();
os.close();
//in = new GZIPInputStream(con.getInputStream());
int code = con.getResponseCode();
if (code == 200) { // 返回成功
reader = new InputStreamReader(con.getInputStream(), "UTF-8");
while ((c = reader.read()) != -1) {
buffer.append((char) c);
}
} else { // 访问失败
throw new Exception("Error occur when try to visit the url:" +
url.getPath() + " using HttpURLConnection");
}
url = null;
con = null;
}else{
//exceptionlog.warn("Invoke 运价接口url为空");
}
} catch (Exception e) {
//exceptionlog.error(this.getClass()+"invoke 运价接口异常:"+e.getMessage());
// System.out.println(this.getClass()+"invoke 运价接口异常:"+e.getMessage());
throw new Exception("Error occur execute " +
"HttpRemoteProxy.performImpl(), the caused by " + e);
} finally {
if (reader != null) {
reader.close();
}
if (os != null) {
os.close();
}
// if (in != null) {
// in.close();
// }
}
return buffer.toString();
}
使用方法调用时应注意如果为get方法调用,应当将参数也放在urlStr里边,param传一个空字符串即可,否则会有错误,底边几个调用方法与此一样
返回值为String
public String getResult(URL url,String sendMsg,String type){
String myResponse = null;
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if("1".equals(type)){
conn.setRequestMethod("POST");
}else if("2".equals(type)){
conn.setRequestMethod("GET");
}
conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.write(sendMsg.getBytes("UTF-8"));
dos.flush();
dos.close();
os.close();
InputStream is = conn.getInputStream();
DataInputStream dis = new DataInputStream(is);
byte[] receivedBytes = new byte[dis.available()];
dis.read(receivedBytes);
myResponse = new String(receivedBytes);
System.out.println(myResponse);
dis.close();
is.close();
conn.disconnect();
}catch (Exception e) {
e.printStackTrace();
}
return myResponse;
}
返回值为String
public static String changeUrlStr(URL url, String sendMsg) {
StringBuffer buffer = null;
InputStreamReader reader = null;
InputStream in = null;
OutputStreamWriter out = null;
URLConnection conn = null;
try {
int c=0;
conn = url.openConnection();
conn.setDoOutput(true);// 设置输出流
conn.setConnectTimeout(30*1000);
conn.setReadTimeout(30*1000);
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");// 得到输出流
out.write(sendMsg);// 输出post数据
out.flush();
out.close();
conn.connect();
//in = conn.getInputStream();// 得到返回的输入流
buffer = new StringBuffer();
reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
while ((c = reader.read()) != -1) {
buffer.append((char) c);
}
return buffer.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
返回值为java.io.InputStream类型
/**
*url为接口地址 sendMsg为参数
*/
public static InputStream changeUrl(URL url, String sendMsg) {
try {
InputStream in = null;
OutputStreamWriter out = null;
URLConnection conn = null;
conn = url.openConnection();
conn.setDoOutput(true);// 设置输出流
conn.setConnectTimeout(30*1000);
conn.setReadTimeout(30*1000);
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");// 得到输出流
out.write(sendMsg);// 输出post数据
out.flush();
out.close();
conn.connect();
in = c
4000
onn.getInputStream();// 得到返回的输入流
return in;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param urlStr 接口地址
* @param paras 请求参数
* @param type 调接口传参类型
* @return
* @throws Exception
*/
public static String invokeFareInterface(String urlStr, String paras, String type) throws Exception{
OutputStream os = null;
StringBuffer buffer = null;
InputStreamReader reader = null;
// GZIPInputStream in = null;
try {
int c = 0;
buffer = new StringBuffer();
if(urlStr!=null && !"".equals(urlStr)){
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(60000); // 15s超时
con.setDoOutput(true);
if(type!=null && "post".equals(type)){
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
}else{}
os = con.getOutputStream();
os.write(paras.getBytes());
os.flush();
os.close();
//in = new GZIPInputStream(con.getInputStream());
int code = con.getResponseCode();
if (code == 200) { // 返回成功
reader = new InputStreamReader(con.getInputStream(), "UTF-8");
while ((c = reader.read()) != -1) {
buffer.append((char) c);
}
} else { // 访问失败
throw new Exception("Error occur when try to visit the url:" +
url.getPath() + " using HttpURLConnection");
}
url = null;
con = null;
}else{
//exceptionlog.warn("Invoke 运价接口url为空");
}
} catch (Exception e) {
//exceptionlog.error(this.getClass()+"invoke 运价接口异常:"+e.getMessage());
// System.out.println(this.getClass()+"invoke 运价接口异常:"+e.getMessage());
throw new Exception("Error occur execute " +
"HttpRemoteProxy.performImpl(), the caused by " + e);
} finally {
if (reader != null) {
reader.close();
}
if (os != null) {
os.close();
}
// if (in != null) {
// in.close();
// }
}
return buffer.toString();
}
使用方法调用时应注意如果为get方法调用,应当将参数也放在urlStr里边,param传一个空字符串即可,否则会有错误,底边几个调用方法与此一样
返回值为String
public String getResult(URL url,String sendMsg,String type){
String myResponse = null;
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if("1".equals(type)){
conn.setRequestMethod("POST");
}else if("2".equals(type)){
conn.setRequestMethod("GET");
}
conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.write(sendMsg.getBytes("UTF-8"));
dos.flush();
dos.close();
os.close();
InputStream is = conn.getInputStream();
DataInputStream dis = new DataInputStream(is);
byte[] receivedBytes = new byte[dis.available()];
dis.read(receivedBytes);
myResponse = new String(receivedBytes);
System.out.println(myResponse);
dis.close();
is.close();
conn.disconnect();
}catch (Exception e) {
e.printStackTrace();
}
return myResponse;
}
返回值为String
public static String changeUrlStr(URL url, String sendMsg) {
StringBuffer buffer = null;
InputStreamReader reader = null;
InputStream in = null;
OutputStreamWriter out = null;
URLConnection conn = null;
try {
int c=0;
conn = url.openConnection();
conn.setDoOutput(true);// 设置输出流
conn.setConnectTimeout(30*1000);
conn.setReadTimeout(30*1000);
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");// 得到输出流
out.write(sendMsg);// 输出post数据
out.flush();
out.close();
conn.connect();
//in = conn.getInputStream();// 得到返回的输入流
buffer = new StringBuffer();
reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
while ((c = reader.read()) != -1) {
buffer.append((char) c);
}
return buffer.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
返回值为java.io.InputStream类型
/**
*url为接口地址 sendMsg为参数
*/
public static InputStream changeUrl(URL url, String sendMsg) {
try {
InputStream in = null;
OutputStreamWriter out = null;
URLConnection conn = null;
conn = url.openConnection();
conn.setDoOutput(true);// 设置输出流
conn.setConnectTimeout(30*1000);
conn.setReadTimeout(30*1000);
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");// 得到输出流
out.write(sendMsg);// 输出post数据
out.flush();
out.close();
conn.connect();
in = c
4000
onn.getInputStream();// 得到返回的输入流
return in;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
相关文章推荐
- 通过silktest调用c#写的工具获取dll文件信息的方法
- Arcengin调用GP工具“对COM组件的调用返回了错误 HRESULTE_FAIL”解决方法
- ASP.NET MVC2 AJAX调用JsonResult方法并返回自定义错误信息
- 调用webservice带参数值返回字符串接口的方法详解
- log信息获取调用类名和调用方法名的实现原理
- java 调用方法栈获取类名,方法名,方法返回类型
- ASP.NET MVC3 AJAX调用JsonResult方法并返回自定义错误信息
- httpclient封装获取响应实体和接口信息的方法
- java调用webservice接口 几种方法
- VB.NET获取硬盘信息的几种方法
- python 调用海康linux下psdatacall_demo,实现获视频取码流并返回到python,以及上传信息到人脸库的方法
- http post方法调用接口获取json文件内容 以及获取Json字符串某节点的值
- scala调用java的方法,返回了一个对象链表List<Student>,在scala中遍历该链表获取指定Student的名字name
- python调用阿里接口获取ECS资产信息
- httpclient封装获取响应实体和接口信息的方法
- ASP.NET中MVC使用AJAX调用JsonResult方法并返回自定义错误信息
- Thinkphp5微信小程序获取用户信息接口调用笔记
- 调用手机号码归属地接口获取手机号码信息
- python调用阿里接口获取ECS资产信息
- 组件接口方法调用,返回S_FALSE 还是E_FAIL?