List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:com.commonsware.cwac.updater.SimpleHttpVersionCheckStrategy.java
@Override public int getVersionCode() throws Exception { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); int result = -1; try {//from ww w .j av a 2s . c om conn.connect(); int status = conn.getResponseCode(); if (status == 200) { InputStream is = conn.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuilder buf = new StringBuilder(); String str; while ((str = in.readLine()) != null) { buf.append(str); buf.append('\n'); } in.close(); JSONObject json = new JSONObject(buf.toString()); result = json.getInt(JSON_VERSION_CODE); updateURL = json.getString(JSON_UPDATE_URL); } else { throw new RuntimeException(String.format("Received %d from server", status)); } } finally { conn.disconnect(); } return (result); }
From source file:gmusic.api.comm.HttpUrlConnector.java
@Override public String dispatchPost(URI address, String json) throws IOException, URISyntaxException { HttpURLConnection connection = prepareConnection(address, true, "POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.connect(); connection.getOutputStream().write(json.getBytes()); if (connection.getResponseCode() != 200) { throw new IllegalStateException("Statuscode " + connection.getResponseCode() + " not supported"); }/* ww w .ja va 2 s . co m*/ String response = IOUtils.toString(connection.getInputStream()); if (!isStartup) { return response; } return setupAuthentication(response); }
From source file:edu.wfu.inotado.helper.OAuthHelper.java
@Deprecated public void sendRequest(AuthStore authStore) throws Exception { OAuthConsumer consumer = cacheHelper.getFromCache(authStore.getSystemName(), consumerCache); URL url;//from w w w .jav a 2 s . co m url = new URL("https://vigrior.schoolchapters.com/api/v1/courses.json"); HttpURLConnection request = (HttpURLConnection) url.openConnection(); consumer.sign(request); System.out.println("Sending request..."); request.connect(); System.out.println(request.getContent()); InputStream in = (InputStream) request.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String result, line = reader.readLine(); result = line; while ((line = reader.readLine()) != null) { result += line; } System.out.println(result); System.out.println("Response: " + request.getResponseCode() + " " + request.getResponseMessage()); }
From source file:io.apiman.manager.ui.server.servlets.UrlFetchProxyServlet.java
/** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) *//*from www .j a va 2 s. c om*/ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String url = req.getHeader("X-Apiman-Url"); //$NON-NLS-1$ if (url == null) { resp.sendError(500, "No URL specified in X-Apiman-Url"); //$NON-NLS-1$ return; } URL remoteUrl = new URL(url); HttpURLConnection remoteConn = (HttpURLConnection) remoteUrl.openConnection(); InputStream remoteIS = null; OutputStream responseOS = null; try { remoteConn.connect(); Map<String, List<String>> headerFields = remoteConn.getHeaderFields(); for (String headerName : headerFields.keySet()) { if (headerName == null) { continue; } if (EXCLUDE_HEADERS.contains(headerName)) { continue; } String headerValue = remoteConn.getHeaderField(headerName); resp.setHeader(headerName, headerValue); } resp.setHeader("Cache-control", "no-cache, no-store, must-revalidate"); //$NON-NLS-1$ //$NON-NLS-2$ remoteIS = remoteConn.getInputStream(); responseOS = resp.getOutputStream(); IOUtils.copy(remoteIS, responseOS); resp.flushBuffer(); } catch (Exception e) { resp.sendError(500, e.getMessage()); } finally { IOUtils.closeQuietly(responseOS); IOUtils.closeQuietly(remoteIS); } }
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);/* w w w . j a va2s .c o m*/ conn.connect(); InputStream stream = conn.getInputStream(); return stream; }
From source file:org.esupportail.nfctagdroid.requestasync.CsnHttpRequestAsync.java
protected String doInBackground(String... params) { CsnMessageBean nfcMsg = new CsnMessageBean(); nfcMsg.setNumeroId(LocalStorage.getValue("numeroId")); nfcMsg.setCsn(params[0]);//from w ww . j a v a 2 s.c om ObjectMapper mapper = new ObjectMapper(); String jsonInString = null; try { jsonInString = mapper.writeValueAsString(nfcMsg); URL url = new URL(NfcTacDroidActivity.ESUP_NFC_TAG_SERVER_URL + "/csn-ws"); log.info("Will call csn-ws on : " + url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); conn.connect(); Writer writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); writer.write(jsonInString); writer.close(); InputStream inputStream = conn.getInputStream(); String response = ""; String line; BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); while ((line = br.readLine()) != null) { response += line; } conn.disconnect(); return response; } catch (Exception e) { throw new NfcTagDroidException(e); } }
From source file:com.gliffy.restunit.http.JavaHttp.java
/** Performs an HTTP PUT. * @param request the request describing the PUT. * @return a response// w w w . ja va 2 s . c o m * @throws IOException if HttpURLConnection generated an IO Exception */ public HttpResponse put(HttpRequest request) throws IOException { HttpURLConnection connection = getConnection(request); connection.setRequestMethod("PUT"); connection.setDoOutput(true); connection.connect(); setBody(request, connection); HttpResponse response = createResponse(connection); connection.disconnect(); return response; }
From source file:com.gliffy.restunit.http.JavaHttp.java
/** Performs an HTTP POST. * @param request the request describing the POST. * @return a response//from ww w . j a va2 s. c o m * @throws IOException if HttpURLConnection generated an IO Exception */ public HttpResponse post(HttpRequest request) throws IOException { HttpURLConnection connection = getConnection(request); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.connect(); setBody(request, connection); HttpResponse response = createResponse(connection); connection.disconnect(); return response; }
From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettyKitchenSinkTest.java
protected int fetchResponseCode(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); return connection.getResponseCode(); }
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./* ww w .j av a 2 s.c om*/ * * @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; }