List of usage examples for java.net HttpURLConnection setDoOutput
public void setDoOutput(boolean dooutput)
From source file:com.google.api.GoogleAPI.java
/** * Forms an HTTP request, sends it using POST method and returns the result of the request as a JSONObject. * /* w ww . j a v a2s .co m*/ * @param url The URL to query for a JSONObject. * @param parameters Additional POST parameters * @return The translated String. * @throws Exception on error. */ protected static JSONObject retrieveJSON(final URL url, final String parameters) throws Exception { try { final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("referer", referrer); uc.setRequestMethod("POST"); uc.setDoOutput(true); final PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.write(parameters); pw.flush(); try { final String result = inputStreamToString(uc.getInputStream()); return new JSONObject(result); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) { uc.getErrorStream().close(); } pw.close(); } } catch (Exception ex) { throw new Exception("[google-api-translate-java] Error retrieving translation.", ex); } }
From source file:Main.java
private static void downloadResource(String from, String to) { OutputStream outputStream = null; BufferedInputStream inputStream = null; HttpURLConnection connection = null; URL url;//from w w w . ja v a2 s .c o m byte[] buffer = new byte[1024]; try { url = new URL(from); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); outputStream = new FileOutputStream(to); inputStream = new BufferedInputStream(url.openStream()); int read; while ((read = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, read); } } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) try { outputStream.close(); } catch (IOException e) { } if (inputStream != null) try { inputStream.close(); } catch (IOException e) { } if (connection != null) connection.disconnect(); } }
From source file:core.RESTCalls.RESTPost.java
public static String httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true);//w w w . j a v a 2 s. co m conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStream out = conn.getOutputStream(); Writer writer = new OutputStreamWriter(out, "UTF-8"); for (int i = 0; i < paramName.length; i++) { writer.write(paramName[i]); writer.write("="); writer.write(URLEncoder.encode(paramVal[i], "UTF-8")); writer.write("&"); } writer.close(); out.close(); 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 + "\n"); rd.close(); conn.disconnect(); return sb.toString(); }
From source file:ai.eve.volley.stack.HurlStack.java
private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { connection.setDoOutput(true); connection.setRequestProperty("connection", "Keep-Alive"); connection.addRequestProperty(HTTP.CONTENT_TYPE, request.getBodyContentType()); request.getBody(connection);/* w w w .j ava 2s .c o m*/ }
From source file:io.fares.junit.soapui.outside.test.SoapUIMockRunnerTest.java
public static String testMockService(String endpoint) throws MalformedURLException, IOException { HttpURLConnection con = (HttpURLConnection) new URL(endpoint).openConnection(); con.setRequestMethod("POST"); con.addRequestProperty("Accept", "application/soap+xml"); con.addRequestProperty("Content-Type", "application/soap+xml"); con.setDoOutput(true); con.getOutputStream().write(/*from w w w . jav a 2 s. c om*/ "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header/><soap:Body><GetWeather xmlns=\"http://www.webserviceX.NET\"><CityName>Brisbane</CityName></GetWeather></soap:Body></soap:Envelope>" .getBytes("UTF-8")); InputStream is = con.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); String rs = writer.toString(); LOG.fine(rs); return rs; }
From source file:GoogleAPI.java
/** * Forms an HTTP request, sends it using GET method and returns the result of the request as a JSONObject. * /*from ww w. j a v a 2 s. c om*/ * @param url The URL to query for a JSONObject. * @return The translated String. * @throws Exception on error. */ protected static JSONObject retrieveJSON(final URL url) throws Exception { try { final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("referer", referrer); uc.setRequestMethod("GET"); uc.setDoOutput(true); try { final String result = inputStreamToString(uc.getInputStream()); return new JSONObject(result); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) { uc.getErrorStream().close(); } } } catch (Exception ex) { throw new Exception("[google-api-translate-java] Error retrieving translation.", ex); } }
From source file:com.scut.easyfe.network.kjFrame.http.HttpConnectStack.java
/** * body//from w ww. java2 s . co m */ private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException { byte[] body = request.getBody(); if (body != null) { connection.setDoOutput(true); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(body); out.close(); } }
From source file:com.example.admin.processingboilerplate.JsonIO.java
public static JSONObject pushJson(String requestURL, String jsonDataName, JSONObject json) { try {/*from w w w . java 2 s . com*/ String boundary = "===" + System.currentTimeMillis() + "==="; URL url = new URL(requestURL); //HttpURLConnection con = (HttpURLConnection) url.openConnection(); URLConnection uc = (url).openConnection(); HttpURLConnection con = requestURL.startsWith("https") ? (HttpsURLConnection) uc : (HttpURLConnection) uc; con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); con.setRequestProperty("User-Agent", USER_AGENT); OutputStream outputStream = con.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true); writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"" + "data" + "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF); writer.append(json.toString()).append(CRLF); writer.flush(); writer.append(CRLF).flush(); writer.append("--" + boundary + "--").append(CRLF); writer.close(); int status = con.getResponseCode(); if (status == HttpURLConnection.HTTP_OK) { StringBuilder sb = load(con); try { json = new JSONObject(sb.toString()); return json; } catch (JSONException e) { Log.e("loadJson", e.getMessage(), e); e.printStackTrace(); return new JSONObject(); } } else { throw new IOException("Server returned non-OK status: " + status); } } catch (IOException e) { e.printStackTrace(); } return new JSONObject(); }
From source file:com.teleca.jamendo.util.download.DownloadTask.java
public static Boolean downloadFile(DownloadJob job) throws IOException { // TODO rewrite to apache client PlaylistEntry mPlaylistEntry = job.getPlaylistEntry(); String mDestination = job.getDestination(); URL u = new URL(mPlaylistEntry.getTrack().getStream()); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect();// w w w . j a v a2 s .c o m job.setTotalSize(c.getContentLength()); Log.i(JamendoApplication.TAG, "creating file"); String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination); String fileName = DownloadHelper.getFileName(mPlaylistEntry, job.getFormat()); try { // Create multiple directory boolean success = (new File(path)).mkdirs(); if (success) { Log.i(JamendoApplication.TAG, "Directory: " + path + " created"); } } catch (Exception e) {//Catch exception if any Log.e(JamendoApplication.TAG, "Error creating folder", e); return false; } FileOutputStream f = new FileOutputStream(new File(path, fileName)); InputStream in = c.getInputStream(); if (in == null) { // When InputStream is a NULL return false; } byte[] buffer = new byte[1024]; int lenght = 0; while ((lenght = in.read(buffer)) > 0) { f.write(buffer, 0, lenght); job.setDownloadedSize(job.getDownloadedSize() + lenght); } f.close(); downloadCover(job); return true; }
From source file:Main.java
public static String UpdateScore(String userName, int br) { String retStr = ""; try {/*from w ww . j a v a 2 s.co m*/ URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/AzurirajHighScore.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(15000); conn.setReadTimeout(10000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); Log.e("http", "por1"); JSONObject data = new JSONObject(); data.put("username", userName); data.put("broj", br); Log.e("http", "por3"); Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString()); String query = builder.build().getEncodedQuery(); Log.e("http", "por4"); OutputStream os = conn.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); bw.write(query); Log.e("http", "por5"); bw.flush(); bw.close(); os.close(); int responseCode = conn.getResponseCode(); Log.e("http", String.valueOf(responseCode)); if (responseCode == HttpURLConnection.HTTP_OK) { retStr = inputStreamToString(conn.getInputStream()); } else retStr = String.valueOf("Error: " + responseCode); Log.e("http", retStr); } catch (Exception e) { Log.e("http", "greska"); } return retStr; }