Пример #1
0
 private void configureHttpClient() {
   MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
   HttpConnectionManagerParams connParams = connMgr.getParams();
   connParams.setMaxTotalConnections(maxConnections);
   connParams.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxConnections);
   connMgr.setParams(connParams);
   hc = new HttpClient(connMgr);
   // NOTE: These didn't seem to help in my initial testing
   //			hc.getParams().setParameter("http.tcp.nodelay", true);
   //			hc.getParams().setParameter("http.connection.stalecheck", false);
   if (proxyHost != null) {
     HostConfiguration hostConfig = new HostConfiguration();
     hostConfig.setProxy(proxyHost, proxyPort);
     hc.setHostConfiguration(hostConfig);
     log.info("Proxy Host set to " + proxyHost + ":" + proxyPort);
     if (proxyUser != null && !proxyUser.trim().equals("")) {
       if (proxyDomain != null) {
         hc.getState()
             .setProxyCredentials(
                 new AuthScope(proxyHost, proxyPort),
                 new NTCredentials(proxyUser, proxyPassword, proxyHost, proxyDomain));
       } else {
         hc.getState()
             .setProxyCredentials(
                 new AuthScope(proxyHost, proxyPort),
                 new UsernamePasswordCredentials(proxyUser, proxyPassword));
       }
     }
   }
 }
Пример #2
0
  private HttpClient getClient() {
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setParameter("http.protocol.content-charset", "gbk");
    httpClient.getParams().setSoTimeout(timeout);

    if (useSSL) {
      if (sslPort > 0) {
        Protocol myhttps =
            new Protocol(
                "https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), sslPort);
        Protocol.registerProtocol("https", myhttps);
      }
    }

    if (useProxy) {
      HostConfiguration hc = new HostConfiguration();
      hc.setProxy(proxyUrl, proxyPort);
      httpClient.setHostConfiguration(hc);
      if (proxyUser != null) {
        httpClient
            .getState()
            .setProxyCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword));
      }
    }

    return httpClient;
  }
Пример #3
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()));
  }
Пример #4
0
 /**
  * Allows to initialize the instance of {@link #httpClient} by setting the configuration of proxy
  */
 public void initialize(@NonNull String baseUrl) {
   EasyCukesConfiguration<CommonConfigurationBean> configuration =
       new EasyCukesConfiguration<>(CommonConfigurationBean.class);
   if (configuration.isProxyNeeded())
     httpClient.setHostConfiguration(configuration.configureProxy());
   configuration.configureSSL();
   this.baseUrl = baseUrl;
 }
Пример #5
0
 private HttpClient createHttpClient() {
   HttpClient client = new HttpClient();
   client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
   client.getParams().setConnectionManagerTimeout(timeout);
   if (proxy) {
     HostConfiguration hcf = new HostConfiguration();
     hcf.setProxy(this.proxyUrl, this.proxyPort);
     client.setHostConfiguration(hcf);
   }
   return client;
 }
Пример #6
0
 public TrackerImpl(Peer localPeer, URL announceUrl, SHAHash infoHash) {
   this.announceUrl = announceUrl;
   this.infoHash = infoHash;
   this.localPeer = localPeer;
   try {
     httpClient = new HttpClient();
     HostConfiguration configuration = new HostConfiguration();
     configuration.setHost(new URI(announceUrl.toExternalForm()));
     httpClient.setHostConfiguration(configuration);
   } catch (URIException e) {
     throw new RuntimeException("Invalid URL", e);
   }
 }
Пример #7
0
 static {
   HostConfiguration hostConfiguration = new HostConfiguration();
   HttpConnectionManagerParams params = new HttpConnectionManagerParams();
   params.setConnectionTimeout(3000);
   params.setMaxTotalConnections(2000);
   params.setStaleCheckingEnabled(true);
   params.setTcpNoDelay(true);
   params.setSoTimeout(10000);
   params.setMaxConnectionsPerHost(hostConfiguration, 1000);
   HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
   connectionManager.setParams(params);
   httpClient = new HttpClient(connectionManager);
   httpClient.setHostConfiguration(hostConfiguration);
 }
Пример #8
0
  protected void initHttpClient(HttpClientConfig config) {
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(config.getHost(), config.getPort());

    connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(config.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(config.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, config.getMaxHostConnections());
    params.setMaxTotalConnections(config.getMaxTotalConnections());
    params.setConnectionTimeout(config.getTimeout());
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpclient = new HttpClient(connectionManager);
    httpclient.setHostConfiguration(hostConfiguration);
  }
  protected void initHttpClient() {
    if (MockServer.isTestMode()) {
      return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(
        diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 10);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
  }
Пример #10
0
  /**
   * Creates a new connection to the server.
   *
   * @param builder The HttpFileSystemConfigBuilder.
   * @param scheme The protocol.
   * @param hostname The hostname.
   * @param port The port number.
   * @param username The username.
   * @param password The password
   * @param fileSystemOptions The file system options.
   * @return a new HttpClient connection.
   * @throws FileSystemException if an error occurs.
   * @since 2.0
   */
  public static HttpClient createConnection(
      HttpFileSystemConfigBuilder builder,
      String scheme,
      String hostname,
      int port,
      String username,
      String password,
      FileSystemOptions fileSystemOptions)
      throws FileSystemException {
    HttpClient client;
    try {
      HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
      HttpConnectionManagerParams connectionMgrParams = mgr.getParams();

      client = new HttpClient(mgr);

      final HostConfiguration config = new HostConfiguration();
      config.setHost(hostname, port, scheme);

      if (fileSystemOptions != null) {
        String proxyHost = builder.getProxyHost(fileSystemOptions);
        int proxyPort = builder.getProxyPort(fileSystemOptions);

        if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
          config.setProxy(proxyHost, proxyPort);
        }

        UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
        if (proxyAuth != null) {
          UserAuthenticationData authData =
              UserAuthenticatorUtils.authenticate(
                  proxyAuth,
                  new UserAuthenticationData.Type[] {
                    UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
                  });

          if (authData != null) {
            final UsernamePasswordCredentials proxyCreds =
                new UsernamePasswordCredentials(
                    UserAuthenticatorUtils.toString(
                        UserAuthenticatorUtils.getData(
                            authData, UserAuthenticationData.USERNAME, null)),
                    UserAuthenticatorUtils.toString(
                        UserAuthenticatorUtils.getData(
                            authData, UserAuthenticationData.PASSWORD, null)));

            AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
            client.getState().setProxyCredentials(scope, proxyCreds);
          }

          if (builder.isPreemptiveAuth(fileSystemOptions)) {
            HttpClientParams httpClientParams = new HttpClientParams();
            httpClientParams.setAuthenticationPreemptive(true);
            client.setParams(httpClientParams);
          }
        }

        Cookie[] cookies = builder.getCookies(fileSystemOptions);
        if (cookies != null) {
          client.getState().addCookies(cookies);
        }
      }
      /**
       * ConnectionManager set methodsmust be called after the host & port and proxy host & port are
       * set in the HostConfiguration. They are all used as part of the key when
       * HttpConnectionManagerParams tries to locate the host configuration.
       */
      connectionMgrParams.setMaxConnectionsPerHost(
          config, builder.getMaxConnectionsPerHost(fileSystemOptions));
      connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));

      client.setHostConfiguration(config);

      if (username != null) {
        final UsernamePasswordCredentials creds =
            new UsernamePasswordCredentials(username, password);
        AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
        client.getState().setCredentials(scope, creds);
      }

      client.executeMethod(new HeadMethod());
    } catch (final Exception exc) {
      throw new FileSystemException(
          "vfs.provider.http/connect.error", new Object[] {hostname}, exc);
    }

    return client;
  }
Пример #11
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;
    }
  }
Пример #12
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();
    }
  }