Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpConnection


     */
    public HttpConnection getConnectionWithTimeout(
        HostConfiguration hostConfiguration, long timeout)
    {

        HttpConnection httpConnection = getLocalHttpConnection();
        if (httpConnection == null)
        {
            httpConnection = new HttpConnection(hostConfiguration);
            setLocalHttpConnection(httpConnection);
            httpConnection.setHttpConnectionManager(this);
            this.params.populateParameters(httpConnection);
        }
        else
        {

            // make sure the host and proxy are correct for this connection
            // close it and set the values if they are not
            if (!hostConfiguration.hostEquals(httpConnection)
                || !hostConfiguration.proxyEquals(httpConnection))
            {

                if (httpConnection.isOpen())
                {
                    httpConnection.close();
                }

                httpConnection.setHost(hostConfiguration.getHost());
                httpConnection.setPort(hostConfiguration.getPort());
                httpConnection.setProtocol(hostConfiguration.getProtocol());
                httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());

                httpConnection.setProxyHost(hostConfiguration.getProxyHost());
                httpConnection.setProxyPort(hostConfiguration.getProxyPort());
            }
            else
            {
                finishLastResponse(httpConnection);
            }
View Full Code Here


            this.generateDebugOutput();
            return;
        }

        /* Call up the remote HTTP server */
        HttpConnection connection = new HttpConnection(this.method.getHostConfiguration());
        HttpState state = new HttpState();
        this.method.setFollowRedirects(true);
        int status = this.method.execute(state, connection);
        if (status == 404) {
            throw new ResourceNotFoundException("Unable to access \"" + this.method.getURI()
                    + "\" (HTTP 404 Error)");
        } else if ((status < 200) || (status > 299)) {
            throw new IOException("Unable to access HTTP resource at \""
                    + this.method.getURI().toString() + "\" (status=" + status + ")");
        }
        InputStream response = this.method.getResponseBodyAsStream();

        /* Let's try to set up our InputSource from the response output stream and to parse it */
        SAXParser parser = null;
        try {
            InputSource inputSource = new InputSource(response);
            parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
            parser.parse(inputSource, super.xmlConsumer);
        } catch (ComponentException ex) {
            throw new ProcessingException("Unable to get parser", ex);
        } finally {
            this.manager.release((Component) parser);
            this.method.releaseConnection();
            connection.close();
        }
    }
View Full Code Here

        this.xscriptObject = xscriptObject;
    }

    public XScriptObject invoke() throws ProcessingException
    {
        HttpConnection conn = null;

        try {
            if (action == null || action.equals("")) {
                action = "\"\"";
            }

            String host = url.getHost();
            int port = url.getPort();

            if (System.getProperty("http.proxyHost") != null) {
                String proxyHost = System.getProperty("http.proxyHost");
                int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
                conn = new HttpConnection(proxyHost, proxyPort, host, port);
            } else {
                conn = new HttpConnection(host, port);
            }

            PostMethod method = new PostMethod(url.getFile());
            String request;

            try {
                // Write the SOAP request body
                if (xscriptObject instanceof XScriptObjectInlineXML) {
                    // Skip overhead
                    request = ((XScriptObjectInlineXML) xscriptObject).getContent();
                } else {
                    StringBuffer bodyBuffer = new StringBuffer();
                    InputSource saxSource = xscriptObject.getInputSource();

                    Reader r = null;
                    // Byte stream or character stream?
                    if (saxSource.getByteStream() != null) {
                        r = new InputStreamReader(saxSource.getByteStream());
                    } else {
                        r = saxSource.getCharacterStream();
                    }

                    try {
                        char[] buffer = new char[1024];
                        int len;
                        while ((len = r.read(buffer)) > 0)
                            bodyBuffer.append(buffer, 0, len);
                    } finally {
                        if (r != null) {
                            r.close();
                        }
                    }

                    request = bodyBuffer.toString();
                }

            } catch (Exception ex) {
                throw new ProcessingException("Error assembling request", ex);
            }

            method.setRequestHeader(
                    new Header("Content-type", "text/xml; charset=\"utf-8\""));
            method.setRequestHeader(new Header("SOAPAction", action));
            method.setUseDisk(false);
            method.setRequestBody(request);

            method.execute(new HttpState(), conn);

            String ret = method.getResponseBodyAsString();
            int startOfXML = ret.indexOf("<?xml");
            if (startOfXML == -1) { // No xml?!
                throw new ProcessingException("Invalid response - no xml");
            }

            return new XScriptObjectInlineXML(
                    xscriptManager,
                    ret.substring(startOfXML));
        } catch (Exception ex) {
            throw new ProcessingException("Error invoking remote service: " + ex,
                    ex);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception ex) {
            }
        }
    }
View Full Code Here

        {
          //Same details, no need to reset
        }
        else
        {
          httpConn = new HttpConnection(hc);
          //TODO check these
            httpConn.setProxyHost(System.getProperty("http.proxyHost"));
            httpConn.setProxyPort( Integer.parseInt(System.getProperty("http.proxyPort","80")));
        }
View Full Code Here

    res.setSampleLabel(urlStr);
    res.sampleStart(); // Count the retries as well in the time

        try
        {
            HttpConnection connection = setupConnection(url, method, res);
             
            if (method.equals(POST))
            {
                sendPostData(httpMethod);
            }
View Full Code Here

        subscribeMethod.addRequestHeader(SubscribeMethod.H_DEPTH, ((depth == DepthSupport.DEPTH_INFINITY ) ? "infinity" : String.valueOf(depth)));
        try {
            subscribeMethod.setDoAuthentication(true);
            HttpState httpState = new HttpState();
            httpState.setCredentials(null, repositoryHost, credentials);
            HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
            httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
            int state = subscribeMethod.execute(httpState, httpConnection);
            if ( state == HttpStatus.SC_OK ) {
                String subscriptionId = subscribeMethod.getResponseHeader(SubscribeMethod.H_SUBSCRIPTION_ID).getValue();
                logger.log(Level.INFO, "Received subscription id="+subscriptionId+", listener: "+listener);
                int id = Integer.valueOf(subscriptionId).intValue();
View Full Code Here

                    unsubscribeMethod.addRequestHeader(UnsubscribeMethod.H_SUBSCRIPTION_ID, id);
                    try {
                        unsubscribeMethod.setDoAuthentication(true);
                        HttpState httpState = new HttpState();
                        httpState.setCredentials(null, repositoryHost, credentials);
                        HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
                        httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
                        int state = unsubscribeMethod.execute(httpState, httpConnection);
                        if ( state == HttpStatus.SC_OK ) {
                            i.remove();
                            return true;
                        } else {
View Full Code Here

    protected void fireEvent(EventMethod eventMethod, Credentials credentials) throws IOException {
        eventMethod.setDoAuthentication(true);
        HttpState httpState = new HttpState();
        httpState.setCredentials(null, repositoryHost, credentials);
        int state = eventMethod.execute(httpState, new HttpConnection(repositoryHost, repositoryPort, protocol));
        if ( state == HttpStatus.SC_OK ) {
        } else {
            logger.log(Level.SEVERE, "Event failed. State: "+state);
        }
    }
View Full Code Here

            pollMethod.addRequestHeader(SubscribeMethod.H_SUBSCRIPTION_ID, pollSubscribers);
            try {
                pollMethod.setDoAuthentication(true);
                HttpState httpState = new HttpState();
                httpState.setCredentials(null, repositoryHost, credentials);
              HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
              httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
                int state = pollMethod.execute(httpState, httpConnection);
                if ( state == HttpStatus.SC_MULTI_STATUS ) {
                    List events = pollMethod.getEvents();
                    for ( Iterator i = events.iterator(); i.hasNext(); ) {
                        Event event = (Event)i.next();
View Full Code Here

        {
          //Same details, no need to reset
        }
        else
        {
          httpConn = new HttpConnection(hc);
          //TODO check these
            httpConn.setProxyHost(System.getProperty("http.proxyHost"));
            httpConn.setProxyPort( Integer.parseInt(System.getProperty("http.proxyPort","80")));
        }
View Full Code Here

TOP

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

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.