package com.lassenInterface.common.httpRpcPost;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
import com.lassenInterface.constant.WorkContext;
public class HttpRpcPost {
/**
*
* lassen-post方法
* @param domain 域名
* @param url rpc接口url
* @param params 参数
* @param loginAccount 登录账号
*
*/
public static String post(String domain, String url, Map<String, String> params, String loginAccount) throws Exception {
//调绕过验证方法
CloseableHttpClient client = SSLUtils.createIgnoreVerifySSL();
//初始化response内容
String responseContent = null;
//获取Cookie
String cookie = getCookie(domain, loginAccount);
//创建HttpPost
String reqURL = domain + url;
HttpPost post = new HttpPost(reqURL);
//构建Post请求的表单参数
ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
if (params != null) {
Set<String> keys = params.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
pairs.add(new BasicNameValuePair(key, params.get(key)));
}
}
//设置参数到请求对象中
post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
//设置header信息,指定报文头【Accept】、【Content-Type】、【Cookie】
post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
// post.setHeader("Content-Type", "application/json;charset=UTF-8");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Cookie", cookie);
try {
//执行请求操作,并拿到response(同步阻塞)
HttpResponse response = client.execute(post);
// 获取响应实体
HttpEntity entity = response.getEntity();
//获取Http状态码
int responseStatusCode = response.getStatusLine().getStatusCode();
//如果态码是200,表示请求成功
if (responseStatusCode == 200) {
//按指定编码转换结果实体为String类型
responseContent = EntityUtils.toString(entity,"UTF-8");
System.out.println("【Response】" + responseContent);
}
else{
System.err.println("【调用rpc失败】"+responseStatusCode);
}
//释放链接
client.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
client.close();
}
return responseContent;
}
/**
*
* etna-post方法
* @param domain 域名
* @param url rpc接口url
* @param params 参数
* @param loginAccount 登录账号
*
*/
public static String etnaPost(String domain, String url, Map<String, String> params, String loginAccount) throws Exception {
CloseableHttpClient client = SSLUtils.createIgnoreVerifySSL();
String responseContent = null;
String cookie = getEtnaCookie(domain, loginAccount);
HttpPost post = new HttpPost(domain + url);
ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
if (params != null) {
Set<String> keys = params.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
pairs.add(new BasicNameValuePair(key, params.get(key)));
}
}
post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
post.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
post.setHeader("content-type", "application/x-www-form-urlencoded");
post.setHeader("cookie", cookie);
try {
HttpResponse response = client.execute(post);
int responseStatusCode = response.getStatusLine().getStatusCode();
if (responseStatusCode == 200) {
responseContent = EntityUtils.toString(response.getEntity());
System.out.println("【Response】" + responseContent);
}
client.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
client.close();
}
return responseContent;
}
/**
*
* etna-get方法
* @param domain 域名
* @param url rpc接口url
* @param params 参数
* @param loginAccount 登录账号
*
*/
public static String etna_get(String domain, String url, String params, String loginAccount) throws Exception {
CloseableHttpClient client = SSLUtils.createIgnoreVerifySSL();
String rtnMsg = "";
try {
String cookie = getEtnaCookie(domain, loginAccount);
HttpGet httpget = new HttpGet(domain + url + "?" + params);
httpget.setHeader("cookie", cookie);
CloseableHttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
rtnMsg = EntityUtils.toString(entity);
}
response.close();
} catch (Exception e) {
rtnMsg = "ERROR";
} finally {
try {
client.close();
} catch (IOException e) {
rtnMsg = "ERROR";
}
}
System.out.println("【rtnMsg】" + rtnMsg);
return rtnMsg;
}
private static String getEtnaCookie(String domain, String loginAccount) throws Exception {
// TODO Auto-generated method stub
CloseableHttpClient client = SSLUtils.createIgnoreVerifySSL();
HttpPost loginpost = new HttpPost(domain + "/login/mock?openaccountId=" + loginAccount);
loginpost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
HttpResponse loginResponse = client.execute(loginpost);
String cookieReturn = "";
Map<String, String> cookieMap = new HashMap<String, String>();
if (loginResponse.getStatusLine().getStatusCode() == 200) {
Header[] cookies = loginResponse.getHeaders("Set-Cookie");
if (cookies.length == 0) {
System.out.println(String.format("未获取到cookie,请检查登录账户是否正确。登录账户=【%s】", loginAccount));
}
for (Header cookie : cookies) {
String value = cookie.getValue();
if (value.startsWith("LAVATMP0=") || value.startsWith("LAVATMP1=")) {
String[] valueArr = value.split(";");
String cookieContent = valueArr[0];
String[] cookieArr = cookieContent.split("=");
cookieMap.put(cookieArr[0], cookieArr[1]);
}
}
}
Set<String> keySet = cookieMap.keySet();
for (String key : keySet) {
cookieReturn += key + "=" + cookieMap.get(key) + ";";
}
return cookieReturn;
}
private static String getCookie(String domain, String loginAccount) throws Exception {
// TODO Auto-generated method stub
CloseableHttpClient client = SSLUtils.createIgnoreVerifySSL();
HttpPost loginpost = new HttpPost(domain + "/login/mock.json?openaccountId=" + loginAccount);
loginpost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
HttpResponse loginResponse = client.execute(loginpost);
String cookieReturn = "";
Map<String, String> cookieMap = new HashMap<String, String>();
if (loginResponse.getStatusLine().getStatusCode() == 200) {
Header[] cookies = loginResponse.getHeaders("Set-Cookie");
for (Header cookie : cookies) {
String value = cookie.getValue();
if (value.contains("LASSENLOGINUSER") || value.contains("LASSENLGAC") || value.contains("LASSENTMP0")) {
String[] valueArr = value.split(";");
String cookieContent = valueArr[0];
String[] cookieArr = cookieContent.split("=");
cookieMap.put(cookieArr[0], cookieArr[1]);
}
}
}
Set<String> keySet = cookieMap.keySet();
for (String key : keySet) {
cookieReturn += key + "=" + cookieMap.get(key) + ";";
}
return cookieReturn.substring(0, cookieReturn.length() - 1);
}
public static void main(String[] args) throws Exception {
String params = "{\"entrustOrganizationId\":\"10003\",\"entrustCourt\":\"速裁庭\",\"entrustPhase\":\"诉前\",\"caseCode\":\"(cxp)test-001\",\"causeAction\":\"民间借贷纠纷\",\"caseType\":\"laborDispute\",\"amount\":111,\"amountCurrency\":\"cny\",\"secretaryId\":\"487\",\"mediationOrganizationId\":\"10013\",\"mandator\":\"达达001\",\"mandatorContacts\":\"0571-101012020\",\"deadlineTime\":\"2018-06-03 10:00:00\",\"prosecutionDate\":\"2018-05-11 16:11:00\",\"entrustTime\":\"2018-05-11 16:11:00\",\"mediateEntityDTOList\":[{\"name\":\"达达001\",\"certifyType\":\"idCard\",\"idCard\":\"123456789\",\"mailAddress\":\"测试通讯地址\",\"mobile\":\"13290901010\",\"email\":\"13290901010@qq.com\",\"phone\":\"13690901010\",\"gender\":\"male\",\"lawOffice\":\"红心律所\",\"lawyerContacts\":\"13190901010\",\"lawyer\":\"小红\",\"contactInfo\":\"13090901010\",\"entityType\":\"normal\",\"entityRole\":\"accuser\",\"entityPosition\":1},{\"name\":\"达达002\",\"certifyType\":\"idCard\",\"idCard\":\"12345678901\",\"mailAddress\":\"测试通讯地址1\",\"mobile\":\"13280801010\",\"email\":\"13080801010@qq.com\",\"phone\":\"13380801010\",\"gender\":\"male\",\"lawOffice\":\"黑心律所\",\"lawyerContacts\":\"13180801010\",\"lawyer\":\"小黑\",\"contactInfo\":\"13080801010\",\"entityType\":\"normal\",\"entityRole\":\"accused\",\"entityPosition\":1}],\"securityFileIds\":[]}";
Map<String, String> parms = new HashMap<String, String>();
parms.put("param", params);
String etnaPost2 = etnaPost(WorkContext.ETNA_BEICU_URL, "/etnaMediator/case/insertMediateCase", parms, "13999999991");
String paramsss = "{\"securityId\":\"oWljD-e_bGBRKUFUIn-mqQ\"}";
Map parmsss = JSONObject.parseObject(paramsss, Map.class);
String etnaPost22 = etnaPost(WorkContext.ETNA_BEICU_URL, "/etnaMediator/case/queryByBaseCaseId", parmsss, "13999999991");
String paramss = "securityId=oWljD-e_bGBRKUFUIn-mqQ";
String getString = etna_get(WorkContext.ETNA_BEICU_URL, "/etnaMediator/case/queryByBaseCaseId", paramss, "13999999991");
}
}