• httpclient的调用 发送json字符串


    public static String postHttp(JSONObject jsonObject, String jsonUrl){

    String responseMsg="";


    //获取http连接
    HttpClient httpClient = new HttpClient();

    httpClient.getParams().setContentCharset("utf-8");

    //2.构造PostMethod的实例
    PostMethod postMethod=new PostMethod(jsonUrl);

    try {

    String postJson = jsonObject.toJSONString();

    RequestEntity se = new StringRequestEntity(postJson, "application/json", "UTF-8");

    //url设置json参数
    postMethod.setRequestEntity(se);

    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

    try {
    //发送url 单独处理该异常
    httpClient.executeMethod(postMethod);//200
    } catch (Exception e) {
    try {
    httpClient.executeMethod(postMethod);//200
    } catch (Exception e2) {
    try {
    httpClient.executeMethod(postMethod);//200
    } catch (Exception e3) {
    log.error("连续3次连接异常,终止数据推送...",e);
    }
    }
    }

    responseMsg=postMethod.getResponseBodyAsString().trim();

    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    //7.释放连接
    postMethod.releaseConnection();
    }
    return responseMsg;


    }

  • 相关阅读:
    Redis 记录
    flink 流控机制详解
    备份和快照的区别
    LVS 总结
    Keepalived 总结
    OpenResty 总结
    Lua 总结
    Element-UI 总结
    Java transient 关键字
    Activiti 框架
  • 原文地址:https://www.cnblogs.com/wangzheguilai/p/8136871.html
Copyright © 2020-2023  润新知