List of usage examples for java.net HttpURLConnection setDoOutput
public void setDoOutput(boolean dooutput)
From source file:edu.dartmouth.cs.dartcard.HttpUtilities.java
private static HttpURLConnection makePostConnection(URL url, byte[] bytes) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false);/* ww w . j ava 2 s .c om*/ conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // Add in API key String authKey = new String(Base64.encodeBase64((key + ":").getBytes())); conn.setRequestProperty("Authorization", "Basic " + authKey); OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); return conn; }
From source file:de.cubeisland.engine.core.util.McUUID.java
private static HttpURLConnection postQuery(ArrayNode node, int page) throws IOException { HttpURLConnection con = (HttpURLConnection) new URL(MOJANG_API_URL_NAME_UUID + page).openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.setUseCaches(false);//from ww w . j av a 2 s . co m con.setDoInput(true); con.setDoOutput(true); DataOutputStream writer = new DataOutputStream(con.getOutputStream()); writer.write(node.toString().getBytes()); writer.close(); return con; }
From source file:com.googlecode.osde.internal.igoogle.IgCredentials.java
/** * Makes a HTTP POST request for authentication. * * @param emailUserName the name of the user * @param password the password of the user * @param loginTokenOfCaptcha CAPTCHA token (Optional) * @param loginCaptchaAnswer answer of CAPTCHA token (Optional) * @return http response as a String/*w w w .j a va 2 s . com*/ * @throws IOException */ // TODO: Refactor requestAuthentication() utilizing HttpPost. private static String requestAuthentication(String emailUserName, String password, String loginTokenOfCaptcha, String loginCaptchaAnswer) throws IOException { // Prepare connection. URL url = new URL(URL_GOOGLE_LOGIN); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"); logger.fine("url: " + url); // Form the POST params. StringBuilder params = new StringBuilder(); params.append("Email=").append(emailUserName).append("&Passwd=").append(password) .append("&source=OSDE-01&service=ig&accountType=HOSTED_OR_GOOGLE"); if (loginTokenOfCaptcha != null) { params.append("&logintoken=").append(loginTokenOfCaptcha); } if (loginCaptchaAnswer != null) { params.append("&logincaptcha=").append(loginCaptchaAnswer); } // Send POST via output stream. OutputStream outputStream = null; try { outputStream = urlConnection.getOutputStream(); outputStream.write(params.toString().getBytes(IgHttpUtil.ENCODING)); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } // Retrieve response. // TODO: Should the caller of this method need to know the responseCode? int responseCode = urlConnection.getResponseCode(); logger.fine("responseCode: " + responseCode); InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK) ? urlConnection.getInputStream() : urlConnection.getErrorStream(); logger.fine("inputStream: " + inputStream); try { String response = IOUtils.toString(inputStream, IgHttpUtil.ENCODING); return response; } finally { if (inputStream != null) { inputStream.close(); } } }
From source file:ilearnrw.utils.ServerHelperClass.java
public static String sendPost(String link, String urlParameters) throws Exception { String url = baseUrl + link;/*from w w w . j av a 2 s . c o m*/ URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Authorization", userNamePasswordBase64("api", "api")); con.setRequestProperty("Content-Type", "application/json;charset=utf-8"); con.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF8"); wr.write(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); }
From source file:com.zf.util.Post_NetNew.java
public static String pn(Map<String, String> pams) throws Exception { if (null == pams) { return ""; }//from ww w. j a va 2 s .c o m String strtmp = "url"; URL url = new URL(pams.get(strtmp)); pams.remove(strtmp); strtmp = "body"; String body = pams.get(strtmp); pams.remove(strtmp); strtmp = "POST"; if (StringUtils.isEmpty(body)) strtmp = "GET"; HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setUseCaches(false); httpConn.setRequestMethod(strtmp); for (String pam : pams.keySet()) { httpConn.setRequestProperty(pam, pams.get(pam)); } if ("POST".equals(strtmp)) { httpConn.setDoOutput(true); httpConn.setDoInput(true); DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream()); dos.writeBytes(body); dos.flush(); } int resultCode = httpConn.getResponseCode(); StringBuilder sb = new StringBuilder(); sb.append(resultCode).append("\n"); String readLine; InputStream stream; try { stream = httpConn.getInputStream(); } catch (Exception ignored) { stream = httpConn.getErrorStream(); } try { BufferedReader responseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); while ((readLine = responseReader.readLine()) != null) { sb.append(readLine).append("\n"); } } catch (Exception ignored) { } return sb.toString(); }
From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java
private static void addBodyIfExists(final HttpURLConnection connection, final Request<?> request) throws IOException, AuthFailureError { final byte[] body = request.getBody(); if (body != null) { connection.setDoOutput(true); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); final DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(body);/*from w w w .j a v a 2s. c om*/ out.close(); } }
From source file:com.ant.myteam.gcm.POST2GCM.java
public static void post(String apiKey, Content content) { try {//from w ww. ja v a2 s.c o m // 1. URL URL url = new URL("https://android.googleapis.com/gcm/send"); // 2. Open connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3. Specify POST method conn.setRequestMethod("POST"); // 4. Set the headers conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key=" + apiKey); conn.setDoOutput(true); // 5. Add JSON data into POST request body //`5.1 Use Jackson object mapper to convert Contnet object into JSON ObjectMapper mapper = new ObjectMapper(); // 5.2 Get connection output stream DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); // 5.3 Copy Content "JSON" into mapper.writeValue(wr, content); // 5.4 Send the request wr.flush(); // 5.5 close wr.close(); // 6. Get the response int responseCode = conn.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 7. Print result System.out.println(response.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:pushandroid.POST2GCM.java
public static void post(Content content) { try {//from w ww .ja v a2 s .c o m // 1. URL URL url = new URL(URL); // 2. Open connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3. Specify POST method conn.setRequestMethod("POST"); // 4. Set the headers conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key=" + apiKey); conn.setDoOutput(true); // 5. Add JSON data into POST request body //`5.1 Use Jackson object mapper to convert Contnet object into JSON ObjectMapper mapper = new ObjectMapper(); // 5.2 Get connection output stream DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); // 5.3 Copy Content "JSON" into mapper.writeValue(wr, content); // 5.4 Send the request wr.flush(); // 5.5 close wr.close(); // 6. Get the response int responseCode = conn.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 7. Print result System.out.println(response.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ee.ria.xroad.signer.certmanager.OcspClient.java
private static HttpURLConnection createConnection(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty(MimeUtils.HEADER_CONTENT_TYPE, MimeTypes.OCSP_REQUEST); connection.setRequestProperty("Accept", MimeTypes.OCSP_RESPONSE); connection.setDoOutput(true); connection.setConnectTimeout(CONNECT_TIMEOUT_MS); connection.setReadTimeout(READ_TIMEOUT_MS); connection.connect();/*w w w . j a v a 2 s .c o m*/ return connection; }
From source file:com.intellij.lang.jsgraphql.languageservice.JSGraphQLNodeLanguageServiceClient.java
private static <R> R executeRequest(Request request, Class<R> responseClass, @NotNull Project project, boolean setProjectDir) { URL url = getJSGraphQLNodeLanguageServiceInstance(project, setProjectDir); if (url == null) { return null; }/*w w w. j a va2 s . c o m*/ HttpURLConnection httpConnection = null; try { httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setConnectTimeout(50); httpConnection.setReadTimeout(1000); httpConnection.setRequestMethod("POST"); httpConnection.setDoOutput(true); httpConnection.setRequestProperty("Content-Type", "application/json"); httpConnection.connect(); try (OutputStreamWriter writer = new OutputStreamWriter(httpConnection.getOutputStream())) { final String jsonRequest = new Gson().toJson(request); writer.write(jsonRequest); writer.flush(); writer.close(); } if (httpConnection.getResponseCode() == 200) { if (responseClass == null) { return null; } try (InputStream inputStream = httpConnection.getInputStream()) { String jsonResponse = IOUtils.toString(inputStream, "UTF-8"); R response = new Gson().fromJson(jsonResponse, responseClass); return response; } } else { log.warn("Got error from JS GraphQL Language Service: HTTP " + httpConnection.getResponseCode() + ": " + httpConnection.getResponseMessage()); } } catch (IOException e) { log.warn("Unable to connect to dev server", e); } finally { if (httpConnection != null) { httpConnection.disconnect(); } } return null; }