List of usage examples for org.apache.http.impl.client DefaultHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request, final HttpContext context) throws IOException, ClientProtocolException
From source file:com.oakley.fon.util.HttpUtils.java
public static HttpResult getUrlByPost(String url, Map<String, String> params, Map<String, String> headers, int maxRetries) throws IOException { String result = null;/* ww w .j av a 2 s . c om*/ int retries = 0; HttpContext localContext = new BasicHttpContext(); DefaultHttpClient httpclient = getHttpClient(); List<NameValuePair> formParams = new ArrayList<NameValuePair>(); if (params != null) { Set<Entry<String, String>> paramsSet = params.entrySet(); for (Entry<String, String> entry : paramsSet) { formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formParams, UTF8); HttpPost httppost = new HttpPost(url); httppost.setEntity(postEntity); if (headers != null) { Set<Entry<String, String>> headersSet = headers.entrySet(); for (Entry<String, String> entry : headersSet) { httppost.setHeader(entry.getKey(), entry.getValue()); } } while (retries < maxRetries && result == null) { try { retries++; HttpEntity responseEntity = httpclient.execute(httppost, localContext).getEntity(); if (responseEntity != null) { result = EntityUtils.toString(responseEntity).trim(); } } catch (SocketException se) { if (retries > maxRetries) { throw se; } else { Log.v(TAG, "SocketException, retrying " + retries, se); } } } HttpHost attribute = (HttpHost) localContext.getAttribute("http.target_host"); String targetHost = null; if (attribute != null) { targetHost = attribute.toURI(); } return new HttpResult(result, (BasicHttpResponse) localContext.getAttribute("http.response"), targetHost); }