List of usage examples for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT
String CONNECTION_TIMEOUT
To view the source code for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT.
Click Source Link
From source file:cn.sharesdk.net.NetworkHelper.java
public static PostResult httpPost(String url, ArrayList<NameValuePair> pairs) { PostResult postResult = new PostResult(); if (pairs == null || pairs.size() == 0) { postResult.setSuccess(false);/*from w ww . j a v a2 s. co m*/ postResult.setResponseMsg("post date of the request params is null !"); return postResult; } try { HttpPost httppost = new HttpPost(url); httppost.addHeader("charset", HTTP.UTF_8); httppost.setHeader("Accept", "application/json,text/x-json,application/jsonrequest,text/json"); HttpEntity entity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8); httppost.setEntity(entity); HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); String resString = EntityUtils.toString(response.getEntity()); postResult = parse(status, resString); } catch (Exception e) { Ln.i("NetworkHelper", "=== post Ln ===", e); } return postResult; }
From source file:com.baofeng.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {/* w w w . j a va2s . co m*/ for (int i = 0; i < BFGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); for (RequestParameter p : parameter) { list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue()))); LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue()); } StringBuffer sb = new StringBuffer(); for (int j = 0; j < list.size(); j++) { sb.append(list.get(j)); if (!(j == list.size() - 1)) { sb.append("&"); } } BasicNameValuePair bn = new BasicNameValuePair("sign", MD5Util.MD5(sb + "1234" + BFGameConfig.SERVERKEY)); // System.out.println("@@@" + sb + "1234" // + BFGameConfig.SERVERKEY); list.add(bn); ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } LogUtil.d("AsyncHttpPost : ", url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { // HttpManager.saveCookies(response); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } break; } catch (Exception e) { if (i == BFGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, BFGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!BFGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:org.extensiblecatalog.ncip.v2.symphony.SymphonyHttpService.java
/** * Initializes the instance./*from w ww. j ava 2s . c o m*/ * <p> * Calling this method will set the <code>httpClient</code>, <code>connectionManager</code>, * and <code>jsonMapper</code> properties to 'sensible' defaults if they have not been supplied * prior to its execution. * </p> * @see #getClient() * @see #getClientConnectionManager() * @see #setMapper(JsonMapper) * */ @Override public void afterPropertiesSet() throws Exception { if (client == null) { connectionManager = getClientConnectionManager(); client = new DefaultHttpClient(connectionManager); HttpParams params = client.getParams(); params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "utf-8"); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout); } if (mapper == null) { mapper = new JsonMapper(); } }
From source file:com.iloomo.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {/*from w w w . j av a2 s .co m*/ for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); Set<String> keys = parameter.keySet(); for (String key : keys) { list.add(new BasicNameValuePair(key, String.valueOf(parameter.get(key)))); } ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { // LogUtil.d("HttpTask", " use GZIPInputStream "); is = new GZIPInputStream(bis); } else { // LogUtil.d("HttpTask", // " not use GZIPInputStream"); is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("999", exception.getMessage()); } break; } catch (Exception e) { if (i == HttpConstant.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("999", exception.getMessage()); } else { Log.d("connection url", "" + i); continue; } } } } catch (IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, HttpConstant.ERROR_MESSAGE); ret = ErrorUtil.errorJson("999", exception.getMessage()); } finally { if (!HttpConstant.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } } super.run(); }
From source file:com.changxiang.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {// w ww . jav a 2 s . c om for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); for (RequestParameter p : parameter) { list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue()))); LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue()); } StringBuffer sb = new StringBuffer(); for (int j = 0; j < list.size(); j++) { sb.append(list.get(j)); if (!(j == list.size() - 1)) { sb.append("&"); } } BasicNameValuePair bn = new BasicNameValuePair("sign", MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY)); System.out.println( "POST_SIGN:" + sb + "&sign=" + MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY)); list.add(bn); ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } System.out.println("====" + url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { // HttpManager.saveCookies(response); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } break; } catch (Exception e) { if (i == CXGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, CXGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!CXGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:test.Http.java
/** * post url :??jsonString???json,?json/*from w w w . j a v a2s .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:no.kantega.kwashc.server.test.SSLProtocolTest.java
private HttpResponse checkClient(Site site, int httpsPort, HttpClient httpclient, String[] protocols, String[] ciphers) throws NoSuchAlgorithmException, KeyManagementException, IOException { SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { allowAllTrustManager }, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000); SSLSocket socket = (SSLSocket) sf.createSocket(params); if (protocols != null) { socket.setEnabledProtocols(protocols); }//from w w w . ja v a 2s .com if (ciphers != null) { socket.setEnabledCipherSuites(ciphers); } URL url = new URL(site.getAddress()); InetSocketAddress address = new InetSocketAddress(url.getHost(), httpsPort); sf.connectSocket(socket, address, null, params); Scheme sch = new Scheme("https", httpsPort, sf); httpclient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet request = new HttpGet( "https://" + url.getHost() + ":" + site.getSecureport() + url.getPath() + "blog"); return httpclient.execute(request); }
From source file:jp.ne.sakura.kkkon.java.net.socketimpl.testapp.android.SocketImplHookTestApp.java
/** Called when the activity is first created. */ @Override//from w ww .j a v a2 s . c om public void onCreate(Bundle savedInstanceState) { final Context context = this.getApplicationContext(); { SocketImplHookFactory.initialize(); } { ProxySelector proxySelector = ProxySelector.getDefault(); Log.d(TAG, "proxySelector=" + proxySelector); if (null != proxySelector) { URI uri = null; try { uri = new URI("http://www.google.com/"); } catch (URISyntaxException e) { Log.d(TAG, e.toString()); } List<Proxy> proxies = proxySelector.select(uri); if (null != proxies) { for (final Proxy proxy : proxies) { Log.d(TAG, " proxy=" + proxy); } } } } super.onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native * function. */ LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); TextView tv = new TextView(this); tv.setText("ExceptionHandler"); layout.addView(tv); Button btn1 = new Button(this); btn1.setText("invoke Exception"); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final int count = 2; int[] array = new int[count]; int value = array[count]; // invoke IndexOutOfBOundsException } }); layout.addView(btn1); { Button btn = new Button(this); btn.setText("upload http AsyncTask"); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AsyncTask<String, Void, Boolean> asyncTask = new AsyncTask<String, Void, Boolean>() { @Override protected Boolean doInBackground(String... paramss) { Boolean result = true; Log.d(TAG, "upload AsyncTask tid=" + android.os.Process.myTid()); try { //$(BRAND)/$(PRODUCT)/$(DEVICE)/$(BOARD):$(VERSION.RELEASE)/$(ID)/$(VERSION.INCREMENTAL):$(TYPE)/$(TAGS) Log.d(TAG, "fng=" + Build.FINGERPRINT); final List<NameValuePair> list = new ArrayList<NameValuePair>(16); list.add(new BasicNameValuePair("fng", Build.FINGERPRINT)); HttpPost httpPost = new HttpPost(paramss[0]); //httpPost.getParams().setParameter( CoreConnectionPNames.SO_TIMEOUT, new Integer(5*1000) ); httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); DefaultHttpClient httpClient = new DefaultHttpClient(); Log.d(TAG, "socket.timeout=" + httpClient.getParams() .getIntParameter(CoreConnectionPNames.SO_TIMEOUT, -1)); Log.d(TAG, "connection.timeout=" + httpClient.getParams() .getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, -1)); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(5 * 1000)); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(5 * 1000)); Log.d(TAG, "socket.timeout=" + httpClient.getParams() .getIntParameter(CoreConnectionPNames.SO_TIMEOUT, -1)); Log.d(TAG, "connection.timeout=" + httpClient.getParams() .getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, -1)); // <uses-permission android:name="android.permission.INTERNET"/> // got android.os.NetworkOnMainThreadException, run at UI Main Thread HttpResponse response = httpClient.execute(httpPost); Log.d(TAG, "response=" + response.getStatusLine().getStatusCode()); } catch (Exception e) { Log.d(TAG, "got Exception. msg=" + e.getMessage(), e); result = false; } Log.d(TAG, "upload finish"); return result; } }; asyncTask.execute("http://kkkon.sakura.ne.jp/android/bug"); asyncTask.isCancelled(); } }); layout.addView(btn); } { Button btn = new Button(this); btn.setText("pre DNS query(0.0.0.0)"); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isReachable = false; Thread thread = new Thread(new Runnable() { public void run() { try { destHost = InetAddress.getByName("0.0.0.0"); if (null != destHost) { try { if (destHost.isReachable(5 * 1000)) { Log.d(TAG, "destHost=" + destHost.toString() + " reachable"); } else { Log.d(TAG, "destHost=" + destHost.toString() + " not reachable"); } } catch (IOException e) { } } } catch (UnknownHostException e) { } Log.d(TAG, "destHost=" + destHost); } }); thread.start(); try { thread.join(1000); } catch (InterruptedException e) { } } }); layout.addView(btn); } { Button btn = new Button(this); btn.setText("pre DNS query(www.google.com)"); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isReachable = false; Thread thread = new Thread(new Runnable() { public void run() { try { InetAddress dest = InetAddress.getByName("www.google.com"); if (null == dest) { dest = destHost; } if (null != dest) { try { if (dest.isReachable(5 * 1000)) { Log.d(TAG, "destHost=" + dest.toString() + " reachable"); isReachable = true; } else { Log.d(TAG, "destHost=" + dest.toString() + " not reachable"); } destHost = dest; } catch (IOException e) { } } else { } } catch (UnknownHostException e) { Log.d(TAG, "dns error" + e.toString()); destHost = null; } { if (null != destHost) { Log.d(TAG, "destHost=" + destHost); } } } }); thread.start(); try { thread.join(); { final String addr = (null == destHost) ? ("") : (destHost.toString()); final String reachable = (isReachable) ? ("reachable") : ("not reachable"); Toast toast = Toast.makeText(context, "DNS result=\n" + addr + "\n " + reachable, Toast.LENGTH_LONG); toast.show(); } } catch (InterruptedException e) { } } }); layout.addView(btn); } { Button btn = new Button(this); btn.setText("pre DNS query(kkkon.sakura.ne.jp)"); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isReachable = false; Thread thread = new Thread(new Runnable() { public void run() { try { InetAddress dest = InetAddress.getByName("kkkon.sakura.ne.jp"); if (null == dest) { dest = destHost; } if (null != dest) { try { if (dest.isReachable(5 * 1000)) { Log.d(TAG, "destHost=" + dest.toString() + " reachable"); isReachable = true; } else { Log.d(TAG, "destHost=" + dest.toString() + " not reachable"); } destHost = dest; } catch (IOException e) { } } else { } } catch (UnknownHostException e) { Log.d(TAG, "dns error" + e.toString()); destHost = null; } { if (null != destHost) { Log.d(TAG, "destHost=" + destHost); } } } }); thread.start(); try { thread.join(); { final String addr = (null == destHost) ? ("") : (destHost.toString()); final String reachable = (isReachable) ? ("reachable") : ("not reachable"); Toast toast = Toast.makeText(context, "DNS result=\n" + addr + "\n " + reachable, Toast.LENGTH_LONG); toast.show(); } } catch (InterruptedException e) { } } }); layout.addView(btn); } setContentView(layout); }
From source file:com.investoday.code.util.aliyun.api.gateway.util.HttpUtil.java
/** * HTTP POST /*ww w . j a v a2s . c o m*/ * * @param url http://host+path+query * @param headers Http * @param bytes * @param appKey APP KEY * @param appSecret APP * @param timeout * @param signHeaderPrefixList ???Header? * @return * @throws Exception */ public static HttpResponse httpPost(String url, Map<String, String> headers, byte[] bytes, String appKey, String appSecret, int timeout, List<String> signHeaderPrefixList) throws Exception { headers = initialBasicHeader(headers, appKey, appSecret, HttpMethod.POST, url, null, signHeaderPrefixList); HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(timeout)); HttpPost post = new HttpPost(url); for (Map.Entry<String, String> e : headers.entrySet()) { post.addHeader(e.getKey(), e.getValue()); } if (bytes != null) { post.setEntity(new ByteArrayEntity(bytes)); } return httpClient.execute(post); }
From source file:org.sonatype.nexus.error.reporting.NexusPRConnector.java
private static HttpParams createHttpParams(final RemoteStorageContext ctx) { final HttpParams params = new BasicHttpParams(); // getting the timeout from RemoteStorageContext. The value we get depends on per-repo and global settings. // The value will "cascade" from repo level to global level, see implementation. int timeout = ctx.getRemoteConnectionSettings().getConnectionTimeout(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout); return params; }