예제 #1
0
  @PostConstruct
  public void afterPropertiesSet() throws Exception {
    Protocol.registerProtocol("http", new Protocol("http", new OSProtocolSocketFactory(), 80));

    httpClient = new HttpClient();
    httpClient.setTimeout(30000);
    HostConfiguration conf = new HostConfiguration();
    conf.setHost(new URI("http://weibo.com"));
    httpClient.setHostConfiguration(conf);

    httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    httpClient.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
    httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    //		 if(null == System.getProperty("env"))
    //			 if(true)
    //		 return;
    //		System.out.println(System.getProperty("env"));
    logger.info(System.getProperty("env"));
    intervalCheck();
    // ck =
    // "__utma=15428400.1695645915.1351319378.1351319378.1351319378.1;
    // SINAGLOBAL=1743311276659.3694.1359040897731; ssoln=18260402168%40139.com; myuid=2350795254;
    // [email protected]; wvr=5; SinaRot_wb_r_topic=84; UV5PAGE=usr511_196;
    // SUE=es%3D6f93acac95733bd5c77d2752300965da%26ev%3Dv1%26es2%3D1be20b8b8e69fb068e7a0ba6fc8ceae4%26rs0%3Dm9CUmlvoVqKlPMc7pNcK6xtfel75%252FhJveinSyZsCdQb1ruTbsb9Csi6Qn3BdmBVdshCUdX7K%252BMwYsznrL51YNDAYNBMcuMbDFtT7g4DO7rFh9C3OolCF%252FpY5eaUwHJj8X80HLgfZzpYq09wgOtACYCKIZu%252FlcRdvO0cFdL3TAQQ%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1384174580%26et%3D1384260980%26d%3Dc909%26i%3Da6d4%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D2%26st%3D0%26uid%3D3807150373%26name%3Djobpassion%2540gmail.com%26nick%3D%25E6%25B5%25B7%25E6%25B7%2598%25E8%25B5%2584%25E8%25AE%25AF2013%26fmp%3D%26lcp%3D2013-10-08%252021%253A20%253A24; SUS=SID-3807150373-1384174580-XD-vc5by-adf6a9ecb6d31faa79c1aeb9e72cc849; ALF=1386766580; SSOLoginState=1384174580; UUG=usrmdins41456; _s_tentry=login.sina.com.cn; UOR=www.juyouqu.com,widget.weibo.com,login.sina.com.cn; Apache=1184292640537.0234.1384174581147; ULV=1384174581208:100:8:2:1184292640537.0234.1384174581147:1384080610846; UV5=usrmdins312_148";
    // ck = null;
    // URL url = new URL("http://www.baidu.com");
    // URLConnection connection = url.openConnection();
    // connection.connect();
    // System.out.println(IOUtils.readLines(connection.getInputStream()));
  }
  public static String getTinyUrl(String fullUrl) {
    HttpClient httpclient = new HttpClient();
    httpclient.setConnectionTimeout(TIMEOUT);
    httpclient.setTimeout(TIMEOUT);
    HttpMethod method = new GetMethod("http://tinyurl.com/api-create.php");
    method.setQueryString(new NameValuePair[] {new NameValuePair("url", fullUrl)});
    String tinyUrl = fullUrl;

    try {
      httpclient.executeMethod(method);
      tinyUrl = method.getResponseBodyAsString();
      method.releaseConnection();
    } catch (HttpException e) {
      method.releaseConnection();
      // implement Liferay logging service
    } catch (IOException e) {
      method.releaseConnection();
      // implement Liferay logging service
    } catch (Exception e) {
      method.releaseConnection();
      // implement Liferay logging service
    }

    return tinyUrl;
  }
예제 #3
0
  /** Handles the HTTP post. Throws HttpException on failure */
  @SuppressWarnings("deprecation")
  private void doPost(PostMethod method, RequestEntity data, String dest)
      throws IOException, HttpException {

    HttpMethodParams pars = method.getParams();
    pars.setParameter(
        HttpMethodParams.RETRY_HANDLER,
        (Object)
            new HttpMethodRetryHandler() {
              public boolean retryMethod(HttpMethod m, IOException e, int exec) {
                return !(e instanceof java.net.ConnectException)
                    && (exec < MAX_RETRIES_PER_COLLECTOR);
              }
            });
    method.setParams(pars);
    method.setPath(dest);

    // send it across the network
    method.setRequestEntity(data);

    log.info("HTTP post to " + dest + " length = " + data.getContentLength());
    // Send POST request

    client.setTimeout(8000);
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
      log.error(
          "HTTP post response statusCode: "
              + statusCode
              + ", statusLine: "
              + method.getStatusLine());
      // do something aggressive here
      throw new HttpException("got back a failure from server");
    }
    // implicitly "else"
    log.info(
        "got success back from the remote collector; response length "
            + method.getResponseContentLength());

    // FIXME: should parse acks here
    InputStream rstream = null;

    // Get the response body
    byte[] resp_buf = method.getResponseBody();
    rstream = new ByteArrayInputStream(resp_buf);
    BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println("response: " + line);
    }
  }
예제 #4
0
  public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    // extract required parameters
    String urlStr = (String) workItem.getParameter("Url");
    String method = (String) workItem.getParameter("Method");
    if (urlStr == null) {
      throw new IllegalArgumentException("Url is a required parameter");
    }
    if (method == null || method.trim().length() == 0) {
      method = "GET";
    }
    Map<String, Object> params = workItem.getParameters();

    // optional timeout config parameters, defaulted to 60 seconds
    Integer connectTimeout = (Integer) params.get("ConnectTimeout");
    if (connectTimeout == null) connectTimeout = 60000;
    Integer readTimeout = (Integer) params.get("ReadTimeout");
    if (readTimeout == null) readTimeout = 60000;

    HttpClient httpclient = new HttpClient();
    httpclient.setConnectionTimeout(connectTimeout);
    httpclient.setTimeout(readTimeout);

    HttpMethod theMethod = null;
    if ("GET".equals(method)) {
      theMethod = new GetMethod(urlStr);
    } else if ("POST".equals(method)) {
      theMethod = new PostMethod(urlStr);
    }
    doAuthorization(httpclient, theMethod, params);
    try {
      int responseCode = httpclient.executeMethod(theMethod);
      Map<String, Object> results = new HashMap<String, Object>();
      if (responseCode >= 200 && responseCode < 300) {
        theMethod.getResponseBody();
        postProcessResult(theMethod.getResponseBodyAsString(), results);
      } else {
        results.put("Status", responseCode);
        results.put(
            "StatusMsg",
            "endpoint " + urlStr + " could not be reached: " + theMethod.getResponseBodyAsString());
      }
      // notify manager that work item has been completed
      manager.completeWorkItem(workItem.getId(), results);
    } catch (Exception e) {
      e.printStackTrace();
      manager.abortWorkItem(workItem.getId());
    } finally {
      theMethod.releaseConnection();
    }
  }
예제 #5
0
  /**
   * @param since last modified time to use
   * @param req
   * @param url if null, ignored
   * @param redirCount number of redirs we've done
   */
  public static HttpData getDataOnce(
      HttpServletRequest req,
      HttpServletResponse res,
      long since,
      String surl,
      int redirCount,
      int timeout)
      throws IOException, HttpException, DataSourceException, MalformedURLException {

    HttpMethodBase request = null;
    HostConfiguration hcfg = new HostConfiguration();

    /*
      [todo hqm 2006-02-01] Anyone know why this code was here? It is setting
      the mime type to something which just confuses the DHTML parser.

      if (res != null) {
        res.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
        }
    */

    try {

      // TODO: [2002-01-09 bloch] cope with cache-control
      // response headers (no-store, no-cache, must-revalidate,
      // proxy-revalidate).

      if (surl == null) {
        surl = getURL(req);
      }
      if (surl == null || surl.equals("")) {
        throw new MalformedURLException(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="url is empty or null"
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-312"));
      }

      String reqType = "";
      String headers = "";

      if (req != null) {
        reqType = req.getParameter("reqtype");
        headers = req.getParameter("headers");
      }

      boolean isPost = false;
      mLogger.debug("reqtype = " + reqType);

      if (reqType != null && reqType.equals("POST")) {
        request = new LZPostMethod();
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        isPost = true;
        mLogger.debug("setting POST req method");
      } else if (reqType != null && reqType.equals("PUT")) {
        request = new LZPutMethod();
        // todo [hqm 2007] treat PUT like POST?
        isPost = true;
        mLogger.debug("setting PUT req method");
      } else if (reqType != null && reqType.equals("DELETE")) {
        request = new LZDeleteMethod();
        mLogger.debug("setting DELETE req method");
      } else {
        mLogger.debug("setting GET (default) req method");
        request = new LZGetMethod();
      }

      request.setHttp11(mUseHttp11);

      // Proxy the request headers
      if (req != null) {
        LZHttpUtils.proxyRequestHeaders(req, request);
      }

      // Set headers from query string
      if (headers != null && headers.length() > 0) {
        StringTokenizer st = new StringTokenizer(headers, "\n");
        while (st.hasMoreTokens()) {
          String h = st.nextToken();
          int i = h.indexOf(":");
          if (i > -1) {
            String n = h.substring(0, i);
            String v = h.substring(i + 2, h.length());
            request.setRequestHeader(n, v);
            mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="setting header " + p[0] + "=" + p[1]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(
                    HTTPDataSource.class.getName(), "051018-359", new Object[] {n, v}));
          }
        }
      }

      mLogger.debug("Parsing url");
      URI uri = LZHttpUtils.newURI(surl);
      try {
        hcfg.setHost(uri);
      } catch (Exception e) {
        throw new MalformedURLException(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="can't form uri from " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-376", new Object[] {surl}));
      }

      // This gets us the url-encoded (escaped) path and query string
      String path = uri.getEscapedPath();
      String query = uri.getEscapedQuery();
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="encoded path:  " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-389", new Object[] {path}));
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="encoded query: " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-397", new Object[] {query}));

      // This call takes a decoded (unescaped) path
      request.setPath(path);

      boolean hasQuery = (query != null && query.length() > 0);

      String rawcontent = null;
      // Newer rawpost protocol puts lzpostbody as a separate
      // top level query arg in the request.
      rawcontent = req.getParameter("lzpostbody");

      if (isPost) {
        // Older rawpost protocol put the "lzpostbody" arg
        // embedded in the "url" args's query args
        if (rawcontent == null && hasQuery) {
          rawcontent = findQueryArg("lzpostbody", query);
        }
        if (rawcontent != null) {
          // Get the unescaped query string
          ((EntityEnclosingMethod) request).setRequestBody(rawcontent);
        } else if (hasQuery) {
          StringTokenizer st = new StringTokenizer(query, "&");
          while (st.hasMoreTokens()) {
            String it = st.nextToken();
            int i = it.indexOf("=");
            if (i > 0) {
              String n = it.substring(0, i);
              String v = it.substring(i + 1, it.length());
              // POST encodes values during request
              ((PostMethod) request).addParameter(n, URLDecoder.decode(v, "UTF-8"));
            } else {
              mLogger.warn(
                  /* (non-Javadoc)
                   * @i18n.test
                   * @org-mes="ignoring bad token (missing '=' char) in query string: " + p[0]
                   */
                  org.openlaszlo.i18n.LaszloMessages.getMessage(
                      HTTPDataSource.class.getName(), "051018-429", new Object[] {it}));
            }
          }
        }
      } else {
        // This call takes an encoded (escaped) query string
        request.setQueryString(query);
      }

      // Put in the If-Modified-Since headers
      if (since != -1) {
        String lms = LZHttpUtils.getDateString(since);
        request.setRequestHeader(LZHttpUtils.IF_MODIFIED_SINCE, lms);
        mLogger.debug(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="proxying lms: " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-450", new Object[] {lms}));
      }

      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="setting up http client"
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-460"));
      HttpClient htc = null;
      if (mConnectionMgr != null) {
        htc = new HttpClient(mConnectionMgr);
      } else {
        htc = new HttpClient();
      }

      htc.setHostConfiguration(hcfg);

      // This is the data timeout
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="timeout set to " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-478", new Object[] {new Integer(timeout)}));
      htc.setTimeout(timeout);

      // Set connection timeout the same
      htc.setConnectionTimeout(mConnectionTimeout);

      // Set timeout for getting a connection
      htc.setHttpConnectionFactoryTimeout(mConnectionPoolTimeout);

      // TODO: [2003-03-05 bloch] this should be more configurable (per app?)
      if (!isPost) {
        request.setFollowRedirects(mFollowRedirects > 0);
      }

      long t1 = System.currentTimeMillis();
      mLogger.debug("starting remote request");
      int rc = htc.executeMethod(hcfg, request);
      String status = HttpStatus.getStatusText(rc);
      if (status == null) {
        status = "" + rc;
      }
      mLogger.debug(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="remote response status: " + p[0]
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(), "051018-504", new Object[] {status}));

      HttpData data = null;
      if (isRedirect(rc) && mFollowRedirects > redirCount) {
        String loc = request.getResponseHeader("Location").toString();
        String hostURI = loc.substring(loc.indexOf(": ") + 2, loc.length());
        mLogger.info(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="Following URL from redirect: " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                HTTPDataSource.class.getName(), "051018-517", new Object[] {hostURI}));
        long t2 = System.currentTimeMillis();
        if (timeout > 0) {
          timeout -= (t2 - t1);
          if (timeout < 0) {
            throw new InterruptedIOException(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes=p[0] + " timed out after redirecting to " + p[1]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(
                    HTTPDataSource.class.getName(), "051018-529", new Object[] {surl, loc}));
          }
        }

        data = getDataOnce(req, res, since, hostURI, redirCount++, timeout);
      } else {
        data = new HttpData(request, rc);
      }

      if (req != null && res != null) {
        // proxy response headers
        LZHttpUtils.proxyResponseHeaders(request, res, req.isSecure());
      }

      return data;

    } catch (ConnectTimeoutException ce) {
      // Transduce to an InterrupedIOException, since lps takes these to be timeouts.
      if (request != null) {
        request.releaseConnection();
      }
      throw new InterruptedIOException(
          /* (non-Javadoc)
           * @i18n.test
           * @org-mes="connecting to " + p[0] + ":" + p[1] + " timed out beyond " + p[2] + " msecs."
           */
          org.openlaszlo.i18n.LaszloMessages.getMessage(
              HTTPDataSource.class.getName(),
              "051018-557",
              new Object[] {
                hcfg.getHost(), new Integer(hcfg.getPort()), new Integer(mConnectionTimeout)
              }));
    } catch (HttpRecoverableException hre) {
      if (request != null) {
        request.releaseConnection();
      }
      throw hre;
    } catch (HttpException e) {
      if (request != null) {
        request.releaseConnection();
      }
      throw e;
    } catch (IOException ie) {
      if (request != null) {
        request.releaseConnection();
      }
      throw ie;
    } catch (RuntimeException e) {
      if (request != null) {
        request.releaseConnection();
      }
      throw e;
    }
  }
예제 #6
0
  public void initCookie() {

    // try {
    // System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
    // driver = new ChromeDriver();
    // driver.get("http://weibo.com/");
    // WebElement e = driver.findElement(By.name("username"));
    // e.sendKeys(account.getUsername());
    // e = driver.findElement(By.name("password"));
    // e.sendKeys(account.getPassword());
    // e = driver.findElement(By
    // .xpath("//span[@node-type='submitStates']"));
    // e.click();
    // Set<Cookie> cookies = driver.manage().getCookies();
    // // httpClient.startSession(new URL("http://weibo.com"));
    // HttpState state = new HttpState();
    // String ck = "";
    // for (Cookie c : cookies) {
    // ck += c.getName() + "=" + c.getValue() + "; ";
    // }
    // account.setCookie(ck);
    // driver.close();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    try {
      HttpClient httpClient = new HttpClient();
      httpClient.setTimeout(30000);
      HostConfiguration conf = new HostConfiguration();
      conf.setHost(new URI("http://www.weibo.com"));
      httpClient.setHostConfiguration(conf);
      httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
      httpClient.getParams().setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
      httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
      GetMethod method =
          new GetMethod(
              "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.11)&_=1387711657507");
      httpClient.executeMethod(method);
      String s = method.getResponseBodyAsString();
      int idx;
      idx = s.indexOf("servertime\":") + "servertime\":".length();
      String servertime = s.substring(idx, s.indexOf(",", idx));
      idx = s.indexOf("nonce\":\"") + "nonce\":\"".length();
      String nonce = s.substring(idx, s.indexOf("\"", idx));
      idx = s.indexOf("pubkey\":\"") + "pubkey\":\"".length();
      String pubkey = s.substring(idx, s.indexOf("\"", idx));
      idx = s.indexOf("rsakv\":\"") + "rsakv\":\"".length();
      String rsakv = s.substring(idx, s.indexOf("\"", idx));
      PostMethod post =
          new PostMethod("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.11)");

      final WebClient webClient = new WebClient();
      HtmlPage page = webClient.getPage("http://www.baidu.com/");
      InputStream is = Testweibo.class.getClassLoader().getResourceAsStream("weibo.js");
      String script = IOUtils.toString(is);
      is.close();
      ScriptResult sr = page.executeJavaScript(script);
      script =
          "sinaSSOController.rsaPubkey = '"
              + pubkey
              + "';sinaSSOController.servertime = '"
              + servertime
              + "';sinaSSOController.nonce = '"
              + nonce
              + "';sinaSSOController.rsakv = '"
              + rsakv
              + "';sinaSSOController.from = 'weibo';sinaSSOController.useTicket = 1;";
      sr = page.executeJavaScript(script);
      script =
          "sinaSSOController.login('"
              + account.getUsername()
              + "','"
              + account.getPassword()
              + "',7);";
      sr = page.executeJavaScript(script);
      NativeObject no = (NativeObject) sr.getJavaScriptResult();

      for (Object o : no.getAllIds()) {
        // System.out.println(o + "=>" + no.get(o));
        post.setParameter(o + "", no.get(o) + "");
      }
      webClient.closeAllWindows();
      post.setParameter("gateway", "1");
      post.setParameter("savestate", "7");
      post.setParameter("useticket", "1");
      post.setParameter(
          "pagerefer",
          "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http%3A%2F%2Fweibo.com%2Flogout.php%3Fbackurl%3D%252F");
      post.setParameter("vsnf", "1");
      post.setParameter("encoding", "UTF-8");
      post.setParameter("prelt", "140");
      post.setParameter(
          "url",
          "http://www.weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack");
      post.setParameter("returntype", "META");
      httpClient.executeMethod(post);
      s = new String(post.getResponseBody(), "UTF-8");
      idx = s.indexOf("location.replace('") + "location.replace('".length();
      String url = s.substring(idx, s.indexOf("'", idx));
      //			System.out.println(url);
      logger.debug(url);
      method = new GetMethod(url);
      httpClient.executeMethod(method);
      ;
      //			System.out.println(method.getResponseBodyAsString());
      logger.debug(method.getResponseBodyAsString());
      //			 method = new GetMethod("http://weibo.com/");
      //			 httpClient.executeMethod(method);
      //			 System.out.println(method.getURI());
      //			 System.out.println(new String(method.getResponseBody(),"UTF-8"));

      //			String ck = "";
      //			for (Cookie c : httpClient.getState().getCookies()) {
      ////				if(c.getDomain().equals(".weibo.com")||c.getDomain().equals("weibo.com"))
      //				ck += c.getName() + "=" + c.getValue() + "; ";
      //			}
      //			account.setCookie(ck);
      clientMap.put(account.getUsername(), httpClient);

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