List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:com.zzl.zl_app.cache.Utility.java
public static boolean loadFile(String loadpath, String fileName, String savePath, Context context, String broadcastAction) { FileOutputStream fos = null; // ? FileInputStream fis = null; // ? InputStream is = null; // ? HttpURLConnection httpConnection = null; int readLength = 0; // ?? int file_length = 0; URL url = null;// www.j a v a2s . c o m try { url = new URL(loadpath); httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setConnectTimeout(10000); httpConnection.setRequestMethod("GET"); is = httpConnection.getInputStream(); FileTools.creatDir(savePath); String filePath = savePath + fileName; FileTools.deleteFile(filePath); FileTools.creatFile(filePath); File download_file = new File(filePath); fos = new FileOutputStream(download_file, true); // ?? fis = new FileInputStream(download_file); // ?? int total_read = fis.available(); // ??0 file_length = httpConnection.getContentLength(); // ? if (is == null) { // ? Tools.log("Voice", "donload failed..."); return false; } byte buf[] = new byte[3072]; // readLength = 0; // Tools.log("Voice", "download start..."); Intent startIntent = new Intent(); Bundle b = new Bundle(); if (broadcastAction != null) { // ????? b.putInt("fileSize", file_length); b.putInt("progress", 0); startIntent.putExtras(b); startIntent.setAction(broadcastAction); context.sendBroadcast(startIntent); } // ????? while (readLength != -1) { if ((readLength = is.read(buf)) > 0) { fos.write(buf, 0, readLength); total_read += readLength; // } if (broadcastAction != null) { b.putInt("fileSize", file_length); b.putInt("progress", total_read); startIntent.putExtras(b); startIntent.setAction(broadcastAction); context.sendBroadcast(startIntent); } if (total_read == file_length) { // ? Tools.log("Voice", "download complete..."); // ?????? if (broadcastAction != null) { Intent completeIntent = new Intent(); b.putBoolean("isFinish", true); completeIntent.putExtras(b); completeIntent.setAction(broadcastAction); context.sendBroadcast(completeIntent); } } // Thread.sleep(10); // ?10 } } catch (Exception e) { if (broadcastAction != null) { Intent errorIntent = new Intent(); errorIntent.setAction(broadcastAction); context.sendBroadcast(errorIntent); e.printStackTrace(); } } finally { try { if (fos != null) { fos.close(); } if (fis != null) { is.close(); } if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; }
From source file:com.orchestra.portale.external.services.manager.BikeSharingService.java
@Override public String getResponse(Map<String, String[]> mapParams) { try {/*w w w . java2 s. c o m*/ HttpURLConnection urlConnection = (HttpURLConnection) new URL(baseUrl).openConnection(); urlConnection.setConnectTimeout(15000); urlConnection.setReadTimeout(30000); urlConnection.addRequestProperty("Accept-Language", Locale.getDefault().toString().replace('_', '-')); String result = IOUtils.toString(urlConnection.getInputStream()); urlConnection.disconnect(); return result; } catch (IOException e) { return "response{code:1,error:" + e.getMessage() + "}"; } }
From source file:com.aperlambda.apercommon.connection.http.HttpRequestSender.java
private HttpURLConnection createConnection(HttpRequest request) throws IOException { URL url = new URL(request.getUrl()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout);//from ww w. jav a2s.c o m connection.setRequestMethod(sendMethod); return connection; }
From source file:com.example.android.networkconnect.DownloadUrl.java
public InputStream downloadUrl(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true);/*from ww w . ja v a2 s . co m*/ conn.connect(); InputStream stream = conn.getInputStream(); return stream; }
From source file:com.ris.mobile.ecloud.util.AbFileUtil.java
/** * ????./*from w ww . ja v a2s . c o m*/ * * @param Url * @return int ? */ public static int getContentLengthFromUrl(String Url) { int mContentLength = 0; try { URL url = new URL(Url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) url.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", Url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { // ???? mContentLength = mHttpURLConnection.getContentLength(); } } catch (Exception e) { e.printStackTrace(); //AbLogUtil.d(AbFileUtil.class, "?"+e.getMessage()); } return mContentLength; }
From source file:URLMonitorPanel.java
public void run() { if (System.currentTimeMillis() - scheduledExecutionTime() > 5000) { // Let the next task do it return;// w ww .ja v a2 s . com } try { HttpURLConnection huc = (HttpURLConnection) url.openConnection(); huc.setConnectTimeout(1000); huc.setReadTimeout(1000); int code = huc.getResponseCode(); if (updater != null) updater.isAlive(true); } catch (Exception e) { if (updater != null) updater.isAlive(false); } }
From source file:it.polito.tellmefirst.apimanager.RestManager.java
public String getStringFromAPI(String urlStr) { LOG.debug("[getStringFromAPI] - BEGIN - url=" + urlStr); String result = ""; try {//from ww w . j a va 2s .c om LOG.debug("[getStringFromAPI] - http.proxyHost=" + System.getProperty("http.proxyHost")); LOG.debug("[getStringFromAPI] - http.proxyPort=" + System.getProperty("http.proxyPort")); LOG.debug("[getStringFromAPI] - https.proxyHost=" + System.getProperty("https.proxyHost")); LOG.debug("[getStringFromAPI] - https.proxyPort=" + System.getProperty("https.proxyPort")); LOG.debug("[getStringFromAPI] - http.nonProxyHosts=" + System.getProperty("http.nonProxyHosts")); URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(15000); //set timeout to 15 seconds conn.setReadTimeout(15000); //set timeout to 15 seconds if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); result = sb.toString(); } catch (Exception e) { LOG.error("[getStringFromAPI] - EXCEPTION for url=" + urlStr, e); } LOG.debug("[getStringFromAPI] - END"); return result; }
From source file:at.mukprojects.giphy4j.dao.HttpRequestSender.java
private HttpURLConnection createConnection(Request request) throws IOException { URL url = new URL(request.getUrl()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(Giphy4JConstants.TIMEOUT); connection.setReadTimeout(Giphy4JConstants.TIMEOUT); connection.setRequestMethod(Giphy4JConstants.SEND_METHOD); return connection; }
From source file:com.bnrc.util.AbFileUtil.java
/** * ????.// www.j a v a 2s.c o m * * @param Url ??? * @return int ????? */ public static int getContentLengthFromUrl(String Url) { int mContentLength = 0; try { URL url = new URL(Url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) url.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", Url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { // ?????? mContentLength = mHttpURLConnection.getContentLength(); } } catch (Exception e) { e.printStackTrace(); AbLogUtil.d(AbFileUtil.class, "" + e.getMessage()); } return mContentLength; }
From source file:org.peterbaldwin.vlcremote.net.ServerConnectionTest.java
@Override protected Integer doInBackground(Server... servers) { if (servers == null || servers.length != 1) { return -1; }//from ww w . ja va 2 s. c o m URL url; try { url = new URL("http://" + servers[0].getUri().getAuthority() + TEST_PATH); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(1000); try { Header auth = BasicScheme.authenticate( new UsernamePasswordCredentials(servers[0].getUser(), servers[0].getPassword()), HTTP.UTF_8, false); connection.setRequestProperty(auth.getName(), auth.getValue()); return connection.getResponseCode(); } finally { connection.disconnect(); } } catch (IOException ex) { } return -1; }