コード例 #1
0
ファイル: HttpUtil.java プロジェクト: guozanhua/TTServer
 public static IMHttpResponse get(String url) {
   IMHttpResponse response = new IMHttpResponse();
   response.setStatusCode(404);
   if (StringUtils.isEmpty(url)) {
     return response;
   }
   int statusCode = 404;
   CloseableHttpClient httpClient = null;
   HttpGet httpGet = null;
   try {
     httpClient = HttpClients.createDefault();
     httpGet = new HttpGet(url); // HTTP Get请求(POST雷同)
     RequestConfig requestConfig =
         RequestConfig.custom()
             .setSocketTimeout(2000)
             .setConnectTimeout(2000)
             .build(); // 设置请求和传输超时时间
     httpGet.setConfig(requestConfig);
     CloseableHttpResponse hresp = httpClient.execute(httpGet); // 执行请求
     String responseString = EntityUtils.toString(hresp.getEntity());
     response.setStatusCode(hresp.getStatusLine().getStatusCode());
     response.setResponseBody(responseString);
     return response;
   } catch (Exception e) {
     logger.error("error code: " + statusCode, e);
   } finally {
     if (httpGet != null) {
       httpGet.releaseConnection();
     }
   }
   return response;
 }
コード例 #2
0
ファイル: HttpUtil.java プロジェクト: guozanhua/TTServer
  @SuppressWarnings({"rawtypes", "unchecked"})
  public static IMHttpResponse post(String url, Map params) {
    IMHttpResponse response = new IMHttpResponse();
    if (StringUtils.isEmpty(url)) {
      response.setStatusCode(404);
      return response;
    }

    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;
    try {
      httpPost = new HttpPost(url); // HTTP Get请求(POST雷同)
      List<NameValuePair> postData = new ArrayList<NameValuePair>();
      Iterator<Entry> piter = params.entrySet().iterator();
      while (piter.hasNext()) {
        Entry entry = piter.next();
        postData.add(
            new BasicNameValuePair(
                String.valueOf(entry.getKey()), String.valueOf(entry.getValue())));
      }
      StringEntity entity = new StringEntity(URLEncodedUtils.format(postData, "UTF-8"));
      httpPost.setEntity(entity);
      RequestConfig requestConfig =
          RequestConfig.custom()
              .setSocketTimeout(2000)
              .setConnectTimeout(2000)
              .build(); // 设置请求和传输超时时间
      httpPost.setConfig(requestConfig);
      httpClient = HttpClients.createDefault();
      CloseableHttpResponse hresp = httpClient.execute(httpPost); // 执行请求
      String responseString = EntityUtils.toString(hresp.getEntity());
      response.setStatusCode(hresp.getStatusLine().getStatusCode());
      response.setResponseBody(responseString);
      return response;
    } catch (Exception e) {
      logger.error("url: " + url, e);
    } finally {
      if (httpPost != null) {
        httpPost.releaseConnection();
      }
    }
    return response;
  }
コード例 #3
0
  /** 解析数据包,主要负责解析数据包前8个字节统一格式的头部信息,生成Packet对象, 剩余的数据部分的解析在后面具体的action处理 */
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
      throws Exception {

    if (buffer.readableBytes() < SysConstants.PROTOCOL_HEADER_LENGTH) {
      return null;
    }

    buffer.markReaderIndex(); // 标记一下
    int length = buffer.readInt(); // 消息长度
    char serviceId = buffer.readChar(); // 服务号, 后端这边固定1000
    char commandId = buffer.readChar(); // 命令号
    char version = buffer.readChar(); // 消息version, 1个字节
    char reserved = buffer.readChar(); // 保留,可用户如序列号等
    int contentLength = length - SysConstants.PROTOCOL_HEADER_LENGTH; // 计算数据包中业务数据部分的长度(去除头部长度16)

    if (buffer.readableBytes() < contentLength) {
      logger.info("数据长度:" + contentLength);
      buffer.resetReaderIndex(); // 返回到上面标记的位置
      return null;
    }

    ChannelBuffer cBuffer = ChannelBuffers.buffer(contentLength);
    buffer.readBytes(cBuffer, contentLength); // 转移所有业务部分的数据到新的byte

    Packet packet = new Packet();
    packet.setLength(contentLength);
    packet.setServiceId(serviceId);
    packet.setCommandId(commandId);
    packet.setVersion(version);
    packet.setReserved(reserved);
    DataBuffer dataBuffer = new DataBuffer(cBuffer); // 数据部分
    packet.setContentBuffer(dataBuffer);

    // logger.info("decode packet serviceId : " + packet.getServiceId()
    // + " commandId: " + packet.getCommandId());
    return packet;
  }