Esempio n. 1
0
  /**
   * 处理返回值
   *
   * @param mConn
   * @throws Exception
   */
  private byte[] getResponse(HttpURLConnection mConn) throws Exception {
    byte[] ret = null;
    ByteArrayOutputStream outputstream = new ByteArrayOutputStream(BUFFERSIZE);
    ;
    InputStream in = null;
    try {
      if (mConn != null) {

        byte[] buf = new byte[BUFFERSIZE];
        int num = -1;
        in = mConn.getInputStream();

        while (context.getResponse().isCancel == false && (num = in.read(buf)) != -1) {
          outputstream.write(buf, 0, num);
        }

        if (context.getResponse().isCancel) {
          throw new BdHttpCancelException();
        }

        ret = outputstream.toByteArray();
      }
      return ret;
    } finally {
      BdCloseHelper.close(outputstream);
      BdCloseHelper.close(in);
    }
  }
Esempio 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);
    }
  }
Esempio n. 3
0
 /** 取消当前的网络请求 */
 public void cancelNetConnect() {
   context.getResponse().isCancel = true;
   BdCloseHelper.close(mConn);
 }
Esempio n. 4
0
  /**
   * 下载文件
   *
   * @return true:成功; false:失败
   */
  @SuppressWarnings("resource")
  public boolean downloadFile(
      String name, Handler handler, int what, int readTimeout, int conntimeout) throws Exception {
    InputStream in = null;
    boolean ret = false;
    long time = 0;
    FileOutputStream fileStream = null;
    try {
      URL url = new URL(context.getRequest().getUrl());
      mConn = getConnect(url);
      if (mConn == null) {
        throw new java.net.SocketException();
      }
      mConn.setConnectTimeout(conntimeout);
      mConn.setReadTimeout(readTimeout);
      mConn.setInstanceFollowRedirects(true);
      if (context.getResponse().isCancel == true) {
        return false;
      }
      time = new Date().getTime();

      File file = ZXFileUtil.createFileIfNotFound(name);
      if (file == null) {
        throw new FileNotFoundException();
      }
      long file_length = file.length();
      fileStream = new FileOutputStream(file, true);
      //			if (context.getRequest().mIsLimited == true) {
      //				mConn.addRequestProperty(
      //						"Range",
      //						"bytes=" + String.valueOf(file_length) + "-"
      //								+ String.valueOf(file_length + 200000));
      //			} else {
      mConn.addRequestProperty("Range", "bytes=" + String.valueOf(file_length) + "-");
      //			}

      mConn.connect();

      //            while(mConn.getResponseCode()==302){
      //                mConn.disconnect();
      //                context.getRequest().setUrl(mConn.getHeaderField("location"));
      //                mConn.connect();
      //            }

      context.getResponse().responseCode = mConn.getResponseCode();
      if (!isFileSegSuccess()) {
        throw new UnsupportedOperationException();
      }

      /** 判断是否是移动的提示信息 */
      if (mConn.getContentType().contains("text/vnd.wap.wml") == true) {
        mConn.disconnect();
        context.getResponse().responseCode = 0;
        return downloadFile(name, handler, what, readTimeout, conntimeout);
      }
      int contentLen = 0;
      String range = mConn.getHeaderField("Content-Range");
      if (range != null) {
        int index = range.indexOf("/");
        if (index != -1) {
          contentLen = toInt(range.substring(index + 1), 0);
        }
      }
      if (contentLen == 0 && context.getResponse().responseCode == HttpStatus.SC_OK) {
        String length = mConn.getHeaderField("Content-Length");
        if (length != null) {
          contentLen = toInt(length, 0);
        } else {
          length = mConn.getHeaderField("Accept-Length");
          if (length != null) {
            contentLen = toInt(length, 0);
          }
        }
      }
      contentLen = contentLen == 0 ? 1 : contentLen;
      if (file_length >= contentLen) {
        return true;
      }
      in = mConn.getInputStream();
      byte[] buf = new byte[BUFFERSIZE];
      int num = -1;
      int datalenth = 0;
      int notify_num = 0;
      if (contentLen > 0) {
        notify_num = contentLen / 50;
      }
      int notify_tmp = 0;
      if (handler != null && file_length > 0) {
        handler.sendMessage(handler.obtainMessage(what, (int) file_length, contentLen));
      }
      while (context.getResponse().isCancel == false && (num = in.read(buf)) != -1) {
        try {
          fileStream.write(buf, 0, num);
        } catch (Exception ex) {
          throw new FileNotFoundException();
        }
        datalenth += num;
        notify_tmp += num;
        if (handler != null && (notify_tmp > notify_num || datalenth == contentLen)) {
          notify_tmp = 0;
          handler.sendMessage(
              handler.obtainMessage(what, (int) (datalenth + file_length), contentLen));
        }
      }
      try {
        fileStream.flush();
      } catch (Exception ex) {
        throw new FileNotFoundException();
      }
      time = new Date().getTime() - time;
      BdLog.i("NetWork", "downloadFile", "time = " + String.valueOf(time) + "ms");
      if (contentLen != -1) {
        BdLog.i("NetWork", "downloadFile", "data.zise = " + String.valueOf(contentLen));
      }
      if (datalenth + file_length >= contentLen) {
        ret = true;
      }
    } finally {
      BdCloseHelper.close(in);
      BdCloseHelper.close(mConn);
      BdCloseHelper.close(fileStream);
    }
    return ret;
  }
Esempio n. 5
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);
    }
  }