zoukankan      html  css  js  c++  java
  • http发送

    package cn.com.yitong.wdph.util;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.ProtocolException;
    import java.net.URL;

    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import com.alibaba.fastjson.JSONObject;

    import cn.com.yitong.ares.error.AresRuntimeException;
    import cn.com.yitong.ares.net.NetConst;

    /**
    * 简易http通信,发往指定地址
    *
    * @author
    */
    public class WdphHttpSendUtils {

    private static Logger logger = LoggerFactory.getLogger(WdphHttpSendUtils.class);

    private static int connectTimeOut = 5000;

    private static int readTimeOut = 90000;

    public static JSONObject sendHttp(JSONObject request, String url) {
    logger.debug("-简易http通信,发往指定地址-run--");
    // ----------http请求配置----------
    HttpURLConnection httpURLConnection = null;
    // out部分
    OutputStream outputStream = null;
    // in部分
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    try {
    httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
    // ----------设置请求属性信息----------
    setHttpConnection(httpURLConnection);
    if (logger.isDebugEnabled()) {
    logger.debug("request headers:{}", httpURLConnection.getRequestProperties());
    }
    // 获取URLConnection对象对应的输出流
    outputStream = httpURLConnection.getOutputStream();
    // 发送请求参数
    if (logger.isDebugEnabled()) {
    logger.debug("request body:{}", request.toJSONString());
    }
    outputStream.write(request.toJSONString().getBytes());
    outputStream.flush();
    // 获取URLConnection对象对应的输入流
    inputStream = httpURLConnection.getInputStream();
    inputStreamReader = new InputStreamReader(inputStream, NetConst.UTF_8);
    bufferedReader = new BufferedReader(inputStreamReader);
    StringBuffer sb = new StringBuffer();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
    sb.append(line);
    }
    JSONObject jsb = JSONObject.parseObject(sb.toString());
    if (logger.isDebugEnabled()) {
    logger.debug("接收到渠道端返回的响应,响应报文为" + jsb.toJSONString());
    }
    return jsb;
    } catch (Exception e) {
    logger.error("调用渠道服务异常-->地址:{} 异常错误栈: {}", url, e);
    throw new AresRuntimeException("net.connect.error", url);
    } finally {
    // ----------资源释放----------
    IOUtils.closeQuietly(outputStream);
    IOUtils.closeQuietly(inputStream);
    IOUtils.closeQuietly(inputStreamReader);
    IOUtils.closeQuietly(bufferedReader);
    if (null != httpURLConnection) {
    try {
    httpURLConnection.disconnect();
    } catch (Exception e) {
    logger.warn("http释放资源失败,", e);
    }
    }
    }
    }

    /**
    * 设置请求属性信息
    *
    * @param httpConn
    * @throws ProtocolException
    */
    private static void setHttpConnection(HttpURLConnection httpConn) throws ProtocolException {
    httpConn.setRequestMethod("POST");
    httpConn.setConnectTimeout(connectTimeOut);
    httpConn.setReadTimeout(readTimeOut);
    httpConn.setRequestProperty("Connection", "keep-alive");
    httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
    httpConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    httpConn.setRequestProperty("Accept", "application/json");
    httpConn.setRequestProperty("User-Agent",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1");
    httpConn.setDoInput(true);
    httpConn.setDoOutput(true);
    }
    }

  • 相关阅读:
    单片机中的类型转换
    vs2013CCyusb报错(CyAPI.obj)
    c/c++ 去掉空格函数
    keil关于正点原子的sys.h工程报错修改
    【C语言】华软C语言程序设计复习
    c/c++中,clock函数的用法和作用
    vs mfc出现错误“MSB8301”解决办法
    vs出现“未将对象引用到实例的错误”
    keil uv5 代码格式化
    嵌入式软件面试
  • 原文地址:https://www.cnblogs.com/jishumonkey/p/12192878.html
Copyright © 2011-2022 走看看