Skip to content

Instantly share code, notes, and snippets.

@jfabrix101
Last active December 14, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfabrix101/5123757 to your computer and use it in GitHub Desktop.
Save jfabrix101/5123757 to your computer and use it in GitHub Desktop.
Effettuare una chiamata POST in https
public static String postUrl(String url, Map<String, String> params) throws Exception {
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", new SimpleSSLSocketFactory(), 443));
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
httpParams.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
httpParams.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
ClientConnectionManager cm = new SingleClientConnManager(httpParams, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, httpParams);
HttpPost httpPost = new HttpPost(url);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
for (Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
nameValuePairs.add(new BasicNameValuePair(key, val));
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
int returnCode = response.getStatusLine().getStatusCode();
String htmlBody = EntityUtils.toString(response.getEntity());
if(returnCode != HttpStatus.SC_OK) {
Log.e(Const.LOG_TAG, "- Network error reading URL: " + url);
Log.e(Const.LOG_TAG, "- Network error: HttpStatusCode : " + response.getStatusLine().getStatusCode());
return null;
}
return htmlBody;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment