java调用webservice,restful

时间:2024-01-09 18:02:44

java调用webservice

public String redoEsb(String loguid, String user, String comments, String newMsg, String ipLocation)
throws Exception {
//String redoEsb = VariableStore.getValue("redoEsb");

String redoEsb = "D:\Admin\"
String result = "";
String data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
+ "<soap:Header xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
+ "</soap:Header>\r\n" + "<soapenv:Body>\r\n"
+ "<v2:RedoService xmlns:v2=\"http://www.ekingwin.com/esb/redo/v2\">\r\n" + "<v2:redoInfo>\r\n"
+ "<v2:tranId>" + loguid + "</v2:tranId>\r\n" + "<v2:userId>" + user + "</v2:userId>\r\n"
+ "<v2:comments>" + comments + "</v2:comments>\r\n" + "<v2:newMsg>" + newMsg + "</v2:newMsg>\r\n"
+ "<v2:ipLocation>" + ipLocation + "</v2:ipLocation>\r\n" + "</v2:redoInfo>\r\n"
+ "</v2:RedoService>\r\n" + "</soapenv:Body>\r\n" + "</soapenv:Envelope>";
String path = redoEsb;
try {
result = loadByXML(path, data);
if(result.contains("fault")) {
throw new Exception("重做失败!!!");
}
return result;
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
}

public String loadByXML(String url, String query) throws Exception {
try {
URL restURL = new URL(url);

HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(query.length()));
conn.setConnectTimeout(60 * 1000);
conn.setReadTimeout(60 * 1000);

/* conn.setAllowUserInteraction(false); */

//
PrintStream ps = new PrintStream(conn.getOutputStream());
ps.print(query);

ps.close();
System.out.println(conn.getOutputStream());
InputStreamReader ir = new InputStreamReader(conn.getInputStream());
BufferedReader bReader = new BufferedReader(ir);

String line, resultStr = "";

while (null != (line = bReader.readLine()))

{

resultStr += line;

}

bReader.close();

return resultStr;
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
//
// return null;
}

java调用restful

public String PersonNumToEsb(){
String path = calblewayEsb;
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.MINUTE, -20);

Calendar endcalendar = Calendar.getInstance();

endcalendar.add(Calendar.MINUTE, -10);

String startTime = format.format(calendar.getTime());
String endTime = format.format(endcalendar.getTime());
String data = "{\r\n" +
" \"request\": {\r\n" +
" \"header\": {\r\n" +
" \"BIZTRANSACTIONID\": \"qwertyuioplkjhgfd\",\r\n" +
" \"COUNT\": \"1\",\r\n" +
" \"CONSUMER\": \"ERP\",\r\n" +
" \"SRVLEVEL\": \"1\",\r\n" +
" \"ACCOUNT\": \"\",\r\n" +
" \"PASSWORD\": \"\"\r\n" +
" },\r\n" +
" \"List\": {\r\n" +
" \"item\": [{\r\n" +
" \"appKey\": \"hsly\",\r\n" +
" \"timeStamp\": \"1490931931732\",\r\n" +
" \"version\": \"1.0\",\r\n" +
" \"sign\": \"0aa25c2db8f46878e80a00ef3a348894\",\r\n" +
" \"dataInfo\": {\r\n" +
" \"beginTime\": \""+startTime+"\","+
" \"endTime\": \""+endTime+"\"\r\n" +
" }\r\n" +
" }]\r\n" +
" }\r\n" +
" }\r\n" +
"}";
String result="";
try {
result = hstdService.loadByjson(path,data);
// sessionid = result.substring(result.indexOf("<sessionId>")+"<sessionId>".length(), result.indexOf("</sessionId>"));

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonMapper jsonMapper = new JsonMapper();
Map<String, Object> map = jsonMapper.fromJson(result, Map.class);
List<Map<String,Object>> listMap = (List<Map<String,Object>>)((Map<String,Object>)((Map<String,Object>)map.get("request")).get("list")).get("item");

for (Map<String,Object>m : listMap) {
CablewayDto ca = new CablewayDto();
ca.setOtmDate(ObjectToString(m.get("OTMDATE")));
ca.setOtmType(ObjectToString(m.get("OTMTYPE")));
ca.setParkname(ObjectToString(m.get("PARKNAME")));
ca.setSearchtype(ObjectToString(m.get("SEARCHTYPE")));
ca.setTicketcount(ObjectToString(m.get("TICKETCOUNT")));

caDao.saveByid(ca.getOtmDate(),
ca.getOtmType(),
ca.getParkname(),
ca.getSearchtype(),
ca.getTicketcount());
}
}

public String loadByjson(String url,String query) throws Exception
{

URL restURL = new URL(url);

HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(query.length()));
conn.setConnectTimeout(60*1000);
conn.setReadTimeout(60*1000);

PrintStream ps = new PrintStream(conn.getOutputStream());
ps.print(query);

ps.close();

BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line,resultStr="";

while(null != (line=bReader.readLine()))

{

resultStr +=line;

}

bReader.close();

return resultStr;

}

java调用get请求

public String loadByGet(String url) throws Exception
{

URL restURL = new URL(url);

HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setConnectTimeout(60*1000);
conn.setReadTimeout(60*1000);

BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line,resultStr="";

while(null != (line=bReader.readLine()))

{

resultStr +=line;

}

bReader.close();

return resultStr;

}