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);
  }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
  private String postResult(String URL) {
    StringBuffer sb = new StringBuffer();
    try {

      String finalUrl = "";

      String[] parsedUrl = URL.split("\\?");
      String params =
          URLEncoder.encode(parsedUrl[1], "UTF-8").replace("%3D", "=").replace("%26", "&");

      URL url = new URL(parsedUrl[0] + "?" + params);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Accept", "application/json");
      conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());

      conn.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
      wr.writeBytes(params);
      wr.flush();
      wr.close();

      boolean redirect = false;
      // normally, 3xx is redirect
      int status = conn.getResponseCode();
      if (status != HttpURLConnection.HTTP_OK) {
        if (status == HttpURLConnection.HTTP_MOVED_TEMP
            || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER) redirect = true;
      }

      if (redirect) {

        // get redirect url from "location" header field
        String newUrl = conn.getHeaderField("Location");

        // open the new connnection again
        conn = (HttpURLConnection) new URL(newUrl).openConnection();
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());
      }

      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;

      while ((inputLine = in.readLine()) != null) {
        sb.append(inputLine);
      }
      in.close();

    } catch (Exception e) {
      e.printStackTrace();
    }

    return sb.toString();
  }
Ejemplo n.º 4
0
  public File download(String songId) {
    try {
      String maxRate = getMaxRate(songId);
      JSONObject songInfo = getSongInfo(songId);
      // 以歌手名字+歌曲名称组成文件名,格式:歌手 - 歌曲名称
      String filename = songInfo.getString("artistName") + " - " + songInfo.getString("songName");

      String link =
          "http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds="
              + songId
              + "&rate="
              + maxRate;

      URL urlObject = new URL(link);
      HttpURLConnection httpUrlConnection = (HttpURLConnection) urlObject.openConnection();
      httpUrlConnection.setRequestMethod("GET");
      httpUrlConnection.setDoOutput(true);
      httpUrlConnection.setRequestProperty("Cookie", cookieValue);
      String disposition = httpUrlConnection.getHeaderField("Content-Disposition");
      disposition = disposition.replaceAll("\"", "");
      // 此转码经测试发现有些是UTF-8编码,有些是GBK编码,所以文件名采用另外方式获得
      // disposition = new String(disposition.getBytes("iso-8859-1"),"UTF-8");

      // 根据disposition中信息确定歌曲格式
      String suffix = disposition.substring(disposition.lastIndexOf("."));
      // System.out.println(disposition);

      InputStream inputStream = httpUrlConnection.getInputStream();

      File file = new File(downloadDirectory + "/" + filename + suffix);
      FileOutputStream fos = new FileOutputStream(file);

      byte[] buf = new byte[4096];
      int read = 0;
      while ((read = inputStream.read(buf)) > 0) {
        fos.write(buf, 0, read);
      }
      fos.flush();
      fos.close();
      inputStream.close();
      // System.out.println("完成<"+file.getName()+">歌曲下载!");
      return file;

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Ejemplo n.º 5
0
  private int invokeServlet(String url, String method, String user, StringBuffer output)
      throws Exception {
    String httpMethod = "GET";
    if ((method != null) && (method.length() > 0)) httpMethod = method;
    System.out.println("Invoking servlet with HTTP method: " + httpMethod);
    URL u = new URL(url);
    HttpURLConnection c1 = (HttpURLConnection) u.openConnection();
    c1.setRequestMethod(httpMethod);
    if ((user != null) && (user.length() > 0)) {
      // Add BASIC header for authentication
      String auth = user + ":" + password;
      String authEncoded = new sun.misc.BASE64Encoder().encode(auth.getBytes());
      c1.setRequestProperty("Authorization", "Basic " + authEncoded);
    }
    c1.setUseCaches(false);

    // Connect and get the response code and/or output to verify
    c1.connect();
    int code = c1.getResponseCode();
    if (code == HttpURLConnection.HTTP_OK) {
      InputStream is = null;
      BufferedReader input = null;
      String line = null;
      try {
        is = c1.getInputStream();
        input = new BufferedReader(new InputStreamReader(is));
        while ((line = input.readLine()) != null) {
          output.append(line);
          // System.out.println(line);
        }
      } finally {
        try {
          if (is != null) is.close();
        } catch (Exception exc) {
        }
        try {
          if (input != null) input.close();
        } catch (Exception exc) {
        }
      }
    } else if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
      URL redir = new URL(c1.getHeaderField("Location"));
      String line = "Servlet redirected to: " + redir.toString();
      output.append(line);
      System.out.println(line);
    }
    return code;
  }
 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;
 }
Ejemplo n.º 7
0
 /**
  * Verify the response code that the server returns
  *
  * @param desiredResponseCode
  * @throws IOException
  */
 @Given("^I should get response code (\\d+)$")
 public void HTTPGetInterogationServerResponse(int desiredResponseCode) throws IOException {
   System.out.println("Server response:  " + conn.getHeaderField(0));
   Assert.assertTrue(conn.getHeaderField(0).contains(String.valueOf(desiredResponseCode)));
 }
Ejemplo n.º 8
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;
  }
Ejemplo n.º 9
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;
  }
Ejemplo n.º 10
0
  /**
   * 登陆百度,其他方法调用之前需要先登陆
   *
   * @param username
   * @param password
   */
  public void login(String username, String password) {
    try {
      URL url = new URL("http://www.baidu.com/");
      HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
      httpUrlConnection.setRequestMethod("GET");
      String cookie1 = httpUrlConnection.getHeaderField("Set-Cookie");
      // System.out.println("cookie1:"+cookie1);
      cookie1 = cookie1.substring(0, 45);

      url = new URL("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");
      httpUrlConnection = (HttpURLConnection) url.openConnection();
      httpUrlConnection.setRequestMethod("GET");
      httpUrlConnection.setRequestProperty("Cookie", cookie1);
      // httpUrlConnection.connect();
      String cookie2 = httpUrlConnection.getHeaderField("Set-Cookie");
      System.out.println("cookie2:" + cookie2);
      cookie2 = cookie2.substring(0, 11);
      String response = getResponse(httpUrlConnection.getInputStream());
      // System.out.println(response);
      Pattern pattern = Pattern.compile("token='(\\w+)'");
      Matcher matcher = pattern.matcher(response);
      matcher.find();
      String token = matcher.group(1);

      url = new URL("https://passport.baidu.com/v2/api/?login");
      httpUrlConnection = (HttpURLConnection) url.openConnection();
      httpUrlConnection.setRequestMethod("POST");
      // System.out.println(cookie1+"; "+cookie2);
      httpUrlConnection.setRequestProperty("Cookie", cookie1 + "; " + cookie2);
      httpUrlConnection.setDoOutput(true);
      // System.out.println(token);

      String querystring =
          "loginType=1&tpl=mn&token="
              + token
              + "&username="******"&password="******"&mem_pass=on";
      httpUrlConnection.getOutputStream().write(querystring.getBytes());
      httpUrlConnection.getOutputStream().flush();
      httpUrlConnection.getOutputStream().close();

      response = getResponse(httpUrlConnection.getInputStream());
      // System.out.println(response);

      // String cookie3=httpUrlConnection.getHeaderField("Set-Cookie");
      // System.out.println("cookie3:"+cookie3);

      // 获取登陆后的cookie
      Map<String, List<String>> hfs = httpUrlConnection.getHeaderFields();
      List<String> loginCookies = hfs.get("Set-Cookie");
      for (String cookie : loginCookies) {
        cookieValue += cookie.substring(0, cookie.indexOf(";") + 1);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }