com.ninja.examples.utility.net.APIRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.ninja.examples.utility.net.APIRequest.java

Source

/*
Copyright 2011 Ryan Mattison
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.ninja.examples.utility.net;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;

public class APIRequest {

    private static final int WS_TIMEOUT = 15000;
    public static DefaultHttpClient httpclient;

    public static DefaultHttpClient getClient() {
        if (httpclient == null) {
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setUseExpectContinue(params, false);
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "utf-8");

            params.setBooleanParameter("http.protocol.expect-continue", false);

            HttpConnectionParams.setConnectionTimeout(params, WS_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, WS_TIMEOUT);

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

            ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);

            httpclient = new DefaultHttpClient(manager, params);

            httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    return 50;
                }
            });

        }
        return httpclient;
    }

}