java调用webservice,restful

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 = "\r\n"
+ "\r\n"
+ "
\r\n" + "\r\n"
+ "\r\n" + "\r\n"
+ "" + loguid + "\r\n" + "" + user + "\r\n"
+ "" + comments + "\r\n" + "" + newMsg + "\r\n"
+ "" + ipLocation + "\r\n" + "
\r\n"
+ "
\r\n" + "
\r\n" + "
";
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("")+"".length(), result.indexOf(""));

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

for (Mapm : 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;

}

转载于:https://www.cnblogs.com/js1314/p/10788883.html

你可能感兴趣的:(java,json)