URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); // Triggers POST. connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); try (OutputStream output = connection.getOutputStream()) { output.write(query.getBytes(charset)); } InputStream response = connection.getInputStream(); // ...
/** * The getOutputStream method from the URL. First an attempt is * made to get the URLConnection object for the URL. If that * succeeds, the getOutputStream method on the URLConnection * is returned. * * @return the OutputStream. */ public OutputStream getOutputStream() throws IOException { // get the url connection if it is available url_conn = url.openConnection(); if (url_conn != null) { url_conn.setDoOutput(true); return url_conn.getOutputStream(); } else return null; }
URL url = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); String responseMsg = con.getResponseMessage(); int response = con.getResponseCode();
public URLConnection getURLConnection() throws IOException { if (urlConnection == null) { if (proxy != null) { urlConnection = url.openConnection(proxy); } else { urlConnection = url.openConnection(); } urlConnection.setDoInput(false); urlConnection.setDoOutput(false); if (getMode().startsWith("r") || getMode().startsWith("w") || getMode().startsWith("a")) { urlConnection.setDoInput(true); } if (getMode().startsWith("w") || getMode().startsWith("a")) { urlConnection.setDoOutput(true); } } return urlConnection; }
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); FileBody fileBody = new FileBody(new File(fileName)); MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); multipartEntity.addPart("file", fileBody); connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); OutputStream out = connection.getOutputStream(); try { multipartEntity.writeTo(out); } finally { out.close(); } int status = connection.getResponseCode(); ...
URL url = new URL("http://www.example.com/resource"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestMethod("PUT"); OutputStreamWriter out = new OutputStreamWriter( httpCon.getOutputStream()); out.write("Resource content"); out.close(); httpCon.getInputStream();
static OutputStream output(URL url, Resource configDir) throws IOException { // check for file url if ("file".equalsIgnoreCase(url.getProtocol())) { File f = URLs.urlToFile(url); if (!f.isAbsolute()) { // make relative to config dir return configDir.get(f.getPath()).out(); } else { return new FileOutputStream(f); } } else { URLConnection cx = url.openConnection(); cx.setDoOutput(true); return cx.getOutputStream(); } }
@Test public void testPostSignedUrl() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } String blobName = "test-post-signed-url-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); assertNotNull(storage.create(blob)); URL url = storage.signUrl(blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST)); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.connect(); Blob remoteBlob = storage.get(BUCKET, blobName); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); }
/** * @see org.geotools.data.ows.HTTPClient#post(java.net.URL, java.io.InputStream, * java.lang.String) */ public HTTPResponse post( final URL url, final InputStream postContent, final String postContentType) throws IOException { URLConnection connection = openConnection(url); if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).setRequestMethod("POST"); } connection.setDoOutput(true); if (postContentType != null) { connection.setRequestProperty("Content-type", postContentType); } connection.connect(); OutputStream outputStream = connection.getOutputStream(); try { byte[] buff = new byte[512]; int count; while ((count = postContent.read(buff)) > -1) { outputStream.write(buff, 0, count); } } finally { outputStream.flush(); outputStream.close(); } return new SimpleHTTPResponse(connection); }
URL url = new URL("http://www.example.com/resource"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" ); httpCon.setRequestMethod("DELETE"); httpCon.connect();
URLConnection con = url.openConnection(); con.setDoOutput(true); con.setDoInput(false); con.connect(); return new StreamResult(con.getOutputStream());
HttpURLConnection httpUrlConnection = null; URL url = new URL("http://example.com/server.cgi"); httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setUseCaches(false); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.setRequestProperty("Connection", "Keep-Alive"); httpUrlConnection.setRequestProperty("Cache-Control", "no-cache"); httpUrlConnection.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + this.boundary);
URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); ) {
public class a { public static void main(String [] a) throws Exception { java.net.URLConnection c = new java.net.URL("https://mydomain.com/").openConnection(); c.setDoOutput(true); c.getOutputStream(); } }
public class a { public static void main(String [] a) throws Exception { java.net.URLConnection c = new java.net.URL("https://google.com/").openConnection(); c.setDoOutput(true); c.getOutputStream(); } }
String urlParameters = "param1=a¶m2=b¶m3=c"; byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); int postDataLength = postData.length; String request = "http://example.com/index.php"; URL url = new URL( request ); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setDoOutput( true ); conn.setInstanceFollowRedirects( false ); conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty( "charset", "utf-8"); conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength )); conn.setUseCaches( false ); try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) { wr.write( postData ); }
URL myURL = new URL(serviceURL); HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection(); String userCredentials = "username:password"; String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes())); myURLConnection.setRequestProperty ("Authorization", basicAuth); myURLConnection.setRequestMethod("POST"); myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length); myURLConnection.setRequestProperty("Content-Language", "en-US"); myURLConnection.setUseCaches(false); myURLConnection.setDoInput(true); myURLConnection.setDoOutput(true);
String rawData = "id=10"; String type = "application/x-www-form-urlencoded"; String encodedData = URLEncoder.encode( rawData ); URL u = new URL("http://www.example.com/page.php"); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty( "Content-Type", type ); conn.setRequestProperty( "Content-Length", String.valueOf(encodedData.length())); OutputStream os = conn.getOutputStream(); os.write(encodedData.getBytes());
URL url = new URL("http://www.example.com/login"); URLConnection con = url.openConnection(); HttpURLConnection http = (HttpURLConnection)con; http.setRequestMethod("POST"); // PUT is another valid option http.setDoOutput(true);
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection())); httpcon.setDoOutput(true); httpcon.setRequestProperty("Content-Type", "application/json"); httpcon.setRequestProperty("Accept", "application/json"); httpcon.setRequestMethod("POST"); httpcon.connect(); byte[] outputBytes = "{'value': 7.5}".getBytes("UTF-8"); OutputStream os = httpcon.getOutputStream(); os.write(outputBytes); os.close();