请求方式
泛微发送请求,将数据同步至第三方系统,有两种请求方式:
- get请求
- post请求
GET请求
相信各位都清楚get请求是什么,在这里就不做过多介绍了
一般当传递的参数比较少的时候,就采用get请求比较方便。具体代码如下所示:
private String doGet(String pathUrl) {
StringBuffer sbf = new StringBuffer();
System.out.println("开始调用第三方系统接口,pathurl:" + pathUrl);
try {
URL apiUrl = new URL(pathUrl);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
sbf.append(inputLine);
}
reader.close();
System.out.println("调用第三方接口返回值:" + sbf.toString());
} else {
System.out.println("GET request failed. Response code: " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
return sbf.toString();
}
private void syncDimStatusToBM() {
String databaseName = "OA";
RecordSetDataSource zsrs = new RecordSetDataSource(databaseName);
RecordSetDataSource urs = new RecordSetDataSource(databaseName);
String sql = "select * from table1";
zsrs.execute(sql);
while (zsrs.next()) {
String t1= zsrs.getString("t1");
String t2= zsrs.getString("t2");
String requestid = zsrs.getString("requestid");
if ((workcode != null) && (!workcode.isEmpty()) && (workcode.length() > 0) && (lastname != null)
&& (!lastname.isEmpty()) && (workcode.length() > 0)) {
try {
String nameStr = new String(URLEncoder.encode(t2, "utf-8").getBytes());
String url = "https://example.cn/test/clearAll/" + t1+ "/" + nameStr;
String result = doGet(url);
System.out.println("requestid: " + requestid + ",url:" + url + ", result: " + result);
JsonParser parser = new JsonParser();
if ((result != null) && (result.length() > 0) && (parser.parse(result) != null)) {
JsonParser parser1 = new JsonParser();
JsonObject object1 = (JsonObject) parser1.parse(result);
String status = object1.get("retCode").getAsString();
String retMsg = object1.get("retMsg").getAsString();
if (retMsg != null && retMsg.length() > 0) {
retMsg = new String(retMsg.getBytes("gbk"), "UTF-8");
}
System.out.println("workcode: " + t1+ ", status: " + status + ", message: " + retMsg);
if ((status != null) && (!status.isEmpty()) && (status.length() > 0)) {
if (status.equals("1")) {
String usql = "update table1 set tname = '123' where requestid = " + requestid;
urs.execute(usql);
} else if ((status.equals("0")) && (retMsg != null) && (!retMsg.isEmpty())
&& ((retMsg.contains("用户不存在")) || (retMsg.contains("用户权限不足")))) {
String usql = "update table3 set sftj = 'Y' where requestid=" + requestid;
urs.execute(usql);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
POST请求
一般当你传送的请求参数比较多的时候,用post请求的时候比较方便,传json的数据格式也比较方便
private static String doPost(String json) {
/*
*跳过ssl认证,因为jdk1.6比较老旧,所支持的ssl比较不全面,
*所以就会报出异常,ssl认证失败
*但是这样,会损失系统的安全性,第三方容易跳过验证攻击服务器
*/
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost()+"urlHostName"+urlHostName);
if(!urlHostName.equals("www.example.com")&&!urlHostName.equals("www.example.com")){
return false;
}
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
System.out.println("传入的参数值"+json);
StringBuilder response = new StringBuilder();
HttpURLConnection connection = null;
try {
URL url = new URL("https://www.example.com/test/addUser");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
byte[] input = json.getBytes(Charset.forName("UTF-8"));
os.write(input, 0, input.length);
os.flush();
int responseCode = connection.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader br = new BufferedReader(new InputStreamReader(responseCode == HttpURLConnection.HTTP_OK ? connection.getInputStream() : connection.getErrorStream(),Charset.forName("UTF-8")));
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
} catch (Exception e) {
System.out.println("doPost方法异常"+e);
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return response.toString();
}
泛微OA E8发起请求,不仅仅要考虑到依赖的包是否是被编译环境jdk1.6所支持的,如果你们的测试环境和正式环境的编译环境不一样的话,最好就是直接将eclipse或者idea的编译jdk设置成正式环境所支持的编译环境,这样能避免很多问题。