Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpMethodBase


    String contextroot = System.getProperty("webAppName");
    String root = "http://localhost:8080/"+contextroot;
    int status = 0;
    HttpClient nclient = new HttpClient();
    String url = root+"/CalculatorServlet";
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    status = nclient.executeMethod(httpMethod)
    Assert.assertEquals(status, 200);
    String response = null;
    if(status==200)
    {
      response = new String(httpMethod.getResponseBodyAsString().getBytes("8859_1"));
    }
    Assert.assertTrue(response.contains("Hello! Reexport in Require-bundle attribute is effective since this INFO displays."));
    Assert.assertTrue(response.contains("result of ADD operation [\"10.0 + 8.0 = ?\"] is: 18"));
    Assert.assertTrue(response.contains("result of SUB operation [\"10.0 - 8.0 = ?\"] is: 2"));   
    httpMethod.releaseConnection();
  }
View Full Code Here


    String VersionPara = System.getProperty("moduleVersion");
    String contextRoot = System.getProperty("appContext");

    HttpClient nclient = new HttpClient();
    String url = "http://localhost:8080/"+contextRoot+"/globalJNDITest?version="+VersionPara;
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
   
    int status = nclient.executeMethod(httpMethod);
    Assert.assertEquals(200, status);
   
    String result1 = null;
    String result2 = null;
    String result3 = null;
    if(status==200){
      String response = new String(httpMethod.getResponseBodyAsString().getBytes("8859_1"));
      result1 = findRes("global (.)+? at testServlet.",response);
      result2 = findRes("app (.)+? at testServlet.",response);
      result3 = findRes("module (.)+? at testServlet.",response);
    }
    Assert.assertEquals(result1, "global says:hello at testServlet.");
    Assert.assertEquals(result2, "app says:hello at testServlet.");
    Assert.assertEquals(result3, "module says:hello at testServlet.");
   
    httpMethod.releaseConnection();
  }
View Full Code Here

  public void NullIf_JPQL_Test() throws HttpException, Exception
  {
    String response = null;
    HttpClient nclient = new HttpClient();
    String url = root+"/StudentAdd?sid=3&sname=student3&country=country3&city=city3&street=street3&telephone=333333&age=33&score=0";
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    int status = nclient.executeMethod(httpMethod);
//    System.out.println("status:" + status);
    Assert.assertEquals(status, 200);
    httpMethod.releaseConnection()
   
    url = root+"/nullIfJPQL?sid=3";
    HttpMethodBase httpMethod2;
    httpMethod2 = new PostMethod(url);
    status = nclient.executeMethod(httpMethod2);
    Assert.assertEquals(status, 200);
    if(status==200)
    {
      response = new String(httpMethod2.getResponseBodyAsString().getBytes("8859_1"));
    }
    Assert.assertTrue(response.contains("Nullif is sucess."));
    httpMethod2.releaseConnection()
  }
View Full Code Here

  public void test()throws Exception{
    String contextRoot = System.getProperty("appContext");
    HttpClient nclient = new HttpClient();
    System.out.println("~~~~~~~~~~~~~~~~"+contextRoot);
    String url = "http://localhost:8080/"+contextRoot+"/testServlet";
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    int status = nclient.executeMethod(httpMethod);
    Assert.assertEquals(200, status);
    String result=null;
    if(status==200){
      String response = new String(httpMethod.getResponseBodyAsString().getBytes("8859_1"));
      Assert.assertTrue(response.contains("http://geronimo.apache.org"));
    }
    httpMethod.releaseConnection();
  }
View Full Code Here

    private int invoke(String address, String methodName, String userName, String password) throws Exception {
        HttpClient client = new HttpClient();
        Credentials defaultcreds = new UsernamePasswordCredentials(userName, password);
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);
        String url = "http://localhost:8080/servlet30" + address;
        HttpMethodBase httpMethod;
        if (methodName.equals("GET")) {
            httpMethod = new GetMethod(url);
        } else {
            httpMethod = new PostMethod(url);
        }
View Full Code Here

  public void AsynEJBTest()throws Exception
  {
    String contextRoot = System.getProperty("appContext");
    HttpClient nclient = new HttpClient();
    String url = "http://localhost:8080/"+contextRoot+"/testServlet";
    HttpMethodBase httpMethod2;
    httpMethod2 = new PostMethod(url);
    int status = nclient.executeMethod(httpMethod2);
    Assert.assertEquals(200, status);
    String result=null;
    if(status==200){
      String response = new String(httpMethod2.getResponseBodyAsString().getBytes("8859_1"));
      Matcher m = Pattern.compile("The notify process(.)+? testServlet.").matcher(response);
     
      while (m.find())
      {
        result = m.group();
      }
    }
    Assert.assertEquals(result, "The notify process is undergoing at testServlet.");
   
    httpMethod2.releaseConnection();
}
View Full Code Here

      String contextRoot = System.getProperty("appContext");
        url = "http://localhost:8080/"+contextRoot+"/protect/hello.html";
  }
 
  private int getHTTPResponseStatus(String username) throws Exception {   
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    httpMethod.setRequestHeader("SM_USER", username);
    int status = 0;
   
    HttpClient nclient = new HttpClient();
    status = nclient.executeMethod(httpMethod);
    httpMethod.releaseConnection();   
    return status;
  }
View Full Code Here

    String contextroot = System.getProperty("webAppName");
    String root = "http://localhost:8080/"+contextroot;
    int status = 0;
    HttpClient nclient = new HttpClient();
    String url = root+"/checkResolution";
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    status = nclient.executeMethod(httpMethod)
    Assert.assertEquals(status, 200);
    String response = null;
    if(status==200)
    {
      response = new String(httpMethod.getResponseBodyAsString().getBytes("8859_1"));
    }
    Assert.assertTrue(response.contains("Hello! Reexport in Require-bundle attribute is effective since this INFO displays."));
    Assert.assertTrue(response.contains("Succeed to resolve Attr \"resolution=optional\""))
    httpMethod.releaseConnection();
  }
View Full Code Here

            HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
            HttpResponseHandler<AmazonServiceException> errorResponseHandler)
            throws AmazonServiceException {

        URI endpoint = request.getEndpoint();
        HttpMethodBase method = createHttpMethodFromRequest(request);

        /* Set content type and encoding */
        if (method.getRequestHeader("Content-Type") == null) {
            log.debug("Setting content-type to application/x-www-form-urlencoded; " +
                "charset=" + DEFAULT_ENCODING.toLowerCase());
            method.addRequestHeader("Content-Type",
                    "application/x-www-form-urlencoded; " +
                    "charset=" + DEFAULT_ENCODING.toLowerCase());
        } else {
            log.debug("Not overwriting Content-Type; already set to: " + method.getRequestHeader("Content-Type"));
        }

        /*
         * Depending on which response handler we end up choosing to handle the
         * HTTP response, it might require us to leave the underlying HTTP
         * connection open, depending on whether or not it reads the complete
         * HTTP response stream from the HTTP connection, or if delays reading
         * any of the content until after a response is returned to the caller.
         */
        boolean leaveHttpConnectionOpen = false;

        /*
         * Apache HttpClient omits the port number in the Host header (even if
         * we explicitly specify it) if it's the default port for the protocol
         * in use. To ensure that we use the same Host header in the request and
         * in the calculated string to sign (even if Apache HttpClient changed
         * and started honoring our explicit host with endpoint), we follow this
         * same behavior here and in the QueryString signer.
         */
        String hostHeader = endpoint.getHost();
        if (HttpUtils.isUsingNonDefaultPort(endpoint)) {
            hostHeader += ":" + endpoint.getPort();
        }
        method.addRequestHeader("Host", hostHeader);

        // When we release connections, the connection manager leaves them
        // open so they can be reused.  We want to close out any idle
        // connections so that they don't sit around in CLOSE_WAIT.
        httpClient.getHttpConnectionManager().closeIdleConnections(1000 * 30);

        int retries = 0;
        while (true) {
            try {
                requestLog.info("Sending Request: " + request.toString());

                retries++;
                int status = httpClient.executeMethod(method);

                if (isRequestSuccessful(status)) {
                    /*
                     * If we get back any 2xx status code, then we know we should
                     * treat the service call as successful.
                     */
                    leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
                    return handleResponse(request, responseHandler, method);
                } else if (isTemporaryRedirect(method, status)) {
                    /*
                     * S3 sends 307 Temporary Redirects if you try to delete an
                     * EU bucket from the US endpoint. If we get a 307, we'll
                     * point the HTTP method to the redirected location, and let
                     * the next retry deliver the request to the right location.
                     */
                    Header locationHeader = method.getResponseHeader("location");
                    String redirectedLocation = locationHeader.getValue();
                    log.debug("Redirecting to: " + redirectedLocation);
                    method.setURI(new org.apache.commons.httpclient.URI(redirectedLocation, false));
                } else {
                    leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
                    AmazonServiceException exception = handleErrorResponse(request, errorResponseHandler, method);

                    if (shouldRetry(exception, retries)) {
                        requestLog.info("Received retryable error response: " + status + ", retrying request...");
                        pauseExponentially(retries);
                    } else {
                        throw exception;
                    }
                }
            } catch (IOException ioe) {
                log.error("Unable to execute HTTP request: " + ioe.getMessage());

                throw new AmazonClientException("Unable to execute HTTP request: "
                        + ioe.getMessage(), ioe);
            } finally {
                /*
                 * Some response handlers need to manually manage the HTTP
                 * connection and will take care of releasing the connection on
                 * their own, but if this response handler doesn't need the
                 * connection left open, we go ahead and release the it to free
                 * up resources.
                 */
                if (!leaveHttpConnectionOpen) {
                    try {method.getResponseBodyAsStream().close();} catch (Throwable t) {}
                    method.releaseConnection();
                }
            }
        }
    }
View Full Code Here

            for (Entry<String, String> entry : request.getParameters().entrySet()) {
                nameValuePairs[i++] = new NameValuePair(entry.getKey(), entry.getValue());
            }
        }

        HttpMethodBase method;
        if (request.getMethodName() == HttpMethodName.POST) {
            PostMethod postMethod = new PostMethod(uri);
            if (nameValuePairs != null) postMethod.addParameters(nameValuePairs);
            method = postMethod;
        } else if (request.getMethodName() == HttpMethodName.GET) {
            GetMethod getMethod = new GetMethod(uri);
            if (nameValuePairs != null) getMethod.setQueryString(nameValuePairs);
            method = getMethod;
        } else if (request.getMethodName() == HttpMethodName.PUT) {
            PutMethod putMethod = new PutMethod(uri);
            if (nameValuePairs != null) putMethod.setQueryString(nameValuePairs);
            method = putMethod;

            /*
             * Enable 100-continue support for PUT operations, since this is
             * where we're potentially uploading large amounts of data and want
             * to find out as early as possible if an operation will fail. We
             * don't want to do this for all operations since it will cause
             * extra latency in the network interaction.
             */
            putMethod.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, true);

            if (request.getContent() != null) {
                putMethod.setRequestEntity(new RepeatableInputStreamRequestEntity(request));
            }
        } else if (request.getMethodName() == HttpMethodName.DELETE) {
            DeleteMethod deleteMethod = new DeleteMethod(uri);
            if (nameValuePairs != null) deleteMethod.setQueryString(nameValuePairs);
            method = deleteMethod;
        } else if (request.getMethodName() == HttpMethodName.HEAD) {
            HeadMethod headMethod = new HeadMethod(uri);
            if (nameValuePairs != null) headMethod.setQueryString(nameValuePairs);
            method = headMethod;
        } else {
            throw new AmazonClientException("Unknown HTTP method name: " + request.getMethodName());
        }

        // No matter what type of HTTP method we're creating, we need to copy
        // all the headers from the request.
        for (Entry<String, String> entry : request.getHeaders().entrySet()) {
            method.addRequestHeader(entry.getKey(), entry.getValue());
        }

        return method;
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpMethodBase

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.