Ejemplo n.º 1
0
  /**
   * 以multipart/form-data格式post网络数据
   *
   * @param readTimeout
   */
  public void postBytesNetData(int readTimeout, int connTimeout, BdHttpStat stat) throws Exception {
    stat.executeStatus = ExecuteStatus.BEGIN;
    try {
      URL url = new URL(context.getRequest().getUrl());
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      stat.executeStatus = ExecuteStatus.CREATE_CONN_BEFORE;
      HttpURLConnection conn = getConnect(url);
      stat.executeStatus = ExecuteStatus.CREATE_CONN_SUCC;
      long time = System.currentTimeMillis();
      if (mConn == null) {
        throw new java.net.SocketException("network not available.");
      }
      mConn.setRequestMethod("POST");
      mConn.setDoOutput(true);
      mConn.setDoInput(true);
      mConn.setConnectTimeout(connTimeout);
      mConn.setReadTimeout(readTimeout);
      mConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      context.getRequest().wrapHead(conn);
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      time = new Date().getTime();
      // 拖慢网络速度,暂时去掉
      // checkDNS(url);
      stat.dnsTime = new Date().getTime() - time;
      stat.executeStatus = ExecuteStatus.CONN_BEFORE;
      conn.connect();
      stat.executeStatus = ExecuteStatus.CONN_SUCC;
      stat.connectTime = new Date().getTime() - time - stat.dnsTime;
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }

      // 发送大文件,使用wap模式会引起getResponseCode无法返回
      if (timer != null) {
        timer.schedule(timerTask, POSTDATATIMEOUT * 3);
      }
      stat.executeStatus = ExecuteStatus.POST_BEFORE;
      context.getRequest().wrapPost2Conn(conn, boundary, stat);
      stat.executeStatus = ExecuteStatus.POST_SUCC;

      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      stat.executeStatus = ExecuteStatus.GETDATA_BEFORE;
      context.getResponse().getResponseHead(mConn);
      stat.responsedCode = context.getResponse().responseCode;
      context.getResponse().retBytes = getResponse(mConn);

      if (context.getResponse().retBytes != null) {
        stat.downloadSize = context.getResponse().retBytes.length;
      }
      stat.rspTime = new Date().getTime() - time;
      stat.executeStatus = ExecuteStatus.GETDATA_SUCC;
    } finally {
      if (timer != null) {
        timer.cancel();
      }
      BdCloseHelper.close(mConn);
    }
  }
Ejemplo n.º 2
0
  /**
   * 以application/x-www-form-urlencoded格式post网络数据
   *
   * @param readTimeout
   * @throws Exception
   */
  public void postNetData(int readTimeout, int conntimeOut, BdHttpStat stat) throws Exception {
    stat.executeStatus = ExecuteStatus.BEGIN;
    try {
      URL url = new URL(context.getRequest().getUrl());
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      stat.executeStatus = ExecuteStatus.CREATE_CONN_BEFORE;
      HttpURLConnection conn = getConnect(url);
      stat.executeStatus = ExecuteStatus.CREATE_CONN_SUCC;
      long time = System.currentTimeMillis();
      if (mConn == null) {
        throw new java.net.SocketException("network not available.");
      }
      mConn.setRequestMethod("POST");
      mConn.setDoOutput(true);
      mConn.setDoInput(true);
      mConn.setConnectTimeout(conntimeOut);
      mConn.setReadTimeout(readTimeout);
      mConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      context.getRequest().wrapHead(conn);
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      // 拖慢网络速度,暂时去掉
      // checkDNS(url);
      stat.dnsTime = System.currentTimeMillis() - time;
      stat.executeStatus = ExecuteStatus.CONN_BEFORE;
      conn.connect();
      stat.executeStatus = ExecuteStatus.CONN_SUCC;
      stat.connectTime = System.currentTimeMillis() - time - stat.dnsTime;
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      stat.executeStatus = ExecuteStatus.POST_BEFORE;
      context.getRequest().wrapPost2Conn(conn, stat);
      stat.executeStatus = ExecuteStatus.POST_SUCC;
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      stat.executeStatus = ExecuteStatus.GETDATA_BEFORE;
      context.getResponse().getResponseHead(mConn);
      stat.responsedCode = context.getResponse().responseCode;
      context.getResponse().retBytes = getResponse(mConn);

      if (context.getResponse().retBytes != null) {
        stat.downloadSize = context.getResponse().retBytes.length;
      }
      stat.rspTime = new Date().getTime() - time;
      stat.executeStatus = ExecuteStatus.GETDATA_SUCC;
    } finally {
      BdCloseHelper.close(mConn);
    }
  }
Ejemplo n.º 3
0
  public void getNetData(int readTimeout, int connTimeout, BdHttpStat stat) throws Exception {
    stat.executeStatus = ExecuteStatus.BEGIN;
    if (context.getResponse().isCancel == true) {
      throw new BdHttpCancelException();
    }
    String urlString = context.getRequest().generateGetString(stat);

    URL url = new URL(urlString);
    if (context.getResponse().isCancel == true) {
      throw new BdHttpCancelException();
    }
    stat.executeStatus = ExecuteStatus.CREATE_CONN_BEFORE;
    HttpURLConnection conn = getConnect(url);
    stat.executeStatus = ExecuteStatus.CREATE_CONN_SUCC;
    long time = System.currentTimeMillis();
    try {
      if (mConn == null) {
        throw new java.net.SocketException("network not available.");
      }
      mConn.setRequestMethod("GET");
      mConn.setConnectTimeout(connTimeout);
      mConn.setReadTimeout(readTimeout);
      context.getRequest().wrapHead(conn);
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      // 拖慢网络速度,暂时去掉
      // checkDNS(url);
      stat.dnsTime = new Date().getTime() - time;
      BdLog.i("GET:" + urlString);
      stat.executeStatus = ExecuteStatus.CONN_BEFORE;
      conn.connect();
      stat.executeStatus = ExecuteStatus.CONN_SUCC;
      stat.connectTime = new Date().getTime() - time - stat.dnsTime;
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }

      stat.executeStatus = ExecuteStatus.GETDATA_BEFORE;
      context.getResponse().getResponseHead(mConn);
      stat.responsedCode = context.getResponse().responseCode;
      context.getResponse().retBytes = getResponse(mConn);

      if (context.getResponse().retBytes != null) {
        stat.downloadSize = context.getResponse().retBytes.length;
      }
      stat.executeStatus = ExecuteStatus.GETDATA_SUCC;
      stat.rspTime = new Date().getTime() - time;
    } finally {
      if (mConn != null) {
        mConn.disconnect();
      }
    }
  }