List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:cn.org.eshow.framwork.util.AbFileUtil.java
/** * ????.//from ww w.j a va 2s. c om * @param url ? * @return ?? */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (AbStrUtil.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.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", ""); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0;; i++) { String mine = mHttpURLConnection.getHeaderField(i); if (mine == null) { break; } if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase()); if (m.find()) return m.group(1).replace("\"", ""); } } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "???"); } return name; }
From source file:UrlEngine.java
@Override public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception { System.setProperty("http.keepAlive", "true"); ResponseEntity<String> responseEntity = null; URL obj = new URL(requestOptions.getUrl()); for (int i = 0; i < requestOptions.getCount(); i++) { HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // add request header con.setRequestMethod("GET"); con.setConnectTimeout(2000); for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) { con.setRequestProperty(e.getKey(), e.getValue()); }/* ww w.ja v a 2s . c o m*/ System.out.println("\nSending 'GET' request to URL : " + requestOptions.getUrl()); con.connect(); int responseCode = con.getResponseCode(); System.out.println("Response Code : " + responseCode); final InputStream is = con.getInputStream(); String response = IOUtils.toString(is); is.close(); //print result System.out.println(response); responseEntity = new ResponseEntity<String>(response, HttpStatus.valueOf(responseCode)); } return responseEntity; }
From source file:eu.codeplumbers.cosi.api.tasks.DeleteDocumentTask.java
@Override protected String doInBackground(Void... voids) { URL urlO = null;/*from w w w. j ava 2 s . c o m*/ try { urlO = new URL(url + remoteId + "/"); HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Authorization", authHeader); conn.setDoOutput(false); conn.setDoInput(true); conn.setRequestMethod("DELETE"); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); result = writer.toString(); in.close(); conn.disconnect(); } catch (MalformedURLException e) { result = "error"; e.printStackTrace(); errorMessage = e.getLocalizedMessage(); } catch (ProtocolException e) { result = "error"; errorMessage = e.getLocalizedMessage(); e.printStackTrace(); } catch (IOException e) { result = "error"; errorMessage = e.getLocalizedMessage(); e.printStackTrace(); } return result; }
From source file:com.micro.utils.F.java
/** * ????.//from ww w . jav 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(); L.D("?" + e.getMessage()); } return mContentLength; }
From source file:com.adguard.commons.web.UrlUtils.java
/** * Downloads content from the specified url using specified proxy (or do not using it) and timeouts. * Returns null if there's an error.//w ww .j a v a 2 s . c o m * * @param url url * @param proxy proxy to use * @param readTimeout read timeout * @param socketTimeout connection timeout * @return Downloaded string */ public static String downloadString(URL url, Proxy proxy, int readTimeout, int socketTimeout, String encoding, int triesCount) { IOException lastException = null; for (int i = 0; i < triesCount; i++) { lastException = null; HttpURLConnection connection = null; InputStream inputStream = null; try { connection = (HttpURLConnection) (proxy == null ? url.openConnection() : url.openConnection(proxy)); connection.setReadTimeout(readTimeout); connection.setConnectTimeout(socketTimeout); connection.connect(); inputStream = connection.getInputStream(); return IOUtils.toString(inputStream, encoding); } catch (IOException ex) { if (LOG.isDebugEnabled()) { LOG.warn("Error downloading string from {}. Try number: {}. Cause:\r\n", url, triesCount, ex); } else { LOG.warn("Error downloading string from {}. Try number: {}. Cause:{}", url, triesCount, ex.getMessage()); } lastException = ex; } finally { IOUtils.closeQuietly(inputStream); if (connection != null) { connection.disconnect(); } } } if (lastException != null) { LOG.error("Could not download string from url: " + url, lastException); } return null; }
From source file:com.pursuer.reader.easyrss.network.NetworkClient.java
public InputStream doGetStream(final String url) throws Exception { final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setConnectTimeout(40 * 1000); conn.setReadTimeout(30 * 1000);/* w w w . j a va 2s. co m*/ conn.setRequestMethod("GET"); if (auth != null) { conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth); } try { final int resStatus = conn.getResponseCode(); if (resStatus == HttpStatus.SC_UNAUTHORIZED) { ReaderAccountMgr.getInstance().invalidateAuth(); } if (resStatus != HttpStatus.SC_OK) { throw new NetworkException("Invalid HTTP status " + resStatus + ": " + url + "."); } } catch (final Exception exception) { if (exception.getMessage() != null && exception.getMessage().contains("authentication")) { ReaderAccountMgr.getInstance().invalidateAuth(); } throw exception; } return conn.getInputStream(); }
From source file:com.boundary.camel.component.url.UrlClient.java
private void configureConnection(HttpURLConnection connection) throws ProtocolException { connection.setConnectTimeout(configuration.getTimeout()); connection.setRequestMethod(configuration.getRequestMethod()); connection.setInstanceFollowRedirects(configuration.getFollowRedirects()); if (configuration.getUser() != null || configuration.getPassword() != null) { StringBuffer sb = new StringBuffer(); if (configuration.getUser() != null) { sb.append(configuration.getUser()); }// www . ja v a 2s . co m sb.append(":"); if (configuration.getPassword() != null) { sb.append(configuration.getPassword()); } connection.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64String(sb.toString().getBytes())); } }
From source file:com.shopgun.android.sdk.network.impl.HttpURLNetwork.java
private HttpURLConnection openConnection(Request<?> request, URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(request.getTimeOut()); connection.setReadTimeout(request.getTimeOut()); connection.setUseCaches(false);/*from ww w. java2 s. c o m*/ connection.setDoInput(true); connection.setInstanceFollowRedirects(false); setHeaders(request, connection); setRequestMethod(connection, request); return connection; }
From source file:org.freshrss.easyrss.network.NetworkClient.java
private HttpURLConnection makeConnection(final String url) throws MalformedURLException, IOException { final HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(url).openConnection()); httpURLConnection.setConnectTimeout(40 * 1000); httpURLConnection.setReadTimeout(30 * 1000); if (url.toLowerCase(Locale.US).startsWith("https://")) { final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection; httpsURLConnection.setSSLSocketFactory(this.sslSocketFactory); }/*from w w w . j a v a 2 s . c om*/ return httpURLConnection; }
From source file:com.pursuer.reader.easyrss.network.NetworkClient.java
public InputStream doPostStream(final String url, final String params) throws IOException, NetworkException { final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setConnectTimeout(40 * 1000); conn.setReadTimeout(30 * 1000);/*from w w w .j a v a 2 s . com*/ conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); if (auth != null) { conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth); } conn.setDoInput(true); conn.setDoOutput(true); final OutputStream output = conn.getOutputStream(); output.write(params.getBytes()); output.flush(); output.close(); conn.connect(); try { final int resStatus = conn.getResponseCode(); if (resStatus == HttpStatus.SC_UNAUTHORIZED) { ReaderAccountMgr.getInstance().invalidateAuth(); } if (resStatus != HttpStatus.SC_OK) { throw new NetworkException("Invalid HTTP status " + resStatus + ": " + url + "."); } } catch (final IOException exception) { if (exception.getMessage() != null && exception.getMessage().contains("authentication")) { ReaderAccountMgr.getInstance().invalidateAuth(); } throw exception; } return conn.getInputStream(); }