Soluzione 1: -Usa sottostante Codice: -
private void sendPost() throws Exception {
String url = "https://accounts.google.com/o/oauth2/token";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String urlParameters = "";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
Soluzione 2: - Utilizzare Primavera/Netty modello resto
public class HttpRequestProcessor {
@Autowired
private AsyncHttpClient asyncHttpClient;
private static final Logger logger_c = LoggerFactory
.getLogger(HttpRequestProcessor.class);
public enum HttpMethod {
HTTP_GET, HTTP_POST, HTTP_DELETE
}
public static final int HTTP_GET = 1;
public static final int HTTP_POST = 2;
public static final int HTTP_DELETE = 3;
// private static AsyncHttpClient client;
public HttpRequestProcessor() {
LOGGER.info("HttpRequestProcessor created");
Create Nett Http provider configuration for asynchronous connect
operation
NettyAsyncHttpProviderConfig nettyAsyncHttpProviderConfig = new
NettyAsyncHttpProviderConfig(); nettyAsyncHttpProviderConfig.addProperty(
NettyAsyncHttpProviderConfig.EXECUTE_ASYNC_CONNECT, true);
nettyAsyncHttpProviderConfig.addProperty(
NettyAsyncHttpProviderConfig.DISABLE_NESTED_REQUEST, false);
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
.setAllowPoolingConnection(true) .setAllowSslConnectionPool(true)
.setMaximumConnectionsPerHost(500) .setMaximumConnectionsTotal(800)
.setIdleConnectionTimeoutInMs(60000)
.setConnectionTimeoutInMs(5000).setRequestTimeoutInMs(25000)
.setAsyncHttpClientProviderConfig(nettyAsyncHttpProviderConfig) .build();
client = new AsyncHttpClient(new NettyAsyncHttpProvider(config), config);
}
private String getRequestType(HttpMethod method) {
switch (method) {
case HTTP_DELETE:
return "DELETE";
case HTTP_GET:
return "GET";
case HTTP_POST:
return "POST";
default:
return null;
}
}
private RequestBuilder createRequestBuilder(HttpMethod method, String url,
Map<String, String> headers, Map<String, String> queryParameters) {
RequestBuilder requestBuilder = new RequestBuilder(
getRequestType(method));
requestBuilder.setUrl(url);
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestBuilder.setHeader(header.getKey(), header.getValue());
}
}
if (queryParameters != null) {
for (Map.Entry<String, String> requestParameter : queryParameters
.entrySet()) {
requestBuilder.addQueryParameter(requestParameter.getKey(),
requestParameter.getValue());
}
}
return requestBuilder;
}
public Future<?> submitRequest(HttpMethod method, String url,
Map<String, String> headers, Map<String, String> queryParameters,
Map<String, String> postParameters, String bodyData,
AsyncHandler<?> asyncHandler) throws IOException {
RequestBuilder requestBuilder = createRequestBuilder(
HttpMethod.HTTP_POST, url, headers, queryParameters);
if (postParameters != null) {
for (Map.Entry<String, String> posParameter : postParameters
.entrySet()) {
requestBuilder.addParameter(posParameter.getKey(),
posParameter.getValue());
}
}
if (bodyData != null)
requestBuilder.setBody(bodyData);
return submitRequest(requestBuilder.build(), asyncHandler);
}
public Future<?> submitPostRequest(String url, Map<String, String> headers,
Map<String, String> queryParameters,
Map<String, String> postParameters, AsyncHandler<?> asyncHandler)
throws IOException {
return submitRequest(HttpMethod.HTTP_POST, url, headers,
queryParameters, postParameters, null, asyncHandler);
}
public Future<?> submitPostRequest(String url, Map<String, String> headers,
Map<String, String> queryParameters, String bodyData,
AsyncHandler<?> asyncHandler) throws IOException {
return submitRequest(HttpMethod.HTTP_POST, url, headers,
queryParameters, null, bodyData, asyncHandler);
}
public Future<?> submitGetRequest(String url, Map<String, String> headers,
Map<String, String> queryParameters, AsyncHandler<?> asyncHandler)
throws IOException {
return submitRequest(
createRequestBuilder(HttpMethod.HTTP_GET, url, headers,
queryParameters).build(), asyncHandler);
}
public Future<?> submitRequest(Request request, AsyncHandler<?> asyncHandler)
throws IOException {
return asyncHttpClient.executeRequest(request, asyncHandler);
}
/
public Future<Response> submitGetRequests(String url, String bodyParam,
AsyncCompletionHandlerBase asyncHandler) throws IOException {
RequestBuilder requestBuilder = new RequestBuilder("GET").setUrl(url); //
requestBuilder.setBody(bodyParam); Future<Response> responseFuture =
asyncHttpClient.executeRequest(requestBuilder.build(), new
AsyncCompletionHandlerBase());
return responseFuture; }
/
public Future<?> submitPostRequest(String url,
Map<String, String> requestHeaders, String body,
AsyncCompletionHandlerBase asyncHandler) throws IOException {
return submitRequest(
createRequestBuilder(HttpMethod.HTTP_POST, requestHeaders, url,
body).build(), asyncHandler);
}
private RequestBuilder createRequestBuilder(HttpMethod method,
Map<String, String> requestHeaders, String url, String body) {
RequestBuilder requestBuilder = new RequestBuilder(
getRequestType(method));
requestBuilder.setUrl(url);
requestBuilder.setBody(body);
requestBuilder.setHeader("Content-Type", "application/json");
if (requestHeaders != null)
for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
requestBuilder.setHeader(entry.getKey(), entry.getValue());
}
logger_c.info("Request is :" + requestBuilder.toString());
return requestBuilder;
}
}