List of usage examples for org.apache.http.client HttpClient getParams
@Deprecated HttpParams getParams();
From source file:cn.edu.mju.Thriphoto.net.HttpManager.java
/** * ? URL ?//from w ww.j ava2 s . co m * * @param url ? * @param method "GET" or "POST" * @param params ? * @param file ????? SdCard ? * * @return ? * @throws com.sina.weibo.sdk.exception.WeiboException ? */ public static String openUrl(String url, String method, WeiboParameters params, String file) throws WeiboException { String result = ""; try { HttpClient client = getNewHttpClient(); HttpUriRequest request = null; ByteArrayOutputStream bos = null; client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, NetStateManager.getAPN()); if (method.equals(HTTPMETHOD_GET)) { url = url + "?" + Utility.encodeUrl(params); HttpGet get = new HttpGet(url); request = get; } else if (method.equals(HTTPMETHOD_POST)) { HttpPost post = new HttpPost(url); request = post; byte[] data = null; String _contentType = params.getValue("content-type"); bos = new ByteArrayOutputStream(); if (!TextUtils.isEmpty(file)) { paramToUpload(bos, params); post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY); // ? // Utility.UploadImageUtils.revitionPostImageSize(file); imageContentToUpload(bos, file); } else { if (_contentType != null) { params.remove("content-type"); post.setHeader("Content-Type", _contentType); } else { post.setHeader("Content-Type", "application/x-www-form-urlencoded"); } String postParam = Utility.encodeParameters(params); data = postParam.getBytes("UTF-8"); bos.write(data); } data = bos.toByteArray(); bos.close(); ByteArrayEntity formEntity = new ByteArrayEntity(data); post.setEntity(formEntity); } else if (method.equals("DELETE")) { request = new HttpDelete(url); } HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); if (statusCode != 200) { result = readHttpResponse(response); throw new WeiboHttpException(result, statusCode); } result = readHttpResponse(response); return result; } catch (IOException e) { throw new WeiboException(e); } }
From source file:cn.edu.mju.Thriphoto.net.HttpManager.java
/** * ???? formdata name="pic" name="file" ????? * * @param url ?/*from ww w . j a v a 2s.c o m*/ * @param method "GET" or "POST" * @param params ? * @param file ????? SdCard ? * * @return ? * @throws com.sina.weibo.sdk.exception.WeiboException ? */ public static String uploadFile(String url, String method, WeiboParameters params, String file) throws WeiboException { String result = ""; try { HttpClient client = getNewHttpClient(); HttpUriRequest request = null; ByteArrayOutputStream bos = null; client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, NetStateManager.getAPN()); if (method.equals(HTTPMETHOD_GET)) { url = url + "?" + Utility.encodeUrl(params); HttpGet get = new HttpGet(url); request = get; } else if (method.equals(HTTPMETHOD_POST)) { HttpPost post = new HttpPost(url); request = post; byte[] data = null; String _contentType = params.getValue("content-type"); bos = new ByteArrayOutputStream(); if (!TextUtils.isEmpty(file)) { paramToUpload(bos, params); post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY); // ? // Utility.UploadImageUtils.revitionPostImageSize(file); fileToUpload(bos, file); } else { if (_contentType != null) { params.remove("content-type"); post.setHeader("Content-Type", _contentType); } else { post.setHeader("Content-Type", "application/x-www-form-urlencoded"); } String postParam = Utility.encodeParameters(params); data = postParam.getBytes("UTF-8"); bos.write(data); } data = bos.toByteArray(); bos.close(); ByteArrayEntity formEntity = new ByteArrayEntity(data); post.setEntity(formEntity); } else if (method.equals("DELETE")) { request = new HttpDelete(url); } HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); if (statusCode != 200) { result = readHttpResponse(response); throw new WeiboHttpException(result, statusCode); } result = readHttpResponse(response); return result; } catch (IOException e) { throw new WeiboException(e); } }
From source file:org.hl7.fhir.client.ClientUtils.java
/** * * @param request/*from w w w. j ava 2 s. c om*/ * @param payload * @return */ protected static HttpResponse sendRequest(HttpUriRequest request, HttpHost proxy) { HttpResponse response = null; try { HttpClient httpclient = new DefaultHttpClient(); if (proxy != null) { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } response = httpclient.execute(request); } catch (IOException ioe) { throw new EFhirClientException("Error sending Http Request", ioe); } return response; }
From source file:org.hl7.fhir.client.ClientUtils.java
/** * Method posts request payload//from w w w . j a v a2 s.c om * * @param request * @param payload * @return */ protected static HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) { HttpResponse response = null; try { HttpClient httpclient = new DefaultHttpClient(); if (proxy != null) { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } request.setEntity(new ByteArrayEntity(payload)); response = httpclient.execute(request); } catch (IOException ioe) { throw new EFhirClientException("Error sending HTTP Post/Put Payload", ioe); } return response; }
From source file:test.Http.java
/** * ?url??/*ww w. j a va 2s. c om*/ * * @param url * @return */ public static String HttpPost2(String url) { String responseBodyData = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); // ? httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); httpPost.getParams().setParameter("http.socket.timeout", new Integer(60000)); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000 * 5); try { HttpResponse response = httpClient.execute(httpPost);// post if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { } HttpEntity entity = response.getEntity(); if (entity != null) { responseBodyData = EntityUtils.toString(entity, "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { httpPost.abort(); httpClient = null; } return responseBodyData; }
From source file:com.lugia.timetable.SSLHttpClient.java
public static SSLHttpClient getHttpClient() throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException { HttpClient client = new DefaultHttpClient(); X509TrustManager tm = createX509TrustManager(); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new MySSLSocketFactory(ctx); ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = client.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", ssf, 443)); return new SSLHttpClient(new ThreadSafeClientConnManager(client.getParams(), sr), client.getParams()); }
From source file:com.gtx.cooliris.imagecache.ImageFetcher.java
public static boolean downloadUrlToStream(String urlString, OutputStream outputStream, BitmapWorkerTask task) { /*if (BuildConfig.DEBUG) { LogUtil.d(TAG, "downloadBitmap - downloading - " + urlString); }*//*from ww w. j a v a 2 s. c om*/ LogUtil.d(TAG, "downloadBitmap - downloading - " + urlString); disableConnectionReuseIfNecessary(); BufferedOutputStream out = null; BufferedInputStream in = null; HttpGet httpGet = new HttpGet(urlString); HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); if (null != task) { task.setURLConnection(httpGet); } try { HttpResponse httpResponse = client.execute(httpGet); if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = httpResponse.getEntity(); LogUtil.d(TAG, "Print Response"); long contentLength = entity.getContentLength(); LogUtil.d(TAG, "Get content length = " + contentLength); /*in = new BufferedInputStream(entity.getContent(), IO_BUFFER_SIZE); out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE); int b; while ((b = in.read()) != -1) { out.write(b); }*/ // Get buffer from network in = new BufferedInputStream(entity.getContent(), IO_BUFFER_SIZE); ByteArrayOutputStream bufos = new ByteArrayOutputStream(); int b; while ((b = in.read()) != -1) { bufos.write(b); } byte[] buf = bufos.toByteArray(); // Create image file by the buffer copyStreamToLocalImage(urlString, new ByteArrayInputStream(buf), task); // Write the buffer into output stream. outputStream.write(buf); bufos.close(); return true; } } catch (final IOException e) { LogUtil.e(TAG, "Error in downloadBitmap - " + e.getMessage() + ", url = " + urlString); //e.printStackTrace(); } catch (Exception e) { LogUtil.e(TAG, "Error in downloadBitmap - " + e.getMessage()); } finally { if (out != null) { try { out.close(); } catch (final IOException e) { LogUtil.e(TAG, "Error in downloadBitmap - " + e); } } // Add by liulin if (null != outputStream) { try { outputStream.close(); } catch (Exception e) { LogUtil.e(TAG, "Error in downloadBitmap - " + e); } } if (null != in) { try { in.close(); } catch (Exception e) { LogUtil.e(TAG, "Error in downloadBitmap - " + e); } } } return false; }
From source file:com.github.restdriver.serverdriver.RestServerDriver.java
private static Response doHttpRequest(ServerDriverHttpUriRequest request) { HttpClient httpClient = new DefaultHttpClient(RestServerDriver.ccm, RestServerDriver.httpParams); HttpParams httpParams = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, (int) request.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpParams, (int) request.getSocketTimeout()); HttpClientParams.setRedirecting(httpParams, false); if (request.getProxyHost() != null) { httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, request.getProxyHost()); }//from w w w.j a v a 2 s .com HttpUriRequest httpUriRequest = request.getHttpUriRequest(); if (!httpUriRequest.containsHeader(USER_AGENT)) { httpUriRequest.addHeader(USER_AGENT, DEFAULT_USER_AGENT); } HttpResponse response; try { long startTime = System.currentTimeMillis(); response = httpClient.execute(httpUriRequest); long endTime = System.currentTimeMillis(); return new DefaultResponse(response, (endTime - startTime)); } catch (ClientProtocolException cpe) { throw new RuntimeClientProtocolException(cpe); } catch (UnknownHostException uhe) { throw new RuntimeUnknownHostException(uhe); } catch (HttpHostConnectException hhce) { throw new RuntimeHttpHostConnectException(hhce); } catch (IOException e) { throw new RuntimeException("Error executing request", e); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:test.Http.java
/** * post url :??jsonString???json,?json//from ww w . ja v a 2 s . c o m * * @date * @param url * @param jsonString * @return * @throws Exception */ public static String httpPost1(String url, String jsonString) throws Exception { String responseBodyData = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); // ? // httpPost.addHeader("Content-Type", // "application/x-www-form-urlencoded"); httpPost.addHeader("Content-Type", "text/html"); httpPost.getParams().setParameter("http.socket.timeout", new Integer(60000)); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000 * 5); // Log.i(TAG, jsonString); // byte[] requestStrings = jsonString.getBytes("UTF-8"); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(requestStrings); httpPost.setEntity(byteArrayEntity); HttpResponse response = httpClient.execute(httpPost);// post try { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { } HttpEntity entity = response.getEntity(); if (entity != null) { responseBodyData = EntityUtils.toString(entity, "UTF-8"); } } catch (Exception e) { // throw new Exception(e); e.printStackTrace(); } finally { httpPost.abort(); httpClient = null; } return responseBodyData; }
From source file:com.easyhome.common.modules.network.HttpManager.java
/** * ? URL ?// www. j a v a2 s.c om * * @param context ? * @param url ? * @param method "GET" or "POST" * @param params ? * @param file ????? SdCard ? * * @return ? * @throws com.sina.weibo.sdk.exception.WeiboException ? */ public static ByteArrayOutputStream openUrl4Binary(Context context, String url, String method, WeiboParameters params, String file) throws WeiboException { ByteArrayOutputStream result = null; try { HttpClient client = getNewHttpClient(); HttpUriRequest request = null; ByteArrayOutputStream bos = null; client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, NetStateManager.getAPN()); if (method.equals(HTTPMETHOD_GET)) { url = url + "?" + Utility.encodeUrl(params); HttpGet get = new HttpGet(url); request = get; } else if (method.equals(HTTPMETHOD_POST)) { HttpPost post = new HttpPost(url); request = post; byte[] data = null; String _contentType = params.getValue("content-type"); bos = new ByteArrayOutputStream(); if (!TextUtils.isEmpty(file)) { paramToUpload(bos, params); post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY); Utility.UploadImageUtils.revitionPostImageSize(context, file); imageContentToUpload(bos, file); } else { if (_contentType != null) { params.remove("content-type"); post.setHeader("Content-Type", _contentType); } else { post.setHeader("Content-Type", "application/x-www-form-urlencoded"); } String postParam = Utility.encodeParameters(params); data = postParam.getBytes("UTF-8"); bos.write(data); } data = bos.toByteArray(); bos.close(); ByteArrayEntity formEntity = new ByteArrayEntity(data); post.setEntity(formEntity); } else if (method.equals("DELETE")) { request = new HttpDelete(url); } HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); if (statusCode != 200) { String resultStr = readHttpResponse(response); throw new WeiboException(resultStr); } result = readBytesFromHttpResponse(response); return result; } catch (IOException e) { throw new WeiboException(e); } }