Examples of DefaultHttpClientConnection


Examples of org.apache.http.impl.DefaultHttpClientConnection

    }

    private synchronized String sendRequest(String body, String address, String soapAddress, String method, boolean secondTry)
    {
        //ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpParams params = null;
        BasicHttpProcessor httpproc = null;

        try
        {
            logger.trace("Will send body: " + body);
            URL url = new URL(address);

            if(params == null)
            {
                params = new BasicHttpParams();
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
                HttpProtocolParams.setContentCharset(params, "UTF-8");
                //HttpProtocolParams.setUserAgent(params, "MSN Explorer/9.0 (MSN 8.0; TmstmpExt)");

                HttpProtocolParams.setUseExpectContinue(params, false);

                httpproc = new BasicHttpProcessor();
                // Required protocol interceptors
                httpproc.addInterceptor(new RequestContent());
                httpproc.addInterceptor(new RequestTargetHost());
                // Recommended protocol interceptors
                httpproc.addInterceptor(new RequestConnControl());
                httpproc.addInterceptor(new RequestUserAgent());
                httpproc.addInterceptor(new RequestExpectContinue());
            }

            HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

            HttpContext context = new BasicHttpContext(null);

            HttpHost host = new HttpHost(url.getHost(), 443, "https");

            context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
            context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);


            if (!conn.isOpen())
            {
                // msn change their certificate with invalid one,
                // which prevents us from retreiving contacts and maging them
                // we install dummy trust man ager in order to fix it
                SSLContext sc = SSLContext.getInstance("SSLv3");
                TrustManager[] tma = {new DummyTrustManager()};
                sc.init(null, tma, null);

                SSLSocketFactory factory = sc.getSocketFactory();
                Socket socket = factory.createSocket(host.getHostName(), host.getPort());

                conn.bind(socket, params);
            }

            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
                method,
                url.getPath());
            request.setEntity(new XmlEntity(body));

            context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
            request.setParams(params);

            if(soapAddress != null)
                request.addHeader("SOAPAction", soapAddress);

            request.addHeader("Host", url.getHost());
            request.addHeader("Accept", "text/*");

            httpexecutor.preProcess(request, httpproc, context);

            HttpResponse response = httpexecutor.execute(request, conn, context);
            httpexecutor.postProcess(response, httpproc, context);

            String resultStr = EntityUtils.toString(response.getEntity());

            logger.debug(response.getStatusLine() + " / " + resultStr);

            //if (!connStrategy.keepAlive(response, context))
            try
            {
                conn.close();
            }
            catch (Exception e)
            {
                logger.error("error closing connection", e);
            }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

            mess.append("    </wst:RequestSecurityToken>\r\n");
            mess.append("  </ps:RequestMultipleSecurityTokens>\r\n");
            mess.append("</Body>\r\n");
            mess.append("</Envelope>");

            DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
            HttpParams params = null;
            BasicHttpProcessor httpproc = null;

            params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "UTF-8");
            HttpProtocolParams.setUserAgent(params, "MSN Explorer/9.0 (MSN 8.0; TmstmpExt)");
            HttpProtocolParams.setUseExpectContinue(params, false);

            httpproc = new BasicHttpProcessor();
            // Required protocol interceptors
            httpproc.addInterceptor(new RequestContent());
            httpproc.addInterceptor(new RequestTargetHost());
            // Recommended protocol interceptors
            httpproc.addInterceptor(new RequestConnControl());
            httpproc.addInterceptor(new RequestUserAgent());
            httpproc.addInterceptor(new RequestExpectContinue());

            HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

            HttpContext context = new BasicHttpContext(null);

            HttpHost host = new HttpHost(url.getHost(), 443, "https");

            context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
            context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);


            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            Socket socket = factory.createSocket(host.getHostName(), host.getPort());

            conn.bind(socket, params);

            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
                "POST",
                url.getPath());
            request.setEntity(new XmlEntity(mess.toString()));

            context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
            request.setParams(params);

            request.addHeader("Host", url.getHost());

            httpexecutor.preProcess(request, httpproc, context);

            HttpResponse response = httpexecutor.execute(request, conn, context);
            httpexecutor.postProcess(response, httpproc, context);

            logger.debug(response.getStatusLine());

            String responseStr = EntityUtils.toString(response.getEntity());

            logger.debug(response.getStatusLine() + " / " + responseStr);

            conn.close();

            int code = response.getStatusLine().getStatusCode();

            if(code > -1 && code != 200)
            {
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

          //socketFactory.connectSocket(outsocket,
          //  reverseUrl.getTargetAddress().getHostName(),
          //  reverseUrl.getTargetAddress().getPort(),
          //  null, -1, builder.buildParams());
         
      DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
          conn.bind(outsocket, builder.buildParams());
         
          if (LOG.isTraceEnabled()) {
            LOG.trace("Outgoing connection to " + outsocket.getInetAddress());
            LOG.trace("request: " + request);
        }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

           
        });
       
        this.server.start();
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());
       
        try {
            for (int r = 0; r < reqNo; r++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, this.client.getParams());
                }
               
                HttpGet get = new HttpGet("/?" + r);
                HttpResponse response = this.client.execute(get, host, conn);
                byte[] received = EntityUtils.toByteArray(response.getEntity());
                byte[] expected = (byte[]) testData.get(r);
               
                assertEquals(expected.length, received.length);
                for (int i = 0; i < expected.length; i++) {
                    assertEquals(expected[i], received[i]);
                }
                if (!this.client.keepAlive(response)) {
                    conn.close();
                }
            }
        } finally {
            conn.close();
            this.server.shutdown();
        }
    }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

           
        });
       
        this.server.start();
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());
       
        try {
            for (int r = 0; r < reqNo; r++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, this.client.getParams());
                }
               
                HttpPost post = new HttpPost("/");
                byte[] data = (byte[]) testData.get(r);
                ByteArrayEntity outgoing = new ByteArrayEntity(data);
                post.setEntity(outgoing);

                HttpResponse response = this.client.execute(post, host, conn);
                byte[] received = EntityUtils.toByteArray(response.getEntity());
                byte[] expected = (byte[]) testData.get(r);
               
                assertEquals(expected.length, received.length);
                for (int i = 0; i < expected.length; i++) {
                    assertEquals(expected[i], received[i]);
                }
                if (!this.client.keepAlive(response)) {
                    conn.close();
                }
            }
        } finally {
            conn.close();
            this.server.shutdown();
        }
    }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

           
        });
       
        this.server.start();
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());
       
        try {
            for (int r = 0; r < reqNo; r++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, this.client.getParams());
                }
               
                HttpPost post = new HttpPost("/");
                byte[] data = (byte[]) testData.get(r);
                ByteArrayEntity outgoing = new ByteArrayEntity(data);
                outgoing.setChunked(true);
                post.setEntity(outgoing);

                HttpResponse response = this.client.execute(post, host, conn);
                byte[] received = EntityUtils.toByteArray(response.getEntity());
                byte[] expected = (byte[]) testData.get(r);
               
                assertEquals(expected.length, received.length);
                for (int i = 0; i < expected.length; i++) {
                    assertEquals(expected[i], received[i]);
                }
                if (!this.client.keepAlive(response)) {
                    conn.close();
                }
            }
        } finally {
            conn.close();
            this.server.shutdown();
        }
    }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

       
        // Set protocol level to HTTP/1.0
        this.client.getParams().setParameter(
                HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());
       
        try {
            for (int r = 0; r < reqNo; r++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, this.client.getParams());
                }
               
                HttpPost post = new HttpPost("/");
                byte[] data = (byte[]) testData.get(r);
                ByteArrayEntity outgoing = new ByteArrayEntity(data);
                post.setEntity(outgoing);

                HttpResponse response = this.client.execute(post, host, conn);
                assertEquals(HttpVersion.HTTP_1_0, response.getStatusLine().getHttpVersion());
                byte[] received = EntityUtils.toByteArray(response.getEntity());
                byte[] expected = (byte[]) testData.get(r);
               
                assertEquals(expected.length, received.length);
                for (int i = 0; i < expected.length; i++) {
                    assertEquals(expected[i], received[i]);
                }
                if (!this.client.keepAlive(response)) {
                    conn.close();
                }
            }
        } finally {
            conn.close();
            this.server.shutdown();
        }
    }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

        this.server.start();
       
        // Activate 'expect: continue' handshake
        this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());
       
        try {
            for (int r = 0; r < reqNo; r++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, this.client.getParams());
                }
               
                HttpPost post = new HttpPost("/");
                byte[] data = (byte[]) testData.get(r);
                ByteArrayEntity outgoing = new ByteArrayEntity(data);
                outgoing.setChunked(true);
                post.setEntity(outgoing);

                HttpResponse response = this.client.execute(post, host, conn);
                byte[] received = EntityUtils.toByteArray(response.getEntity());
                byte[] expected = (byte[]) testData.get(r);
               
                assertEquals(expected.length, received.length);
                for (int i = 0; i < expected.length; i++) {
                    assertEquals(expected[i], received[i]);
                }
                if (!this.client.keepAlive(response)) {
                    conn.close();
                }
            }
        } finally {
            conn.close();
            this.server.shutdown();
        }
    }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

        this.server.start();
       
        // Activate 'expect: continue' handshake
        this.client.getParams().setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
       
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());
       
        try {
            for (int r = 0; r < reqNo; r++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket, this.client.getParams());
                }
               
                HttpPost post = new HttpPost("/");
                post.addHeader("Secret", Integer.toString(r));
                ByteArrayEntity outgoing = new ByteArrayEntity(
                        EncodingUtils.getAsciiBytes("No content"));
                post.setEntity(outgoing);

                HttpResponse response = this.client.execute(post, host, conn);

                HttpEntity entity = response.getEntity();
                assertNotNull(entity);
                entity.consumeContent();
               
                if (r < 2) {
                    assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getStatusLine().getStatusCode());
                } else {
                    assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
                }
               
                if (!this.client.keepAlive(response)) {
                    conn.close();
                }
            }
        } finally {
            conn.close();
            this.server.shutdown();
        }
    }
View Full Code Here

Examples of org.apache.http.impl.DefaultHttpClientConnection

        // Prepare connection
        HttpHost host = new HttpHost(
                url.getHost(),
                url.getPort(),
                Scheme.getScheme(url.getProtocol()));
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection(host);
       
        // Prepare request
        HttpRequest request = null;
        if (cmd.hasOption('p')) {
            File file = new File(cmd.getOptionValue('p'));
            if (!file.exists()) {
                System.err.println("File not found: " + file);
                System.exit(-1);
            }
            String contenttype = null;
            if (cmd.hasOption('T')) {
                contenttype = cmd.getOptionValue('T');
            }
            FileEntity entity = new FileEntity(file, contenttype);
            if (file.length() > 100000) {
                entity.setChunked(true);
            }
            HttpPost httppost = new HttpPost(url.getPath());
            httppost.setEntity(entity);
            request = httppost;
        } else if (cmd.hasOption('i')) {
            HttpHead httphead = new HttpHead(url.getPath());
            request = httphead;
        } else {
            HttpGet httpget = new HttpGet(url.getPath());
            request = httpget;
        }
        if (!keepAlive) {
            request.addHeader(new Header(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
        }

        if(cmd.hasOption('H')) {
            String[] strs = cmd.getOptionValues('H');
            for (int i = 0; i < strs.length; i++) {
                String s = strs[i];
                int pos = s.indexOf(':');
                if (pos != -1) {
                    Header header = new Header(
                            s.substring(0, pos).trim(),
                            s.substring(pos + 1));
                    request.addHeader(header);
                }
            }
        }
       
        // Prepare request executor
        HttpRequestExecutor executor = createRequestExecutor();
        BenchmarkWorker worker = new BenchmarkWorker(executor, verbosity);
       
        // Execute
        Stats stats = null;
        try {
            stats = worker.execute(request, conn, num, keepAlive);
        } finally {
            conn.close();
        }
       
        // Show the results
        float totalTimeSec = (float)stats.getDuration() / 1000;
        float reqsPerSec = (float)stats.getSuccessCount() / totalTimeSec;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.