Example usage for org.apache.http.params BasicHttpParams BasicHttpParams

List of usage examples for org.apache.http.params BasicHttpParams BasicHttpParams

Introduction

In this page you can find the example usage for org.apache.http.params BasicHttpParams BasicHttpParams.

Prototype

public BasicHttpParams() 

Source Link

Usage

From source file:com.fujitsu.dc.test.jersey.HttpClientFactory.java

/**
 * HTTPClient?./*from w w  w. j  a  v a2  s  .com*/
 * @param type 
 * @param connectionTimeout ()0????
 * @return ???HttpClient
 */
public static HttpClient create(final String type, final int connectionTimeout) {
    if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
        return new DefaultHttpClient();
    }

    SSLSocketFactory sf = null;
    try {
        if (TYPE_INSECURE.equalsIgnoreCase(type)) {
            sf = createInsecureSSLSocketFactory();
        }
    } catch (Exception e) {
        return null;
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", PORTHTTPS, sf));
    schemeRegistry.register(new Scheme("http", PORTHTTP, PlainSocketFactory.getSocketFactory()));
    HttpParams params = new BasicHttpParams();
    ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
    // ClientConnectionManager cm = new
    // ThreadSafeClientConnManager(schemeRegistry);
    HttpClient hc = new DefaultHttpClient(cm, params);

    HttpParams params2 = hc.getParams();
    int timeout = TIMEOUT;
    if (connectionTimeout != 0) {
        timeout = connectionTimeout;
    }
    HttpConnectionParams.setConnectionTimeout(params2, timeout); // ?
    HttpConnectionParams.setSoTimeout(params2, timeout); // ??
    return hc;
}

From source file:com.juick.android.JettyWsClient.java

public boolean connect(String host, int port, String location, String headers) {
    try {//from w  w w .j av a 2  s . c  o m

        SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
        HttpParams params = new BasicHttpParams();
        sock = sf.connectSocket(sock, host, port, null, 0, params);

        is = sock.getInputStream();
        os = sock.getOutputStream();

        String handshake = "GET " + location + " HTTP/1.1\r\n" + "Host: " + host + "\r\n"
                + "Connection: Upgrade\r\n" + "Upgrade: WebSocket\r\n" + "Sec-WebSocket-Version: 13\r\n"
                + "Sec-WebSocket-Key: 9 9 9 9\r\n" + "Sec-WebSocket-Protocol: sample\r\n";
        if (headers != null) {
            handshake += headers;
        }
        handshake += "\r\n";
        os.write(handshake.getBytes());
        return true;
    } catch (Exception e) {
        System.err.println(e);
        //e.printStackTrace();
        return false;
    }
}

From source file:org.apache.hadoop.gateway.dispatch.HttpClientDispatchTest.java

@Test
public void testJiraKnox58() throws URISyntaxException, IOException {

    URI uri = new URI("http://unreachable-host");
    BasicHttpParams params = new BasicHttpParams();

    HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpUriRequest.class);
    EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(outboundRequest.getURI()).andReturn(uri).anyTimes();
    EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();

    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);

    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() {
        @Override/*from  ww  w  . java  2  s  .  c om*/
        public ServletOutputStream answer() throws Throwable {
            return new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }
            };
        }
    });

    EasyMock.replay(outboundRequest, inboundRequest, outboundResponse);

    HttpClientDispatch dispatch = new HttpClientDispatch();
    try {
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
        fail("Should have thrown IOException");
    } catch (IOException e) {
        assertThat(e.getMessage(), not(containsString("unreachable-host")));
        assertThat(e, not(instanceOf(UnknownHostException.class)));
        assertThat("Message needs meaningful content.", e.getMessage().trim().length(), greaterThan(12));
    }
}

From source file:com.leanengine.RestService.java

private HttpClient getClient() {
    if (client == null) {
        synchronized (this) {
            if (client == null) {
                if (configuration != null) {
                    final HttpParams params = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(params, configuration.getConnectionTimeout());
                    HttpProtocolParams.setVersion(params, configuration.getHttpVersion());
                    HttpProtocolParams.setContentCharset(params, configuration.getContentCharset());

                    final SchemeRegistry schemeRegistry = new SchemeRegistry();
                    schemeRegistry.register(
                            new Scheme("http", configuration.getPlainFactory(), configuration.getPort()));
                    schemeRegistry.register(
                            new Scheme("https", configuration.getSslFactory(), configuration.getSslPort()));

                    final ClientConnectionManager ccm = configuration.createClientConnectionManager(params,
                            schemeRegistry);

                    client = configuration.createClient(ccm, params);
                } else {
                    client = new DefaultHttpClient();
                }//from w  w w.ja  va2 s.com
            }
        }
    }
    return client;
}

From source file:de.ecclesia.kipeto.repository.HttpRepositoryStrategy.java

public HttpRepositoryStrategy(String repository) {
    this.repository = repository;

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, (int) TimeUnit.SECONDS.toMillis(5));
    HttpConnectionParams.setSoTimeout(httpParams, (int) TimeUnit.SECONDS.toMillis(10));
    client = new DefaultHttpClient(httpParams);
}

From source file:org.bishoph.oxdemo.util.OXLoginAction.java

public OXLoginAction(OXDemo oxdemo) {
    this.oxdemo = oxdemo;
    if (httpclient == null) {
        CookieStore cookieStore = new BasicCookieStore();
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        httpclient = new DefaultHttpClient(params);
        localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    }//w w  w  . jav a 2 s .co  m
}

From source file:de.huxhorn.whistler.services.RealurlUrlUnshortener.java

public String unshorten(String url) {

    XMLStreamReader2 xmlStreamReader = null;
    try {/*from ww  w  .  j  a va2  s  . c o  m*/
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        // http://realurl.org/api/v1/getrealurl.php?url=
        qparams.add(new BasicNameValuePair("url", url));
        BasicHttpParams params = new BasicHttpParams();

        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        URI uri = URIUtils.createURI("http", "realurl.org", -1, "/api/v1/getrealurl.php",
                URLEncodedUtils.format(qparams, "UTF-8"), null);
        HttpGet httpget = new HttpGet(uri);
        if (logger.isDebugEnabled())
            logger.debug("HttpGet.uri={}", httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            xmlStreamReader = (XMLStreamReader2) WstxInputFactory.newInstance().createXMLStreamReader(instream);

            /*
            <root>
              <status>1</status>
              <url>
                 <short>http://bit.ly/10QRTY</short>
                 <real>
                 http://uk.techcrunch.com/2009/04/09/tweetmeme-re-launches-to-gun-for-top-of-the-twitter-link-tree/
                 </real>
              </url>
            </root>
             */
            while (xmlStreamReader.hasNext()) {
                int type = xmlStreamReader.next();
                if (type == XMLStreamConstants.START_ELEMENT) {
                    if ("real".equals(xmlStreamReader.getName().getLocalPart())) {
                        return xmlStreamReader.getElementText();
                    }
                }
            }
            /*
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
            StringBuilder builder=new StringBuilder();
            for(;;)
            {
               String line = reader.readLine();
               if(line == null)
               {
                  break;
               }
               if(builder.length() != 0)
               {
                  builder.append("\n");
               }
               builder.append(line);
            }
            if(logger.isInfoEnabled()) logger.info("Result: {}", builder);
            */
        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled())
            logger.warn("Exception!", ex);
    } catch (URISyntaxException ex) {
        if (logger.isWarnEnabled())
            logger.warn("Exception!", ex);
    } catch (XMLStreamException ex) {
        if (logger.isWarnEnabled())
            logger.warn("Exception!", ex);
    } finally {
        if (xmlStreamReader != null) {
            try {
                xmlStreamReader.closeCompletely();
            } catch (XMLStreamException e) {
                // ignore
            }
        }
    }
    return null;
}

From source file:server.web.HttpRequestHelper.java

private static DefaultHttpClient getHttpClient() {
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    return new DefaultHttpClient(httpParameters);
}

From source file:edu.vt.alerts.android.library.util.HttpClientFactory.java

/**
 * Create an HttpClient that is configured with the installer certificate
 * @param context The application context
 * @param installerKeystore The installer certificate
 * @return An HttpClient configured to talk to the VTAPNS using the supplied
 * installer keystore//from  ww  w  .  j a  v  a  2s.c om
 * @throws Exception Anything really
 */
public HttpClient generateInstallerClient(Context context, InputStream installerKeystore) throws Exception {

    HttpParams httpParameters = new BasicHttpParams();

    SSLSocketFactory sockfact = new SSLSocketFactory(getInstallerKeyStore(context, installerKeystore),
            "changeit", getTrustStore(context));
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", sockfact, 443));

    return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParameters, registry), httpParameters);
}