Example usage for org.apache.http.params CoreProtocolPNames USER_AGENT

List of usage examples for org.apache.http.params CoreProtocolPNames USER_AGENT

Introduction

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

Prototype

String USER_AGENT

To view the source code for org.apache.http.params CoreProtocolPNames USER_AGENT.

Click Source Link

Usage

From source file:com.mde.potdroid.helpers.WebsiteInteraction.java

public WebsiteInteraction(Context context) {
    mContext = context;//from w ww.  j a va2  s.  c o  m
    mSettings = PreferenceManager.getDefaultSharedPreferences(mContext);
    mHttpClient = new DefaultHttpClient();
    mHttpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
            "Apache-HttpClient/potdroid " + mSettings.getString("unique_uagent", "potdroid"));

    // check if login cookie exists. If so, attach it to the
    // http client
    if (mSettings.contains("cookie_name")) {
        BasicClientCookie cookie = new BasicClientCookie(mSettings.getString("cookie_name", null),
                mSettings.getString("cookie_value", null));
        cookie.setPath(mSettings.getString("cookie_path", null));
        cookie.setDomain(mSettings.getString("cookie_url", null));
        mHttpClient.getCookieStore().addCookie(cookie);
    }
}

From source file:com.trickl.crawler.protocol.http.HttpProtocol.java

public HttpProtocol(final HttpClient httpclient) {
    super();//from   w w  w.  jav a  2 s.  co  m
    this.httpclient = httpclient;
    this.httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
    this.contentLoader = new HttpClientContentLoader(httpclient);
}

From source file:com.norconex.collector.http.robot.impl.DefaultRobotsTxtProvider.java

@Override
public synchronized RobotsTxt getRobotsTxt(DefaultHttpClient httpClient, String url) {
    String baseURL = getBaseURL(url);
    RobotsTxt robotsTxt = robotsTxtCache.get(baseURL);
    if (robotsTxt != null) {
        return robotsTxt;
    }//from   www.  j  a  v  a2 s . c  o m

    String userAgent = ((String) httpClient.getParams().getParameter(CoreProtocolPNames.USER_AGENT))
            .toLowerCase();
    String robotsURL = baseURL + "/robots.txt";
    HttpGet method = new HttpGet(robotsURL);
    List<String> sitemapLocations = new ArrayList<String>();
    List<IURLFilter> filters = new ArrayList<IURLFilter>();
    MutableFloat crawlDelay = new MutableFloat(RobotsTxt.UNSPECIFIED_CRAWL_DELAY);
    try {
        HttpResponse response = httpClient.execute(method);
        InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
        BufferedReader br = new BufferedReader(isr);
        boolean agentAlreadyMatched = false;
        boolean doneWithAgent = false;
        String line;
        while ((line = br.readLine()) != null) {
            String key = line.replaceFirst("(.*?)(:.*)", "$1").trim();
            String value = line.replaceFirst("(.*?:)(.*)", "$2").trim();
            if ("sitemap".equalsIgnoreCase(key)) {
                sitemapLocations.add(value);
            }
            if (!doneWithAgent) {
                if ("user-agent".equalsIgnoreCase(key)) {
                    if (matchesUserAgent(userAgent, value)) {
                        agentAlreadyMatched = true;
                    } else if (agentAlreadyMatched) {
                        doneWithAgent = true;
                    }
                }
                if (agentAlreadyMatched) {
                    parseAgentLines(baseURL, filters, crawlDelay, key, value);
                }
            }
        }
        isr.close();
    } catch (Exception e) {
        LOG.warn("Not able to obtain robots.txt at: " + robotsURL, e);
    }

    robotsTxt = new RobotsTxt(filters.toArray(new IURLFilter[] {}), crawlDelay.floatValue());
    robotsTxtCache.put(baseURL, robotsTxt);
    return robotsTxt;
}

From source file:com.quantcast.measurement.service.QCDataUploader.java

String synchronousUploadEvents(Collection<QCEvent> events) {
    if (events == null || events.isEmpty())
        return null;

    String uploadId = QCUtility.generateUniqueId();

    JSONObject upload = new JSONObject();
    try {//from www  .  j  a  va  2s .c  o  m
        upload.put(QC_UPLOAD_ID_KEY, uploadId);
        upload.put(QC_QCV_KEY, QCUtility.API_VERSION);
        upload.put(QCEvent.QC_APIKEY_KEY, QCMeasurement.INSTANCE.getApiKey());
        upload.put(QCEvent.QC_NETWORKCODE_KEY, QCMeasurement.INSTANCE.getNetworkCode());
        upload.put(QCEvent.QC_DEVICEID_KEY, QCMeasurement.INSTANCE.getDeviceId());

        JSONArray event = new JSONArray();
        for (QCEvent e : events) {
            event.put(new JSONObject(e.getParameters()));
        }
        upload.put(QC_EVENTS_KEY, event);
    } catch (JSONException e) {
        QCLog.e(TAG, "Error while encoding json.");
        return null;
    }

    int code;
    String url = QCUtility.addScheme(UPLOAD_URL_WITHOUT_SCHEME);
    final DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
    defaultHttpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
    final BasicHttpContext localContext = new BasicHttpContext();

    try {
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json");
        StringEntity se = new StringEntity(upload.toString(), HTTP.UTF_8);
        post.setEntity(se);

        HttpParams params = new BasicHttpParams();
        params.setBooleanParameter("http.protocol.expect-continue", false);
        post.setParams(params);

        HttpResponse response = defaultHttpClient.execute(post, localContext);
        code = response.getStatusLine().getStatusCode();
    } catch (Exception e) {
        QCLog.e(TAG, "Could not upload events", e);
        QCMeasurement.INSTANCE.logSDKError("json-upload-failure", e.toString(), null);
        code = HttpStatus.SC_REQUEST_TIMEOUT;
    }

    if (!isSuccessful(code)) {
        uploadId = null;
        QCLog.e(TAG, "Events not sent to server. Response code: " + code);
        QCMeasurement.INSTANCE.logSDKError("json-upload-failure",
                "Bad response from server. Response code: " + code, null);
    }
    return uploadId;
}

From source file:com.phonty.improved.Register.java

public Register(String url, Context _context) {
    context = _context;/* w  w w.  java2s .  c o  m*/
    APIURL = url;
    httppost = new HttpPost(APIURL);
    httppost.addHeader("Content-Type", "application/json; charset=\"utf-8\"");
    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new PhontyHttpClient(cm, params, context);
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
}

From source file:com.phonty.improved.Sms.java

public Sms(String url, Context _context) {
    context = _context;//from  w  ww  .j  a v a  2 s  . c om
    APIURL = url;
    httppost = new HttpPost(APIURL);
    httppost.addHeader("Content-Type", "application/json; charset=\"utf-8\"");

    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new PhontyHttpClient(cm, params, context);
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(Login.SESSION_COOKIE);
    client.setCookieStore(cookieStore);
}

From source file:com.phonty.improved.Login.java

public Login(String _login, String _password, String url, Context context) {
    APIURL = url;//from   w  w w  . j  ava 2s . c o m
    login = _login;
    password = _password;
    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 4711));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new PhontyHttpClient(cm, params, context);
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
}

From source file:com.nextgis.maplib.util.NetworkUtil.java

public DefaultHttpClient getHttpClient() {
    /*HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOKET);
    */// www  . j  a v a  2  s. c o  m
    DefaultHttpClient HTTPClient = new DefaultHttpClient();//httpParameters);
    HTTPClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, APP_USER_AGENT);
    HTTPClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT_CONNECTION);
    HTTPClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT_SOKET);

    return HTTPClient;
}

From source file:com.vk.sdk.api.httpClient.VKHttpClient.java

/**
 * Creates the http client (if need). Returns reusing client
 *
 * @return Prepared client used for API requests loading
 *//*from w  ww . j a va  2  s.  c o  m*/
public static VKHttpClient getClient() {
    if (sInstance == null) {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        HttpParams params = new BasicHttpParams();
        Context ctx = VKUIHelper.getApplicationContext();

        try {
            if (ctx != null) {
                PackageManager packageManager = ctx.getPackageManager();
                if (packageManager != null) {
                    PackageInfo info = packageManager.getPackageInfo(ctx.getPackageName(), 0);
                    params.setParameter(CoreProtocolPNames.USER_AGENT,
                            String.format(Locale.US, "%s/%s (%s; Android %d; Scale/%.2f; VK SDK %s; %s)",
                                    VKUtil.getApplicationName(ctx), info.versionName, Build.MODEL,
                                    Build.VERSION.SDK_INT, ctx.getResources().getDisplayMetrics().density,
                                    VKSdkVersion.SDK_VERSION, info.packageName));
                }
            }
        } catch (Exception ignored) {
        }
        sInstance = new VKHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params);
    }
    return sInstance;
}

From source file:com.phonty.improved.Contacts.java

public Contacts(String url, Context _context) {
    context = _context;//w w  w.  j  av a  2s .  c o m
    APIURL = url;
    httppost = new HttpPost(APIURL);
    httppost.addHeader("Content-Type", "application/json; charset=\"utf-8\"");

    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new PhontyHttpClient(cm, params, context);
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(Login.SESSION_COOKIE);
    client.setCookieStore(cookieStore);
}