private boolean handshake() throws Exception {
    URL homePage = new URL("http://mangaonweb.com/viewer.do?ctsn=" + ctsn);

    HttpURLConnection urlConn = (HttpURLConnection) homePage.openConnection();
    urlConn.connect();
    if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return (false);

    // save the cookie
    String headerName = null;
    for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
      if (headerName.equals("Set-Cookie")) {
        cookies = urlConn.getHeaderField(i);
      }
    }

    // save cdn and crcod
    String page = "", line;
    BufferedReader stream =
        new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
    while ((line = stream.readLine()) != null) page += line;

    cdn = param(page, "cdn");
    crcod = param(page, "crcod");

    return (true);
  }
Пример #2
0
 /**
  * 获取真实文件名(xx.后缀),通过网络获取.
  *
  * @param connection 连接
  * @return 文件名
  */
 public static String getRealFileName(HttpURLConnection connection) {
   String name = null;
   try {
     if (connection == null) {
       return name;
     }
     if (connection.getResponseCode() == 200) {
       for (int i = 0; ; i++) {
         String mime = connection.getHeaderField(i);
         if (mime == null) {
           break;
         }
         // "Content-Disposition","attachment; filename=1.txt"
         // Content-Length
         if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) {
           Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase());
           if (m.find()) {
             return m.group(1).replace("\"", "");
           }
         }
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
     AbLogUtil.e(AbFileUtil.class, "网络上获取文件名失败");
   }
   return name;
 }
Пример #3
0
 public static long getFileSize(String sURL) {
   int nFileLength = -1;
   try {
     URL url = new URL(sURL);
     HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
     httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
     int responseCode = httpConnection.getResponseCode();
     if (responseCode >= 400) {
       System.err.println("Error Code : " + responseCode);
       return -2; // -2 represent access is error
     }
     System.out.println(httpConnection.getContentLength());
     String sHeader;
     for (int i = 1; ; i++) {
       sHeader = httpConnection.getHeaderFieldKey(i);
       if (sHeader != null) {
         if (sHeader.equals("Content-Length")) {
           nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));
           break;
         }
       } else break;
     }
   } catch (IOException e) {
     e.printStackTrace();
   } catch (Exception e) {
     e.printStackTrace();
   }
   System.out.println(nFileLength);
   return nFileLength;
 }
Пример #4
0
 /**
  * 抓取数据,with cookie
  *
  * @param postStr
  * @param type
  * @return
  * @throws Exception
  */
 private String doSpider2(String postStr, int type) throws Exception {
   url = new URL(surl);
   connection = url.openConnection();
   httpUrlConnection = (HttpURLConnection) connection;
   httpUrlConnection.setRequestProperty("accesstoken", "2436dba7713b4680802eeacbd275ffd0");
   if (type == 1) {
     httpUrlConnection.setRequestProperty("Cookie", sessionId);
   }
   if (type == 0) {
     httpUrlConnection.setDoOutput(true);
     out = new OutputStreamWriter(httpUrlConnection.getOutputStream(), "UTF-8");
     out.write(postStr);
     out.flush();
     out.close();
     out = null;
   }
   for (int i = 1; (key = httpUrlConnection.getHeaderFieldKey(i)) != null; i++) {
     if (key.equalsIgnoreCase("set-cookie")) {
       cookieVal = httpUrlConnection.getHeaderField(i);
       cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
       sessionId = sessionId + cookieVal + ";";
     }
   }
   sCurrentLine = "";
   l_urlStream = httpUrlConnection.getInputStream();
   StringBuffer sb = new StringBuffer();
   l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
   while ((sCurrentLine = l_reader.readLine()) != null) {
     sb.append(sCurrentLine);
   }
   gc();
   return sb.toString();
 }
  static void download(final DownloadJob job) throws IOException {
    final HttpURLConnection connection = (HttpURLConnection) job.url.openConnection();
    connection.setRequestMethod("GET");
    if (job.headers != null) {
      for (final Entry<String, String> e : job.headers.entrySet()) {
        connection.addRequestProperty(e.getKey(), e.getValue());
      }
    }
    connection.connect();

    job.responseCode = connection.getResponseCode();
    job.responseHeaders.clear();
    job.responseText = null;

    for (int i = 1; ; i++) {
      final String key = connection.getHeaderFieldKey(i);
      if (key == null) {
        break;
      }
      final String value = connection.getHeaderField(i);
      job.responseHeaders.put(key.toLowerCase(), value);
    }

    final StringBuilder sb = new StringBuilder();
    final BufferedReader r =
        new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    try {
      for (int ch = r.read(); ch != -1; ch = r.read()) {
        sb.append((char) ch);
      }
      job.responseText = sb.toString();
    } finally {
      r.close();
    }
  }
Пример #6
0
 public static String login() throws Exception {
   // 登录
   URL url = new URL(loginUrl);
   String param =
       "__VIEWSTATE=%2FwEPDwUJNzE2NjczNjA4ZGQCaV1Dt0DOUg5r7O3ZD%2BsGL1Hq6Q%3D%3D&__EVENTVALIDATION=%2FwEWBQK%2F2uyqBgKl1bKzCQK1qbSRCwLZpaCZAwKTuvSuCTYE9T8nbPNCPEY2eqmCy5I3BwtG&txtUserName=asri&txtPassword=asri&loginBtn=%B5%C7%C2%BD";
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setDoInput(true);
   conn.setDoOutput(true);
   conn.setRequestMethod("POST");
   OutputStream out = conn.getOutputStream();
   out.write(param.getBytes());
   out.flush();
   out.close();
   String sessionId = "";
   String cookieVal = "";
   String key = null;
   // 取cookie
   for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
     if (key.equalsIgnoreCase("set-cookie")) {
       cookieVal = conn.getHeaderField(i);
       cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
       sessionId = sessionId + cookieVal + ";";
     }
   }
   return sessionId;
 }
Пример #7
0
  public static String getRealFileNameFromUrl(String url) {
    String name = null;
    try {

      URL mUrl = new URL(url);
      HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
      mHttpURLConnection.setConnectTimeout(5 * 1000);
      mHttpURLConnection.setRequestMethod("GET");
      mHttpURLConnection.setRequestProperty(
          "Accept",
          "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
      mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN");
      mHttpURLConnection.setRequestProperty("Referer", url);
      mHttpURLConnection.setRequestProperty("Charset", "UTF-8");
      mHttpURLConnection.setRequestProperty("User-Agent", "");
      mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive");
      mHttpURLConnection.connect();
      if (mHttpURLConnection.getResponseCode() == 200) {
        for (int i = 0; ; i++) {
          String mine = mHttpURLConnection.getHeaderField(i);
          if (mine == null) {
            break;
          }
          if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) {
            Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase());
            if (m.find()) return m.group(1).replace("\"", "");
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return name;
  }
Пример #8
0
  /**
   * 获取文件名
   *
   * @param conn
   * @return
   */
  private String getFileName(HttpURLConnection conn) {
    String filename = this.downloadUrl.substring(this.downloadUrl.lastIndexOf('/') + 1);

    if (filename == null || filename.trim().length() == 0) { // 如果获取不到文件名称
      for (int i = 0; ; i++) {
        String mine = conn.getHeaderField(i);

        if (mine == null) break;

        if ("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase())) {
          Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase());
          if (m.find()) {
            filename = m.group(1);
            break;
          }
        }
      }

      if (filename == null || filename.length() == 0) {
        filename = UUID.randomUUID() + ".tmp"; // 默认取一个文件名
      }
    }

    return filename;
  }
Пример #9
0
 // 获得文件长度
 public long getFileSize() {
   int nFileLength = -1;
   try {
     URL url = new URL(siteInfoBean.getSSiteURL());
     HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
     httpConnection.setRequestProperty("User-Agent", "NetFox");
     int responseCode = httpConnection.getResponseCode();
     if (responseCode >= 400) {
       processErrorCode(responseCode);
       return -2; // -2 represent access is error
     }
     String sHeader;
     for (int i = 1; ; i++) {
       // DataInputStream in = new
       // DataInputStream(httpConnection.getInputStream ());
       // Utility.log(in.readLine());
       sHeader = httpConnection.getHeaderFieldKey(i);
       if (sHeader != null) {
         if (sHeader.equals("Content-Length")) {
           nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));
           break;
         }
       } else break;
     }
   } catch (IOException e) {
     e.printStackTrace();
   } catch (Exception e) {
     e.printStackTrace();
   }
   Utility.log(nFileLength);
   return nFileLength;
 }
Пример #10
0
 /**
  * 获取Http响应头字段
  *
  * @param http
  * @return
  */
 public static LinkedHashMap<String, String> getHttpResponseHeader(HttpURLConnection http) {
   LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
   for (int i = 0; ; i++) {
     String mine = http.getHeaderField(i);
     if (mine == null) break;
     header.put(http.getHeaderFieldKey(i), mine);
   }
   return header;
 }
Пример #11
0
 // Deal with servers with "location" instead of "Location" in redirect
 // headers
 private InputStream getInputStream(URL url) throws IOException {
   int redirectLimit = 15;
   while (redirectLimit-- > 0) {
     HttpURLConnection con = (HttpURLConnection) url.openConnection();
     con.setInstanceFollowRedirects(false);
     con.connect();
     if (con.getResponseCode() == 200) {
       return con.getInputStream();
     }
     if (con.getResponseCode() > 300 && con.getResponseCode() > 399) {
       say(url + " gave resposneCode " + con.getResponseCode());
       throw new IOException();
     }
     url = null;
     for (int i = 0; i < 50; i++) {
       if (con.getHeaderFieldKey(i) == null) continue;
       // println
       // "key="+con.getHeaderFieldKey(i)+" field="+con.getHeaderField(i)
       if (con.getHeaderFieldKey(i).toLowerCase().equals("location")) {
         url = new URL(con.getHeaderField(i));
         // say("key=" + con.getHeaderFieldKey(i) + " field="
         // + con.getHeaderField(i));
       }
     }
     if (url == null) {
       say("Got 302 without Location");
       // String x = "";
       // for (int jj = 0; jj < 50; jj++) {
       // x += ", " + con.getHeaderFieldKey(jj);
       // }
       // say("headers " + x);
     }
     // println "next: "+url
   }
   throw new IOException(BaseActivity.getAppTitle() + " redirect limit reached");
 }
Пример #12
0
 synchronized void requestCheckCode(String site) {
   URL url = null;
   String cookieVal = null;
   String sessionId = "";
   String key = null;
   try {
     url = new URL(site);
     connection = (HttpURLConnection) url.openConnection();
     connection.addRequestProperty(
         "User-Agent",
         "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
     if (null != cookie && !cookie.toString().equals("")) {
       connection.setRequestProperty("Cookie", cookie.toString());
     }
     connection.setRequestMethod("GET");
     connection.connect();
     if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
       // 服务端响应成功
       if (null == cookie) {
         cookie = new StringBuilder();
         for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
           if (key.equalsIgnoreCase("set-cookie")) {
             cookieVal = connection.getHeaderField(i);
             cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
             sessionId = sessionId + cookieVal + ";";
           }
         }
         cookie.append(sessionId);
       }
       System.out.println("验证码获取的服务端SESSIONID:" + cookie.toString());
       try {
         InputStream is = connection.getInputStream();
         byte[] buffer = new byte[2048];
         int c;
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         while ((c = is.read(buffer)) != -1) {
           baos.write(buffer, 0, c);
           baos.flush();
         }
         baos.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #13
0
 /** 获取文件名 */
 private String getFileName(HttpURLConnection conn) {
   String filename = this.downloadUrl.substring(this.downloadUrl.lastIndexOf('/') + 1);
   if (filename == null || "".equals(filename.trim())) { // 如果获取不到文件名称
     for (int i = 0; ; i++) {
       String mine = conn.getHeaderField(i);
       if (mine == null) break;
       // Content-disposition是MIME协议的扩展,由于多方面的安全性考虑没有被标准化,所以可能某些浏览器不支持,比如说IE4.01
       if ("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase())) {
         Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase());
         if (m.find()) return m.group(1);
       }
     }
     // 全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的
     filename = UUID.randomUUID() + ".tmp"; // 默认取一个文件名
   }
   return filename;
 }
 private byte[] sendHttpMessage(byte[] body) throws IOException {
   HttpMessageContext context = HttpMessageContext.getInstance();
   context.Debug("GNHttpConnection [sendHttpMessage] starts");
   if (context.getLogheader()) {
     context.Info(logMessageSetting());
   }
   connection.setDoInput(true);
   if (body != null) {
     connection.setDoOutput(true);
     OutputStream os = TimedURLConnection.getOutputStream(connection, timeout * 1000);
     context.Debug("GNHttpConnection [sendHttpMessage] sending message ...");
     os.write(body);
     context.Debug("GNHttpConnection [sendHttpMessage] message sent");
   }
   context.Debug(
       "GNHttpConnection [sendHttpMessage] TimedURLConnection.getInputStream timeout["
           + timeout
           + " S]");
   InputStream is = TimedURLConnection.getInputStream(connection, timeout * 1000);
   responseCode = connection.getResponseCode();
   context.Debug("GNHttpConnection [sendHttpMessage] responseCode[" + responseCode + "]");
   responseMessage = HttpMessageContext.getMessage(is);
   responseheaders = new Hashtable<String, String>();
   int no = 0;
   while (true) {
     String headerName = connection.getHeaderFieldKey(no);
     String headerValue = connection.getHeaderField(no);
     if (headerName != null && headerName.length() > 0) {
       responseheaders.put(headerName, headerValue);
     } else {
       if (headerValue == null || headerValue.length() <= 0) break;
     }
     no++;
   }
   if (context.getLogheader()) {
     GTConfigFile head = new GTConfigFile(responseheaders);
     context.Debug("GNHttpConnection [sendHttpMessage] responseHeader\r\n" + head);
     context.Debug(
         "GNHttpConnection [sendHttpMessage] responseMessage\r\n"
             + new String(getResponseMessage()));
     context.Info("GNHttpConnection [sendHttpMessage] success for " + url);
   } else context.Info("GNHttpConnection [sendHttpMessage] success for " + url);
   return responseMessage;
 }
Пример #15
0
 private String getFileName(HttpURLConnection conn, String url) {
   String filename = null;
   if (url != null) {
     filename = url.substring(url.lastIndexOf('/') + 1);
   }
   if (filename == null || "".equals(filename.trim())) {
     for (int i = 0; ; i++) {
       String mine = conn.getHeaderField(i);
       if (mine == null) break;
       if ("content-disposition"
           .equals(conn.getHeaderFieldKey(i).toLowerCase(Locale.getDefault()))) {
         Matcher m =
             Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase(Locale.getDefault()));
         if (m.find()) return m.group(1);
       }
     }
     filename = UUID.randomUUID() + ".tmp";
   }
   return filename;
 }
Пример #16
0
  protected NSPResponse request(String httpurl, String data, Map<String, String> headers)
      throws NSPException {
    if (LOG.isDebugEnabled()) {
      LOG.debug("rquest:" + httpurl + "?" + data);
    }
    URL url = null;
    HttpURLConnection conn = null;
    try {
      url = new URL(httpurl);
      conn = (HttpURLConnection) url.openConnection();
    } catch (IOException e1) {
      throw new NSPException(2, "服务临时不可用");
    }
    HttpURLConnection.setFollowRedirects(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("accept-encoding", "gzip");
    if (headers != null) {
      for (Map.Entry<String, String> entry : headers.entrySet())
        conn.setRequestProperty(entry.getKey(), entry.getValue());
    }

    if (data != null) {
      OutputStreamWriter wr;
      try {
        wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        wr.write(data);
        wr.flush();
        wr.close();
      } catch (IOException e) {
        throw new NSPException(2, "服务临时不可用");
      }
    }

    boolean isGzip = false;
    NSPResponse response = new NSPResponse();
    for (int i = 0; ; i++) {
      String k = conn.getHeaderFieldKey(i);
      String v = conn.getHeaderField(i);
      if (k == null && v == null) break;

      if (k == null) {
        // HTTP Status
        response.setStatus(Integer.parseInt(v.split("\\s+")[1]));
      } else if (NSP_STATUS.equalsIgnoreCase(k)) {
        // NSP Status
        response.setCode(Integer.parseInt(v));
      } else {
        // Normal Header
        response.putHeader(k, v);
      }
      if (k != null
          && k.equalsIgnoreCase("Content-Encoding")
          && v != null
          && v.equalsIgnoreCase("gzip")) {
        isGzip = true;
      }
    }
    try {
      if (response.getStatus() == 200 || response.getStatus() == 302) {
        if (isGzip) {
          response.setContent(IOUtils.toByteArray(new GZIPInputStream(conn.getInputStream())));
        } else {
          response.setContent(IOUtils.toByteArray(conn.getInputStream()));
        }
      }
    } catch (IOException ioe) {
      throw new NSPException(2, "服务临时不可用");
    }

    if (response.getCode() > 0) {
      PHPSerializer phpSerializer = new PHPSerializer();
      Map<String, Object> map = null;
      try {
        map = NSPWrapper.toBean(phpSerializer.unserialize(response.getContent()), Map.class);
      } catch (Exception e) {
        throw new NSPException(2, "返回数据phprpc反序列化错误");
      }
      if (map == null) throw new NSPException(response.getCode(), "Unknown error");
      else if (map.containsKey("error")) {
        String errMsg = "";
        if (map.get("error") instanceof String) {
          errMsg = (String) map.get("error");
        } else if (map.get("error") instanceof byte[]) {
          errMsg = new String((byte[]) map.get("error"));
        }
      }
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug("response:" + new String(response.getContent()));
    }
    return response;
  }
Пример #17
0
  @SuppressWarnings("unchecked")
  protected Object execute(final Method method, final Object value) throws QueryException {
    Object result = value;
    URL location = getLocation();
    HttpURLConnection connection = null;

    Serializer<Object> serializerLocal = (Serializer<Object>) this.serializer;

    bytesSent = 0;
    bytesReceived = 0;
    bytesExpected = -1;

    status = 0;
    String message = null;

    try {
      // Clear any properties from a previous response
      responseHeaders.clear();

      // Open a connection
      if (proxy == null) {
        connection = (HttpURLConnection) location.openConnection();
      } else {
        connection = (HttpURLConnection) location.openConnection(proxy);
      }

      connection.setRequestMethod(method.toString());
      connection.setAllowUserInteraction(false);
      connection.setInstanceFollowRedirects(false);
      connection.setUseCaches(false);

      if (connection instanceof HttpsURLConnection && hostnameVerifier != null) {
        HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
        httpsConnection.setHostnameVerifier(hostnameVerifier);
      }

      // Set the request headers
      if (result != null) {
        connection.setRequestProperty("Content-Type", serializerLocal.getMIMEType(result));
      }

      for (String key : requestHeaders) {
        for (int i = 0, n = requestHeaders.getLength(key); i < n; i++) {
          if (i == 0) {
            connection.setRequestProperty(key, requestHeaders.get(key, i));
          } else {
            connection.addRequestProperty(key, requestHeaders.get(key, i));
          }
        }
      }

      // Set the input/output state
      connection.setDoInput(true);
      connection.setDoOutput(result != null);

      // Connect to the server
      connection.connect();
      queryListeners.connected(this);

      // Write the request body
      if (result != null) {
        OutputStream outputStream = null;
        try {
          outputStream = connection.getOutputStream();
          serializerLocal.writeObject(result, new MonitoredOutputStream(outputStream));
        } finally {
          if (outputStream != null) {
            outputStream.close();
          }
        }
      }

      // Notify listeners that the request has been sent
      queryListeners.requestSent(this);

      // Set the response info
      status = connection.getResponseCode();
      message = connection.getResponseMessage();

      // Record the content length
      bytesExpected = connection.getContentLength();

      // NOTE Header indexes start at 1, not 0
      int i = 1;
      for (String key = connection.getHeaderFieldKey(i);
          key != null;
          key = connection.getHeaderFieldKey(++i)) {
        responseHeaders.add(key, connection.getHeaderField(i));
      }

      // If the response was anything other than 2xx, throw an exception
      int statusPrefix = status / 100;
      if (statusPrefix != 2) {
        throw new QueryException(status, message);
      }

      // Read the response body
      if (method == Method.GET && status == Query.Status.OK) {
        InputStream inputStream = null;
        try {
          inputStream = connection.getInputStream();
          result = serializerLocal.readObject(new MonitoredInputStream(inputStream));
        } finally {
          if (inputStream != null) {
            inputStream.close();
          }
        }
      }

      // Notify listeners that the response has been received
      queryListeners.responseReceived(this);
    } catch (IOException exception) {
      queryListeners.failed(this);
      throw new QueryException(exception);
    } catch (SerializationException exception) {
      queryListeners.failed(this);
      throw new QueryException(exception);
    } catch (RuntimeException exception) {
      queryListeners.failed(this);
      throw exception;
    }

    return result;
  }
Пример #18
0
  /**
   * 计算文件大小, 并开启线程下载
   *
   * @param path
   * @param targetFile
   */
  private void doDownload(String path, String targetFile) {
    try {
      HttpURLConnection httpConnection = (HttpURLConnection) new URL(path).openConnection();
      httpConnection.setRequestMethod("HEAD");
      int responseCode = httpConnection.getResponseCode();
      if (responseCode >= 400) {
        Logg.out("Web服务器响应错误!");
        return;
      }
      String sHeader;
      for (int i = 1; ; i++) {
        sHeader = httpConnection.getHeaderFieldKey(i);
        if (sHeader != null && sHeader.equals("Content-Length")) {
          Logg.out("文件大小ContentLength:" + httpConnection.getContentLength());
          fileSize = Long.parseLong(httpConnection.getHeaderField(sHeader));
          break;
        }
      }
      httpConnection.disconnect();

      // 生成文件
      File newFile = new File(targetFile);
      RandomAccessFile raf = new RandomAccessFile(newFile, "rw");
      raf.setLength(fileSize);
      raf.close();

      // 计算需要多少个线程,并开启线程下载
      threadCount = (int) (fileSize / unitSize);

      Logg.out("共启动 " + (fileSize % unitSize == 0 ? threadCount : threadCount + 1) + " 个线程");

      if (fileSize > 20 * 1024 * 1000) { // 如果文件远大于20M, 建议使用单线程下载
        new SingleDownloadThread().start();
      } else {
        long offset = 0;
        if (fileSize <= unitSize) { // 如果远程文件尺寸小于等于unitSize
          downloadThreads = new DownloadThread[1];
          downloadThreads[0] = new DownloadThread(path, targetFile, offset, fileSize);
          downloadThreads[0].start();
        } else { // 如果远程文件尺寸大于unitSize
          if (fileSize % unitSize != 0) {
            downloadThreads = new DownloadThread[threadCount + 1];
          } else {
            downloadThreads = new DownloadThread[threadCount];
          }
          for (int i = 0; i < threadCount; i++) {
            downloadThreads[i] = new DownloadThread(path, targetFile, offset, unitSize);
            downloadThreads[i].start();
            offset = offset + unitSize;
          }
          if (fileSize % unitSize != 0) { // 如果不能整除,则需要再创建一个线程下载剩余字节
            downloadThreads[threadCount] =
                new DownloadThread(path, targetFile, offset, fileSize - unitSize * threadCount);
            downloadThreads[threadCount].start();
          }
        }
      }
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #19
0
  private String postMessage(URL url, String message) throws IOException {

    if (url == null) {
      throw new IOException("Bad URL : null");
    }

    /* Contact the server. */
    HttpURLConnection connection = null;
    String protocol = url.getProtocol().toLowerCase();

    if (protocol.equals("https")) {
      HttpsURLConnection sslConnection = (HttpsURLConnection) url.openConnection();
      sslConnection.setHostnameVerifier(new AutoVerifier());
      connection = sslConnection;
    } else {
      connection = (HttpURLConnection) url.openConnection();
    }

    connection.setDoOutput(true);
    connection.setDoInput(true);

    /*
     * if(requestContentType != null)
     * connection.setRequestProperty("Content-Type",requestContentType);
     *
     * if(setContentLength)
     * connection.setRequestProperty("Content-Length",Integer
     * .toString(message.length()));
     */
    connection.setRequestMethod("POST");
    /*
     * if(!Str.isEmpty(cookie)) {
     * connection.setRequestProperty("Cookie",cookie);
     * connection.setRequestProperty("Set-Cookie",cookie);
     * System.out.println("Setting cookie to " + cookie); }
     */

    StringBuffer buffer = new StringBuffer("");
    String agent = "Mozilla/4.0";
    String rawData = "userid=joe&password=guessme";
    String type = "application/x-www-form-urlencoded";
    BufferedReader reader = null;
    // String encodedData = StringFunctions.urlEncoded( message ); //
    // user-supplied

    try {
      /*
       * connection = (HttpConnection) Connector.open( url );
       * connection.setRequestMethod( HttpConnection.POST );
       * connection.setRequestProperty( "User-Agent", agent );
       * connection.setRequestProperty( "Content-Type", type );
       * connection.setRequestProperty( "Content-Length", message.length()
       * ); // OutputStream os = conn.openOutputStream(); // os.write(
       * message.getBytes() );
       */
      PrintWriter writer = new PrintWriter(connection.getOutputStream());
      writer.print(message);
      writer.close();

      InputStream in = connection.getInputStream();
      String replyContentType = connection.getContentType();
      int replyContentLength = connection.getContentLength();
      String cookie = connection.getHeaderField("set-cookie");

      // DEBUG
      for (int count = 0; ; count++) {
        String key = connection.getHeaderFieldKey(count + 1);
        if (key == null) break;
        String val = connection.getHeaderField(count + 1);
        System.out.println("header field " + key + "=" + val);
      }
      reader = new BufferedReader(new InputStreamReader(in));
      System.out.println("read input.....");
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println("in...." + line);
        buffer.append(line + "\n");
      }
    } finally {
      try {
        if (reader != null) reader.close();
      } catch (IOException e) {
      }
    }
    System.out.println("input....." + buffer.toString().trim());

    return buffer.toString().trim();
  }
Пример #20
0
  /**
   * @param ctxt The context.
   * @param urlString The URL to connect to.
   * @param parameters A collection of key value pairs to send along
   * @param cookieAction Whether we need to save or send a session cookie.
   * @param imageUri An image URI.
   * @param fileParameterName The key to send in the request.
   */
  public static String execute(
      Context ctxt,
      String urlString,
      HashMap<String, String> parameters,
      int cookieAction,
      Uri imageUri,
      String fileParameterName)
      throws IOException {
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;
    FileInputStream fileInputStream = null;
    URL url = new URL(urlString);
    String sResponse = "";

    byte[] buffer;
    int maxBufferSize = 20 * 1024;
    try {

      // Open a HTTP connection to the URL
      conn = (HttpURLConnection) url.openConnection();

      // Send cookie ?
      if (cookieAction == Common.SEND_COOKIE) {
        String drupoidCookie = Common.getPref(ctxt, "drupappCookie", "");
        conn.setRequestProperty("Cookie", drupoidCookie);
      }

      // Allow Inputs
      conn.setDoInput(true);
      // Allow Outputs
      conn.setDoOutput(true);
      // Don't use a cached copy.
      conn.setUseCaches(false);
      // Use a post method.
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
      conn.connect();

      dos = new DataOutputStream(conn.getOutputStream());

      // See if we need to upload a file.
      if (imageUri != null) {
        fileInputStream = (FileInputStream) ctxt.getContentResolver().openInputStream(imageUri);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes(
            "Content-Disposition: form-data; name=\""
                + fileParameterName
                + "\"; filename=\"drupapp.jpg\""
                + lineEnd);
        dos.writeBytes("Content-Type: text/xml" + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        buffer = new byte[Math.min(30, maxBufferSize)];
        int length;
        // read file and write it into form...
        while ((length = fileInputStream.read(buffer)) != -1) {
          dos.write(buffer, 0, length);
        }
      }

      // Basic data.
      for (String name : parameters.keySet()) {
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(parameters.get(name));
      }

      // Send form data necessary after file data.
      dos.writeBytes(lineEnd);
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
      dos.flush();

      if (cookieAction == Common.SAVE_COOKIE) {
        String headerName;
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
          if (headerName.equals("Set-Cookie")) {
            String cookie = conn.getHeaderField(i);
            cookie = cookie.substring(0, cookie.indexOf(";"));
            if (cookie.substring(0, 4).equals("SESS")) {
              Common.setPref(ctxt, "drupappCookie", cookie);
            }
          }
        }
      }
    } finally {
      if (fileInputStream != null) {
        fileInputStream.close();
      }
      if (dos != null) {
        dos.close();
      }
    }

    // Read the server response.
    try {
      dis = new DataInputStream(conn.getInputStream());
      StringBuilder response = new StringBuilder();

      String line;
      while ((line = dis.readLine()) != null) {
        response.append(line);
      }

      sResponse = response.toString();
    } finally {
      if (dis != null) {
        dis.close();
      }
    }

    return sResponse;
  }
Пример #21
0
  /**
   * Sending an api request through Http GET
   *
   * @param command command name
   * @param params command query parameters in a HashMap
   * @return http request response string
   */
  protected String sendRequest(String command, HashMap<String, String> params) {
    try {
      // Construct query string
      StringBuilder sBuilder = new StringBuilder();
      sBuilder.append("command=");
      sBuilder.append(command);
      if (params != null && params.size() > 0) {
        Iterator<String> keys = params.keySet().iterator();
        while (keys.hasNext()) {
          String key = keys.next();
          sBuilder.append("&");
          sBuilder.append(key);
          sBuilder.append("=");
          sBuilder.append(URLEncoder.encode(params.get(key), "UTF-8"));
        }
      }

      // Construct request url
      String reqUrl = rootUrl + "?" + sBuilder.toString();

      // Send Http GET request
      URL url = new URL(reqUrl);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");

      if (!command.equals("login") && cookieToSent != null) {
        // add the cookie to a request
        conn.setRequestProperty("Cookie", cookieToSent);
      }
      conn.connect();

      if (command.equals("login")) {
        // if it is login call, store cookie
        String headerName = null;
        for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
          if (headerName.equals("Set-Cookie")) {
            String cookie = conn.getHeaderField(i);
            cookie = cookie.substring(0, cookie.indexOf(";"));
            String cookieName = cookie.substring(0, cookie.indexOf("="));
            String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
            cookieToSent = cookieName + "=" + cookieValue;
          }
        }
      }

      // Get the response
      StringBuilder response = new StringBuilder();
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      try {
        while ((line = rd.readLine()) != null) {
          response.append(line);
        }
      } catch (EOFException ex) {
        // ignore this exception
        System.out.println("EOF exception due to java bug");
      }
      rd.close();

      return response.toString();

    } catch (Exception e) {
      throw new CloudRuntimeException("Problem with sending api request", e);
    }
  }
 public String getHeaderFieldKey(int i) {
   return _flddelegate.getHeaderFieldKey(i);
 }
Пример #23
0
  SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException {
    boolean isFailure = false;

    URL url = null;
    HttpURLConnection httpConnection = null;

    int responseCode = 0;
    try {
      if (endPoint.getProtocol().equals("https"))
        // if(!setHttps)
        initHttps();
      // Process the URL
      JaxmURI uri = new JaxmURI(endPoint.toString());
      String userInfo = uri.getUserinfo();

      url = endPoint;

      if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri);

      // TBD
      //    Will deal with https later.
      if (!url.getProtocol().equalsIgnoreCase("http")
          && !url.getProtocol().equalsIgnoreCase("https")) {
        log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
        throw new IllegalArgumentException(
            "Protocol " + url.getProtocol() + " not supported in URL " + url);
      }
      httpConnection = (HttpURLConnection) createConnection(url);

      httpConnection.setRequestMethod("POST");

      httpConnection.setDoOutput(true);
      httpConnection.setDoInput(true);
      httpConnection.setUseCaches(false);
      httpConnection.setInstanceFollowRedirects(true);

      if (message.saveRequired()) message.saveChanges();

      MimeHeaders headers = message.getMimeHeaders();

      Iterator it = headers.getAllHeaders();
      boolean hasAuth = false; // true if we find explicit Auth header
      while (it.hasNext()) {
        MimeHeader header = (MimeHeader) it.next();

        String[] values = headers.getHeader(header.getName());
        if (values.length == 1)
          httpConnection.setRequestProperty(header.getName(), header.getValue());
        else {
          StringBuffer concat = new StringBuffer();
          int i = 0;
          while (i < values.length) {
            if (i != 0) concat.append(',');
            concat.append(values[i]);
            i++;
          }

          httpConnection.setRequestProperty(header.getName(), concat.toString());
        }

        if ("Authorization".equals(header.getName())) {
          hasAuth = true;
          log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
        }
      }

      if (!hasAuth && userInfo != null) {
        initAuthUserInfo(httpConnection, userInfo);
      }

      OutputStream out = httpConnection.getOutputStream();
      message.writeTo(out);

      out.flush();
      out.close();

      httpConnection.connect();

      try {

        responseCode = httpConnection.getResponseCode();

        // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        }
        // else if (responseCode != HttpURLConnection.HTTP_OK)
        // else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207))
        else if ((responseCode / 100) != 2) {
          log.log(
              Level.SEVERE,
              "SAAJ0008.p2p.bad.response",
              new String[] {httpConnection.getResponseMessage()});
          throw new SOAPExceptionImpl(
              "Bad response: (" + responseCode + httpConnection.getResponseMessage());
        }
      } catch (IOException e) {
        // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
        responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        } else {
          throw e;
        }
      }

    } catch (SOAPException ex) {
      throw ex;
    } catch (Exception ex) {
      log.severe("SAAJ0009.p2p.msg.send.failed");
      throw new SOAPExceptionImpl("Message send failed", ex);
    }

    SOAPMessage response = null;
    if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
      try {
        MimeHeaders headers = new MimeHeaders();

        String key, value;

        // Header field 0 is the status line so we skip it.

        int i = 1;

        while (true) {
          key = httpConnection.getHeaderFieldKey(i);
          value = httpConnection.getHeaderField(i);

          if (key == null && value == null) break;

          if (key != null) {
            StringTokenizer values = new StringTokenizer(value, ",");
            while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim());
          }
          i++;
        }

        InputStream httpIn =
            (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream());

        byte[] bytes = readFully(httpIn);

        int length =
            httpConnection.getContentLength() == -1
                ? bytes.length
                : httpConnection.getContentLength();

        // If no reply message is returned,
        // content-Length header field value is expected to be zero.
        if (length == 0) {
          response = null;
          log.warning("SAAJ0014.p2p.content.zero");
        } else {
          ByteInputStream in = new ByteInputStream(bytes, length);
          response = messageFactory.createMessage(headers, in);
        }

        httpIn.close();
        httpConnection.disconnect();

      } catch (SOAPException ex) {
        throw ex;
      } catch (Exception ex) {
        log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex);
        throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage());
      }
    }
    return response;
  }
Пример #24
0
  SOAPMessage doGet(URL endPoint) throws SOAPException {
    boolean isFailure = false;

    URL url = null;
    HttpURLConnection httpConnection = null;

    int responseCode = 0;
    try {
      /// Is https GET allowed??
      if (endPoint.getProtocol().equals("https")) initHttps();
      // Process the URL
      JaxmURI uri = new JaxmURI(endPoint.toString());
      String userInfo = uri.getUserinfo();

      url = endPoint;

      if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri);

      // TBD
      //    Will deal with https later.
      if (!url.getProtocol().equalsIgnoreCase("http")
          && !url.getProtocol().equalsIgnoreCase("https")) {
        log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
        throw new IllegalArgumentException(
            "Protocol " + url.getProtocol() + " not supported in URL " + url);
      }
      httpConnection = (HttpURLConnection) createConnection(url);

      httpConnection.setRequestMethod("GET");

      httpConnection.setDoOutput(true);
      httpConnection.setDoInput(true);
      httpConnection.setUseCaches(false);
      httpConnection.setFollowRedirects(true);

      httpConnection.connect();

      try {

        responseCode = httpConnection.getResponseCode();

        // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        } else if ((responseCode / 100) != 2) {
          log.log(
              Level.SEVERE,
              "SAAJ0008.p2p.bad.response",
              new String[] {httpConnection.getResponseMessage()});
          throw new SOAPExceptionImpl(
              "Bad response: (" + responseCode + httpConnection.getResponseMessage());
        }
      } catch (IOException e) {
        // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
        responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        } else {
          throw e;
        }
      }

    } catch (SOAPException ex) {
      throw ex;
    } catch (Exception ex) {
      log.severe("SAAJ0012.p2p.get.failed");
      throw new SOAPExceptionImpl("Get failed", ex);
    }

    SOAPMessage response = null;
    if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
      try {
        MimeHeaders headers = new MimeHeaders();

        String key, value;

        // Header field 0 is the status line so we skip it.

        int i = 1;

        while (true) {
          key = httpConnection.getHeaderFieldKey(i);
          value = httpConnection.getHeaderField(i);

          if (key == null && value == null) break;

          if (key != null) {
            StringTokenizer values = new StringTokenizer(value, ",");
            while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim());
          }
          i++;
        }

        InputStream httpIn =
            (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream());
        // If no reply message is returned,
        // content-Length header field value is expected to be zero.
        // java SE 6 documentation says :
        // available() : an estimate of the number of bytes that can be read
        // (or skipped over) from this input stream without blocking
        // or 0 when it reaches the end of the input stream.
        if ((httpIn == null)
            || (httpConnection.getContentLength() == 0)
            || (httpIn.available() == 0)) {
          response = null;
          log.warning("SAAJ0014.p2p.content.zero");
        } else {
          response = messageFactory.createMessage(headers, httpIn);
        }

        httpIn.close();
        httpConnection.disconnect();

      } catch (SOAPException ex) {
        throw ex;
      } catch (Exception ex) {
        log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex);
        throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage());
      }
    }
    return response;
  }
 /**
  * Returns the key for the nth header field. Some implementations may treat the 0th header field
  * as special, i.e. as the status line returned by the HTTP server. In this case,
  * getHeaderField(0) returns the status line, but getHeaderFieldKey(0) returns null.
  */
 public String getHeaderFieldKey(int num) throws IOException {
   if (conn == null) {
     throw new IOException("Cannot open output stream on non opened connection");
   }
   return conn.getHeaderFieldKey(num);
 }