zoukankan      html  css  js  c++  java
  • 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 = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> "
    + "<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> "
    + "</soap:Header> " + "<soapenv:Body> "
    + "<v2:RedoService xmlns:v2="http://www.ekingwin.com/esb/redo/v2"> " + "<v2:redoInfo> "
    + "<v2:tranId>" + loguid + "</v2:tranId> " + "<v2:userId>" + user + "</v2:userId> "
    + "<v2:comments>" + comments + "</v2:comments> " + "<v2:newMsg>" + newMsg + "</v2:newMsg> "
    + "<v2:ipLocation>" + ipLocation + "</v2:ipLocation> " + "</v2:redoInfo> "
    + "</v2:RedoService> " + "</soapenv:Body> " + "</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 = "{ " +
    " "request": { " +
    " "header": { " +
    " "BIZTRANSACTIONID": "qwertyuioplkjhgfd", " +
    " "COUNT": "1", " +
    " "CONSUMER": "ERP", " +
    " "SRVLEVEL": "1", " +
    " "ACCOUNT": "", " +
    " "PASSWORD": "" " +
    " }, " +
    " "List": { " +
    " "item": [{ " +
    " "appKey": "hsly", " +
    " "timeStamp": "1490931931732", " +
    " "version": "1.0", " +
    " "sign": "0aa25c2db8f46878e80a00ef3a348894", " +
    " "dataInfo": { " +
    " "beginTime": ""+startTime+"","+
    " "endTime": ""+endTime+"" " +
    " } " +
    " }] " +
    " } " +
    " } " +
    "}";
    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;

    }

  • 相关阅读:
    响应式的WEB设计
    WPF的抽奖程序
    使用Visual Studio 利用WinGDB编译和远程调试嵌入式Linux的程序
    Sqler 工具帮你解决(更新部分监控)
    YSlow相关规则的调优工具和方法
    wcf基础笔记
    HTTP协议之状态码详解
    网站首页图片滚动显示
    CodeLove初版发布
    HDFS学习– Namenode and Datanode
  • 原文地址:https://www.cnblogs.com/js1314/p/10788883.html
Copyright © 2011-2022 走看看