Example usage for org.apache.http.protocol HTTP USER_AGENT

List of usage examples for org.apache.http.protocol HTTP USER_AGENT

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP USER_AGENT.

Prototype

String USER_AGENT

To view the source code for org.apache.http.protocol HTTP USER_AGENT.

Click Source Link

Usage

From source file:groovesquid.GetAdsThread.java

public static String getFile(String url) {
    String responseContent = null;
    HttpEntity httpEntity = null;/*  w  w w . jav  a  2  s . com*/
    try {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
        httpGet.setHeader(HTTP.USER_AGENT,
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse httpResponse = httpClient.execute(httpGet);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        httpEntity = httpResponse.getEntity();

        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            httpEntity.writeTo(baos);
        } else {
            throw new RuntimeException(url);
        }

        responseContent = baos.toString("UTF-8");
    } catch (Exception ex) {
        log.log(Level.SEVERE, null, ex);
    } finally {
        try {
            EntityUtils.consume(httpEntity);
        } catch (IOException ex) {
            log.log(Level.SEVERE, null, ex);
        }
    }
    return responseContent;
}

From source file:com.googlecode.deadalus.integrations.foursquare.FourSquareApi.java

public FourSquareApi() {
    this(new DefaultHttpClient());
    httpClient.getParams().setParameter(HTTP.EXPECT_CONTINUE, false);
    httpClient.getParams().setParameter(HTTP.USER_AGENT, "Deadalus 0.1.0");
}

From source file:lyrics.crawler.webClient.ContentDownloader.java

private HttpEntity getContent(HttpHost targetHost, String content) throws DownloadException, IOException {
    HttpGet request = new HttpGet(content);
    request.setHeader(HTTP.USER_AGENT,
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-us) AppleWebKit/532.3+ (KHTML, like Gecko) Version/4.0.3 Safari/531.9");
    request.setHeader("Accept-Language", "en-us,en;q=0.5");
    // Other possibly useful headers
    //request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    //request.setHeader("Accept-Encoding","gzip,deflate");
    //request.setHeader("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");

    HttpResponse response = client.execute(targetHost, request);

    if (response.getStatusLine().getStatusCode() != 200) {
        request.abort();//w ww  .j  a  va  2s .co m
        throw new DownloadException(response.getStatusLine().getStatusCode());
    }

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        return entity;
    } else {
        throw new DownloadException();
    }
}

From source file:org.gdg.frisbee.android.api.GdgStack.java

private void acquireCsrfToken() {
    StringRequest csrfRequest = new StringRequest(DIRECTORY_URL, null, null);
    HashMap<String, String> additionalHeaders = new HashMap<String, String>();
    additionalHeaders.put(HTTP.USER_AGENT, USER_AGENT);

    try {//from  www  .  j a  va 2 s . c om
        HttpResponse response = performRequest(csrfRequest, additionalHeaders);

        if (response.getStatusLine().getStatusCode() == 200) {
            Header csrfCookie = response.getFirstHeader("Set-Cookie");
            if (csrfCookie != null && csrfCookie.getValue().contains("csrftoken")) {
                Pattern pattern = Pattern.compile("csrftoken=([a-z0-9]{32})");
                Matcher matcher = pattern.matcher(csrfCookie.getValue());
                if (matcher.find()) {
                    mCsrfToken = matcher.group(1);
                    Log.d(LOG_TAG, "Got csrf token: " + mCsrfToken);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (AuthFailureError authFailureError) {
        authFailureError.printStackTrace();
    }
}

From source file:com.android.volley.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }//from  w  w  w. j a  v a 2  s  .  co m
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }

    setConnectionParametersForRequest(connection, request);

    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }

    StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));

    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }

    return response;
}

From source file:com.vincestyling.netroid.stack.HurlStack.java

@Override
public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError {
    HashMap<String, String> map = new HashMap<String, String>();
    if (!TextUtils.isEmpty(mUserAgent)) {
        map.put(HTTP.USER_AGENT, mUserAgent);
    }//from  ww  w . j a  v  a  2 s .c om
    map.putAll(request.getHeaders());

    URL parsedUrl = new URL(request.getUrl());
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }

    setConnectionParametersForRequest(connection, request);

    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }

    StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
            connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        response.setEntity(entityFromConnection(connection));
    }

    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }

    return response;
}

From source file:com.microsoft.windowsazure.mobileservices.http.ServiceFilterRequestImpl.java

@Override
public ServiceFilterResponse execute() throws Exception {
    // Execute request
    AndroidHttpClient client = mAndroidHttpClientFactory.createAndroidHttpClient();
    client.getParams().setParameter(HTTP.USER_AGENT, MobileServiceConnection.getUserAgent());

    try {/*from www  . j  a va 2 s  . c o  m*/
        final HttpResponse response = client.execute(mRequest);
        ServiceFilterResponse serviceFilterResponse = new ServiceFilterResponseImpl(response);
        return serviceFilterResponse;
    } finally {
        client.close();
    }
}

From source file:com.hazelcast.example.musicdb.gui.step.AvailableDataSetStep.java

@Override
protected StepOperation createOperation() {
    return new StepOperation() {
        @Override//w  w  w.  j a  va2 s .c  o m
        public void execute() throws Exception {
            HttpClient client = new DefaultHttpClient();
            HttpGet operation = new HttpGet(AutoLoader.BASE_URL);
            operation.setHeader(HTTP.USER_AGENT, USER_AGENT);
            operation.setHeader("Accept", "text/html,application/xhtml+xml");
            HttpResponse response = client.execute(operation);
            String html = EntityUtils.toString(response.getEntity());

            final List<String> dates = new ArrayList<>();
            Matcher matcher = CHECKSUM_PATTERN.matcher(html);
            while (matcher.find()) {
                String date = matcher.group(1);
                if (checkOtherFiles(date, html)) {
                    dates.add(date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6));
                }
            }

            getContext().put("dates", dates);
            transition();
        }
    };
}

From source file:org.yamj.api.common.http.HttpClientWrapper.java

@SuppressWarnings("unused")
protected void prepareRequest(HttpUriRequest request) throws ClientProtocolException {
    if (randomUserAgent) {
        request.setHeader(HTTP.USER_AGENT, UserAgentSelector.randomUserAgent());
    }/*www  .j  a  v a 2s .  co m*/
}

From source file:org.camunda.connect.httpclient.HttpConnectorSystemPropertiesTest.java

@Test
public void shouldSetUserAgentFromSystemProperty() {
    // given/*www  . j  a  va2  s.  c om*/
    setSystemProperty("http.agent", "foo");

    HttpConnector customConnector = new HttpConnectorImpl();

    // when
    customConnector.createRequest().url("http://localhost:" + PORT).get().execute();

    // then
    verify(getRequestedFor(urlEqualTo("/")).withHeader(HTTP.USER_AGENT, equalTo("foo")));

}