public void runSimpleLoginLoadTest() {
    HttpClient httpClient = getHttpClient();

    // RequestEntity re = new StringRequestEntity(raw_body, "text/xml",
    // "UTF-16");

    try {

      PostMethod postMethod = new PostMethod(getAsvip() + "mas/login");
      postMethod.addParameters(getReqParams());
      int statuscode = httpClient.executeMethod(postMethod);
      byte[] responseBody = postMethod.getResponseBody();
      postMethod.releaseConnection();
      // System.out.println(statuscode + " LOGIN   RESPONSE  ---- "+ new String(responseBody));

      postMethod = new PostMethod(getAsvip() + "mas/logout");
      statuscode = httpClient.executeMethod(postMethod);
      responseBody = postMethod.getResponseBody();
      //	System.out.println(statuscode + "  LOGOUT  RESPONSE  ---- "+ new String(responseBody));
      postMethod.releaseConnection();

    } catch (HttpException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
示例#2
0
  /**
   * POST 요청을 처리한다.
   *
   * @param <T>
   * @param baseUrl
   * @param httpInvocation
   * @return
   */
  public <T> T post(String baseUrl, HttpInvocation<T> httpInvocation) {
    Assert.notNull(httpInvocation, "httpInvocation 값은 null일 수 없다.");
    PostMethod postMethod = new PostMethod(baseUrl + httpInvocation.getUri());
    postMethod.setRequestHeader(
        "Content-Type",
        "application/x-www-form-urlencoded; charset=" + httpInvocation.getEncoding());
    String parameterEncoding = httpInvocation.getParameterEncoding();
    if (StringUtils.isNotBlank(parameterEncoding)) {
      postMethod.getParams().setContentCharset(parameterEncoding);
    }

    postMethod.addParameters(httpInvocation.getParameters());

    return invoke(postMethod, httpInvocation);
  }
示例#3
0
  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  public void run() {
    HttpClient client = new HttpClient();

    PostMethod method = new PostMethod(url);

    NameValuePair[] postData = new NameValuePair[1];
    postData[0] = new NameValuePair("XDEBUG_SESSION", "default");

    method.addParameters(postData);

    try {
      System.out.println("sending http request with debugging flag");

      debugger.terminate(url, client.executeMethod(method));

      System.out.println("get http response");
    } catch (Exception e) {
    }
  }
  /**
   * 执行Http请求
   *
   * @param request 请求数据
   * @param strParaFileName 文件类型的参数名
   * @param strFilePath 文件路径
   * @return
   * @throws HttpException, IOException
   */
  public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath)
      throws HttpException, IOException {
    HttpClient httpclient = new HttpClient(connectionManager);

    // 设置连接超时
    int connectionTimeout = defaultConnectionTimeout;
    if (request.getConnectionTimeout() > 0) {
      connectionTimeout = request.getConnectionTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

    // 设置回应超时
    int soTimeout = defaultSoTimeout;
    if (request.getTimeout() > 0) {
      soTimeout = request.getTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

    // 设置等待ConnectionManager释放connection的时间
    httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout);

    String charset = request.getCharset();
    charset = charset == null ? DEFAULT_CHARSET : charset;
    HttpMethod method = null;

    // get模式且不带上传文件
    if (request.getMethod().equals(HttpRequest.METHOD_GET)) {
      method = new GetMethod(request.getUrl());
      method.getParams().setCredentialCharset(charset);

      // parseNotifyConfig会保证使用GET方法时,request一定使用QueryString
      method.setQueryString(request.getQueryString());
    } else if (strParaFileName.equals("") && strFilePath.equals("")) {
      // post模式且不带上传文件
      method = new PostMethod(request.getUrl());
      ((PostMethod) method).addParameters(request.getParameters());
      method.addRequestHeader(
          "Content-Type", "application/x-www-form-urlencoded; text/html; charset=" + charset);
    } else {
      // post模式且带上传文件
      method = new PostMethod(request.getUrl());
      List<Part> parts = new ArrayList<Part>();
      for (int i = 0; i < request.getParameters().length; i++) {
        parts.add(
            new StringPart(
                request.getParameters()[i].getName(),
                request.getParameters()[i].getValue(),
                charset));
      }
      // 增加文件参数,strParaFileName是参数名,使用本地文件
      parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath))));

      // 设置请求体
      ((PostMethod) method)
          .setRequestEntity(
              new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams()));
    }

    // 设置Http Header中的User-Agent属性
    method.addRequestHeader("User-Agent", "Mozilla/4.0");
    HttpResponse response = new HttpResponse();

    try {
      httpclient.executeMethod(method);
      if (request.getResultType().equals(HttpResultType.STRING)) {
        response.setStringResult(method.getResponseBodyAsString());
      } else if (request.getResultType().equals(HttpResultType.BYTES)) {
        response.setByteResult(method.getResponseBody());
      }
      response.setResponseHeaders(method.getResponseHeaders());
    } catch (UnknownHostException ex) {

      return null;
    } catch (IOException ex) {

      return null;
    } catch (Exception ex) {

      return null;
    } finally {
      method.releaseConnection();
    }
    return response;
  }
示例#5
0
  /**
   * prelogin后拿到2个参数用来生成加密的密码,正式登陆。 账户,密码采用输入参数
   *
   * @param client
   * @param serverTimeAndNonce
   * @param userEmail
   * @param passwordOld
   * @return
   * @throws IOException
   * @throws HttpException
   */
  public static String Login(
      HttpClient client,
      Map<String, String> serverTimeAndNonce,
      String userEmail,
      String passwordOld)
      throws IOException, HttpException {
    PostMethod post = new PostMethod(loginurl);
    // 初始化POST方法的content
    NameValuePair entry = new NameValuePair("entry", "miniblog");
    NameValuePair gateway = new NameValuePair("gateway", "1");
    NameValuePair from = new NameValuePair("from", "");
    NameValuePair savestate = new NameValuePair("savestate", "7");
    NameValuePair useticket = new NameValuePair("useticket", "1");
    NameValuePair ssosimplelogin = new NameValuePair("ssosimplelogin", "1");
    NameValuePair username = new NameValuePair("username", userEmail);
    NameValuePair service = new NameValuePair("service", "miniblog");
    NameValuePair servertime =
        new NameValuePair("servertime", serverTimeAndNonce.get("servertime"));
    NameValuePair nonce = new NameValuePair("nonce", serverTimeAndNonce.get("nonce"));
    NameValuePair pwencode = new NameValuePair("pwencode", "wsse");
    NameValuePair password =
        new NameValuePair("password", encryption(passwordOld, serverTimeAndNonce));
    NameValuePair encoding = new NameValuePair("encoding", "utf-8");
    NameValuePair url =
        new NameValuePair(
            "url",
            "http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack");
    NameValuePair returntype = new NameValuePair("returntype", "META");
    post.addParameters(
        new NameValuePair[] {
          entry,
          gateway,
          from,
          savestate,
          useticket,
          ssosimplelogin,
          username,
          service,
          servertime,
          nonce,
          pwencode,
          password,
          encoding,
          url,
          returntype
        });

    int status = client.executeMethod(post);
    String responseBodyAsString = post.getResponseBodyAsString();

    /*
     * System.out.println(status);
     *
     * System.out.println(response);
     * System.out.println(post.getFollowRedirects());
     * System.out.println(post.getPath()); for (Header h :
     * post.getResponseHeaders()) { System.out.print(h); }
     */
    // printCookie(client);
    // mergeCookie(client);
    // printCookie(client);
    System.out.println("-------------Login结束--------------");
    post.releaseConnection();

    return getAjaxUrl(responseBodyAsString);
  }
  /**
   * 执行Http请求
   *
   * @param request
   * @return
   */
  public HttpResponse execute(HttpRequest request) {
    HttpClient httpclient = new HttpClient(connectionManager);

    // 设置连接超时
    int connectionTimeout = defaultConnectionTimeout;
    if (request.getConnectionTimeout() > 0) {
      connectionTimeout = request.getConnectionTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

    // 设置回应超时
    int soTimeout = defaultSoTimeout;
    if (request.getTimeout() > 0) {
      soTimeout = request.getTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

    // 设置等待ConnectionManager释放connection的时间
    httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout);

    String charset = request.getCharset();
    charset = charset == null ? DEFAULT_CHARSET : charset;
    HttpMethod method = null;

    if (request.getMethod().equals(HttpRequest.METHOD_GET)) {
      method = new GetMethod(request.getUrl());
      method.getParams().setCredentialCharset(charset);

      // parseNotifyConfig会保证使用GET方法时,request一定使用QueryString
      method.setQueryString(request.getQueryString());
    } else {
      method = new PostMethod(request.getUrl());
      ((PostMethod) method).addParameters(request.getParameters());
      method.addRequestHeader(
          "Content-Type", "application/x-www-form-urlencoded; text/html; charset=" + charset);
    }

    // 设置Http Header中的User-Agent属性
    method.addRequestHeader("User-Agent", "Mozilla/4.0");
    HttpResponse response = new HttpResponse();

    try {
      httpclient.executeMethod(method);
      if (request.getResultType().equals(HttpResultType.STRING)) {
        response.setStringResult(method.getResponseBodyAsString());
      } else if (request.getResultType().equals(HttpResultType.BYTES)) {
        response.setByteResult(method.getResponseBody());
      }
      response.setResponseHeaders(method.getResponseHeaders());
    } catch (UnknownHostException ex) {

      return null;
    } catch (IOException ex) {

      return null;
    } catch (Exception ex) {

      return null;
    } finally {
      method.releaseConnection();
    }
    return response;
  }