All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.bingads.internal.HttpClientWebServiceCaller Maven / Gradle / Ivy

Go to download

The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.

There is a newer version: 13.0.22.1
Show newest version
package com.microsoft.bingads.internal;

import java.io.IOException;
import java.net.ProxySelector;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;


class HttpClientWebServiceCaller implements WebServiceCaller {       
	private CloseableHttpClient client;
	
    @Override
    public HttpResponse post(URL requestUrl, List formValues) throws IOException {

        try {
            client = createHttpClientWithProxy();
            
            final HttpPost httpPost = new HttpPost(requestUrl.toURI());
            
            
            final UrlEncodedFormEntity requestEntity = new UrlEncodedFormEntity(formValues, "UTF-8");
            
            httpPost.setEntity(requestEntity);

            return client.execute(httpPost);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }
    
    private CloseableHttpClient createHttpClientWithProxy() {
        int timeout = 300 * 1000; // 5 minutes

        try {
            String strTimeout = System.getProperty("sun.net.client.defaultConnectTimeout", "300000");
            timeout = Integer.parseInt(strTimeout);
        } catch (Exception e) {
            // ignore
            timeout = 300 * 1000;
        }

        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout).build();

        CloseableHttpClient client = HttpClients.custom().setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
                .setDefaultRequestConfig(config).build();
    	
    	return client;
    }

	@Override
	public void shutDown() {
		if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy