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:org.transdroid.search.Demonoid.DemonoidAdapter.java

@Override
public List<SearchResult> search(Context context, String query, SortOrder order, int maxResults)
        throws Exception {

    if (query == null) {
        return null;
    }/*from w  ww.  j a va2s  .  c o  m*/

    this.maxResults = maxResults;

    // Build a search request parameters
    String encodedQuery = "";
    try {
        encodedQuery = URLEncoder.encode(query, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw e;
    }

    final String url = String.format(QUERYURL, encodedQuery,
            (order == SortOrder.BySeeders ? SORT_SEEDS : SORT_COMPOSITE));

    // Start synchronous search

    // Setup request using GET
    HttpParams httpparams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpparams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpparams, CONNECTION_TIMEOUT);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpparams);
    // Spoof Firefox user agent to force a result
    httpclient.getParams().setParameter("http.useragent",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    HttpGet httpget = new HttpGet(url);

    // Make request
    HttpResponse response = httpclient.execute(httpget);

    // Read HTML response
    InputStream instream = response.getEntity().getContent();
    String html = HttpHelper.convertStreamToString(instream);
    instream.close();
    return parseHtml(html);

}

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

/**
 * Generate an HttpClient that is configured to use the subscriber's
 * certificate//from w  w  w . ja va 2  s  .c om
 * @param context The application context
 * @param env The environment to run in
 * @return An HttpClient that is configured to talk to the VTAPNS using the
 * subscriber's certificate
 * @throws Exception Any exception really...
 */
public HttpClient generateSubscriberClient(Context context, Environment env) throws Exception {
    HttpParams httpParameters = new BasicHttpParams();

    KeyStore keyStore = subscriberKeystoreContainer.retrieveKeyStore(context, env);
    SSLSocketFactory sockfact = new SSLSocketFactory(keyStore, "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);
}

From source file:org.transdroid.search.AsiaTorrents.AsiaTorrentsAdapter.java

private DefaultHttpClient prepareRequest(Context context) throws Exception {

    String username = SettingsHelper.getSiteUser(context, TorrentSite.AsiaTorrents);
    String password = SettingsHelper.getSitePass(context, TorrentSite.AsiaTorrents);
    if (username == null || password == null) {
        throw new InvalidParameterException(
                "No username or password was provided, while this is required for this private site.");
    }//from  w  w  w  . j av a  2 s .  c  om

    // Setup request using GET
    HttpParams httpparams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpparams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpparams, CONNECTION_TIMEOUT);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpparams);

    // First log in
    HttpPost loginPost = new HttpPost(LOGINURL);
    loginPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair[] {
            new BasicNameValuePair(LOGIN_USER, username), new BasicNameValuePair(LOGIN_PASS, password) })));
    HttpResponse loginResult = httpclient.execute(loginPost);
    if (loginResult.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        // Failed to sign in
        throw new LoginException("Login failure for AsiaTorrents with user " + username);
    }

    return httpclient;

}

From source file:com.bai.android.data.arcamera.DownloadContent.java

/**
 * The method that carries out the download and saving of the file to the according path 
 *///ww  w .  ja  v a 2s  .com
public void downloadFile() throws Exception {
    ManageFileService manageFileService = new ManageFileService(localFileLocation);
    if (manageFileService.cardChecks() == true) {
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
        HttpConnectionParams.setSoTimeout(httpParameters, 15000);
        HttpConnectionParams.setTcpNoDelay(httpParameters, true);

        DefaultHttpClient client = new DefaultHttpClient(httpParameters);

        HttpGet request = new HttpGet(downloadUrl.toString());

        //get path for file save and prepare output stream to write the file;

        FileOutputStream fileOutput = new FileOutputStream(manageFileService.createFileForDownload());

        // Get the response
        HttpResponse response = client.execute(request);

        //start getting the data from the url input stream
        InputStream inputStream = response.getEntity().getContent();

        //create a buffer
        byte[] buffer = new byte[1024];
        int bufferLength = 0; //used to store a temporary size of the buffer

        //read through the input buffer and write the contents to the file
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            //add the data in the buffer to the file in the file output stream
            fileOutput.write(buffer, 0, bufferLength);
        }
        //close the output stream when done
        fileOutput.close();
    } else {
        //TODO toast error
    }
    if (manageFileService.fileExists() == true) {
        String zipFile = localFileLocation;
        Decompress d = new Decompress(zipFile, unzipLocation);
        d.unzip();
        manageFileService.deleteFile();
    } else {
        //TODO toast error
    }
}

From source file:edu.hust.grid.crawl.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());
    params.setBooleanParameter("http.protocol.handle-redirects", false);
    params.setParameter("http.language.Accept-Language", "en-us");
    params.setParameter("http.protocol.content-charset", "UTF-8");
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }/*from ww  w.  j a v  a  2s .  c om*/

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:org.apache.hadoop.gateway.hdfs.dispatch.WebHdfsHaHttpClientDispatchTest.java

@Test
public void testConnectivityFailover() throws Exception {
    String serviceName = "WEBHDFS";
    HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
    descriptor.addServiceConfig(//from  ww w  . j a  v  a2  s . c  om
            HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000"));
    HaProvider provider = new DefaultHaProvider(descriptor);
    URI uri1 = new URI("http://unreachable-host");
    URI uri2 = new URI("http://reachable-host");
    ArrayList<String> urlList = new ArrayList<String>();
    urlList.add(uri1.toString());
    urlList.add(uri2.toString());
    provider.addHaService(serviceName, urlList);
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);

    EasyMock.expect(filterConfig.getInitParameter(WebHdfsHaHttpClientDispatch.RESOURCE_ROLE_ATTRIBUTE))
            .andReturn(serviceName).anyTimes();
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME))
            .andReturn(provider).anyTimes();

    BasicHttpParams params = new BasicHttpParams();

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

    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once();
    EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0))
            .once();
    EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1))
            .once();

    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() {
        @Override
        public ServletOutputStream answer() throws Throwable {
            return new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }
            };
        }
    }).once();
    EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
    Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
    WebHdfsHaHttpClientDispatch dispatch = new WebHdfsHaHttpClientDispatch();
    dispatch.init(filterConfig);
    long startTime = System.currentTimeMillis();
    try {
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } catch (IOException e) {
        //this is expected after the failover limit is reached
    }
    long elapsedTime = System.currentTimeMillis() - startTime;
    Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName));
    //test to make sure the sleep took place
    Assert.assertTrue(elapsedTime > 1000);
}

From source file:yin.autoflowcontrol.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }/*from   w  ww  . ja  v  a 2 s .  c  o  m*/

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:bluej.collect.DataSubmitter.java

/**
 * Actually post the data to the server.
 * //  www .j a  v  a  2  s  . co  m
 * Returns false if there was an error.
 */
private static boolean postData(Event evt) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 10000);
    HttpConnectionParams.setSoTimeout(params, 10000);
    HttpClient client = new DefaultHttpClient(params);

    try {
        HttpPost post = new HttpPost(submitUrl);

        /*
        if (errFilePath != null) {
        mpe.addPart("filepath", new StringBody(errFilePath, utf8));
        }
        if (errMsg != null) {
        mpe.addPart("errormsg", new StringBody(errMsg, utf8));
        }
        if (errline != 0) {
        mpe.addPart("errorline", new StringBody("" + errline, utf8));
        }
                
        int i = 0;
        for (SourceContent changedFile : changedFiles) {
        mpe.addPart("sourcefileName" + i, new StringBody(changedFile.getFilePath(), utf8));
        mpe.addPart("sourcefileContent" + i, new ByteArrayBody(changedFile.getFileContent(),
                changedFile.getFilePath()));
        }
        */
        MultipartEntity mpe = evt.makeData(sequenceNum, fileVersions);
        if (mpe == null)
            return true; // nothing to send, no error
        //Only increment sequence number if we actually send data:
        sequenceNum += 1;
        post.setEntity(mpe);
        HttpResponse response = client.execute(post);

        for (Header h : response.getAllHeaders()) {
            if ("X-Status".equals(h.getName()) && !"Created".equals(h.getValue())) {
                // Temporary printing:
                System.err.println("Problem response: " + mpe.toString() + " " + response.toString());
                return false;
            }
        }

        if (response.getStatusLine().getStatusCode() != 200) {
            System.err.println("Problem code: " + response.getStatusLine().getStatusCode());
            return false;
        }

        evt.success(fileVersions);

        EntityUtils.consume(response.getEntity());
    } catch (ClientProtocolException cpe) {
        return false;
    } catch (IOException ioe) {
        //For now:
        ioe.printStackTrace();

        return false;
    }

    return true;
}

From source file:com.iia.giveastick.util.RestClient.java

/**
 * @param serviceName The serviceName should be the name of the Service
 *    you are going to be using.// w w  w. j a  v a  2  s. c  o m
 */
public RestClient(String serviceName, int port, String scheme) {
    HttpParams myParams = new BasicHttpParams();

    // Set timeout
    myParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpConnectionParams.setConnectionTimeout(myParams, 30000);
    HttpConnectionParams.setSoTimeout(myParams, 20000);
    HttpProtocolParams.setUseExpectContinue(myParams, true);

    this.httpClient = new DefaultHttpClient(myParams);
    this.serviceName = serviceName;
    this.port = port;
    this.scheme = scheme;
}