1.restful接口被调用
首先要设置拦截,否则无法直接访问
接口测试方法
package test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author 包福平
* @QQ:1140913970
*/
public class ToInterface {
/**
* 调用对方接口方法
* @param path 对方或第三方提供的路径
* @param data 向对方或第三方发送的数据,大多数情况下给对方发送JSON数据让对方解析
*/
public static void interfaceUtil(String path,String data) {
try {
URL url = new URL(path);
//打开和url之间的连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
//请求方式
// conn.setRequestMethod("POST");
// //设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
//设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
//最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,
//post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
conn.setDoOutput(true);
conn.setDoInput(true);
//获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求参数即数据
out.print(data);
//缓冲数据
out.flush();
//获取URLConnection对象对应的输入流
InputStream is = conn.getInputStream();
//构造一个字符流缓存
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
//关闭流
is.close();
//断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
//固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。
conn.disconnect();
System.out.println("完整结束");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
interfaceUtil("http://localhost:8080/jsc/toInterfaceController.do?getDemo&body=2", "");
// interfaceUtil("http://192.168.10.89:8080/eoffice-restful/resources/sys/oadata", "usercode=10012");
// interfaceUtil("http://192.168.10.89:8080/eoffice-restful/resources/sys/oaholiday",
// "floor=first&year=2017&month=9&isLeader=N");
}
}
2.带签名密钥的接口
package test;
import com.alibaba.fastjson.JSONObject;
import org.jeecgframework.web.cgform.util.SignatureUtil;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class HttpUtil {
/**
* 发起https请求并获取结果
*
* @param requestUrl
* 请求地址
* @param requestMethod
* 请求方式(GET、POST)
* @param outputStr
* 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpRequest(String requestUrl,
String requestMethod, String outputStr,String sign) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpUrlConn = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestProperty("X-JEECG-SIGN",sign);
// httpUrlConn.setRequestProperty("content-type", "text/html");
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
// jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
org.jeecgframework.core.util.LogUtil
.info("Weixin server connection timed out.");
} catch (Exception e) {
//e.printStackTrace();
org.jeecgframework.core.util.LogUtil.info("https request error:{}"
+ e.getMessage());
}finally{
try {
httpUrlConn.disconnect();
}catch (Exception e) {
e.printStackTrace();
org.jeecgframework.core.util.LogUtil.info("http close error:{}"+ e.getMessage());
}
}
return jsonObject;
}
public static void main(String args[]){
String key="26F72780372E84B6CFAED6F7B19139CC47B1912B6CAED753";
JSONObject jsonObject=new JSONObject();
jsonObject.put("id","40281381537e969401537eb9902d0006");
jsonObject.put("tableName","jform_contact");
String body=jsonObject.toJSONString();
Map param=new HashMap();
param.put("body",body);
String sign=SignatureUtil.sign(param,key);
System.out.println(" -- sign -- "+ sign);
JSONObject resp=HttpUtil.httpRequest("http://localhost:8080/jsc/api/cgFormDataController.do?getFormInfo","POST","body="+body,sign);
System.out.println(resp.toJSONString());
}
public static void main2(String args[]){
String key="26F72780372E84B6CFAED6F7B19139CC47B1912B6CAED753";
JSONObject jsonObject=new JSONObject();
jsonObject.put("id","40281381537e969401537eb9902d0006");
jsonObject.put("tableName","jform_contact");
JSONObject data=new JSONObject();
data.put("name","张山丰");
data.put("sex","0");
jsonObject.put("data",data);
String body=jsonObject.toJSONString();
Map param=new HashMap();
param.put("body",body);
String sign=SignatureUtil.sign(param,key);
System.out.println(" -- sign -- "+ sign);
JSONObject resp=HttpUtil.httpRequest("http://localhost:8080/jeecg/api/cgFormDataController.do?addFormInfo","POST","body="+body,sign);
System.out.println(resp.toJSONString());
}
}