• 通过HttpClient调用服务


    /**
    * 通过HttpClient调用服务
    *
    * @param url 路径
    * data json数据
    * @return
    */

    //post请求方法
    public String sendItsmTask(String url, String data) throws Exception{

    System.out.println("进入发送itsm方法 url:"+ url + "data:" +data);

    String isSuccess = "success";

    HttpPost post = null;
    try {
    HttpClient httpClient = new DefaultHttpClient();

    // 设置超时时间
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);

    post = new HttpPost(url);
    // 构造消息头
    post.setHeader("Content-type", "application/json; charset=utf-8");
    post.setHeader("Connection", "Close");


    // 构建消息实体
    StringEntity entity = new StringEntity(data, Charset.forName("UTF-8"));
    entity.setContentEncoding("UTF-8");
    // 发送Json格式的数据请求
    entity.setContentType("application/json");
    post.setEntity(entity);

    HttpResponse response = httpClient.execute(post);

    // 检验返回码
    int statusCode = response.getStatusLine().getStatusCode();
    if(statusCode != HttpStatus.SC_OK){
    log.info("请求出错: "+statusCode);
    isSuccess = "error";
    }else{
    String retCode = "";
    // 返回码中包含retCode
    // 找响应信息
    for(Header header : response.getAllHeaders()){
    System.out.println("这是个什么神奇的东西"+header.getName() +":" + header.getValue());
    if("retcode".equals(header.getName())){
    retCode = header.getValue();
    }
    }

    if(IAS_SUCCESS != retCode ){
    // 日志打印
    log.info("调用全网接口下发工单 返回状态异常响应码为: "+retCode);
    isSuccess = "error";
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    isSuccess = "error";
    }finally{
    if(post != null){
    try {
    post.releaseConnection();
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    return isSuccess;


    }
  • 相关阅读:
    虚函数
    类的继承
    析构
    构造
    枚举类型
    c++中的静态类型 static
    c++中的类
    sizeof和strlen的区别
    剑指36 二叉搜索书与双向链表
    剑指35 复杂链表的复制
  • 原文地址:https://www.cnblogs.com/xiaoyu1994/p/8567543.html
Copyright © 2020-2023  润新知