使用HTTP URL,发送请求并接收服务器接口响应消息,如5分钟内发生5次异常,则把异常信息发送到指定电子邮箱中;指定时间内异常未达到5次则清零。
1,Http Post
package m;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import MailSenderInfo.MyAuthenticator;
public class test_postout {
static String sessionId = "";
static int num;
static int j;
// 默认的url
String surl = "http://IP:Port/ibis/faceService/compareFaces";
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入服务器接口URL:");
String surl = sc.nextLine();
System.out.println("请输入图片img1所在文件夹的路径:");
String folderPath1 = sc.nextLine();
folderPath1 = folderPath1.replace("\\", "/");
System.out.println("请输入图片img1的名称:");
String imageName1 = sc.nextLine();
System.out.println("请输入图片img2所在文件夹的路径:");
String folderPath2 = sc.nextLine();
folderPath2 = folderPath2.replace("\\", "/");
System.out.println("请输入图片img2的名称:");
String imageName2 = sc.nextLine();
// 执行图片上传接收方法
postOut(surl, folderPath1, imageName1, folderPath2, imageName2);
sc.close();
}
public static void postOut(String surl, String folderPath1, String imageName1, String folderPath2,
String imageName2) {
test_parm mp = new test_parm();
// 获取文件夹图片调用接口次数
int num = 0;
// 接口调用不成功SocketException次数,达到门限就告警
int i = 0;
// 5分钟计时器,j>300秒时,如果异常计数器未达到门限值则,计数器清零
int j = 0;
// 脸脸比对返回code不是0,的门限告警值
int k = 0;
// 调用接口未能返回200 OK消息
int l = 0;
// EX的门限告警
int m = 0;
do {
// 开始比对时间点t1
long t1 = System.currentTimeMillis();
// 比对次数num++;
num = mp.getNum() + 1;
mp.setNum(num);
// http属性设置
try {
URL url = new URL(surl);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("contentType", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.connect();
Date date = new Date();
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
String imgpath1 = "";
String imgpath2 = "";
imgpath1 = folderPath1 + "/" + imageName1;
imgpath2 = folderPath2 + "/" + imageName2;
// 把jpg图片转换为二进制流
System.out.println("imgpath1:" + imgpath1);
System.out.println("imgpath2:" + imgpath2);
// 构造表单消息体
//System.out.println("构造消息体前的时间:" + System.currentTimeMillis()
//+ "***************************************************");
// 图片base64编码
String img1 = null;
String img2 = null;
try {
//System.out.println("编码前" + System.currentTimeMillis());
img1 = URLEncoder.encode(test_PicBASE64.getPicBASE64(folderPath1 + "/" + imageName1), "UTF-8");
img2 = URLEncoder.encode(test_PicBASE64.getPicBASE64(folderPath2 + "/" + imageName2), "UTF-8");
//System.out.println("编码后" + System.currentTimeMillis());
} catch (Exception e) {
throw new RuntimeException(e);
}
String data = ("{" + "\"" + "methodName" + "\"" + ":" + "\"" + "compareFaces" + "\"" + "," + "\""
+ "channel" + "\"" + ":" + "\"" + "0400" + "\"" + "," + "\"" + "img1" + "\"" + ":" + "\"" + img1
+ "\"" + "," + "\"" + "img2" + "\"" + ":" + "\"" + img2 + "\"" + "}");
out.writeBytes("requestData" + "=" + data);
out.flush();
//System.out.println("HTTP发送时间" + System.currentTimeMillis());
//long t2 = System.currentTimeMillis();
out.close();
// 接收返回值
ByteArrayOutputStream baos = new Byte