java.net.URLConnection.setDoOutput()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(317)

本文整理了Java中java.net.URLConnection.setDoOutput()方法的一些代码示例,展示了URLConnection.setDoOutput()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.setDoOutput()方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:setDoOutput

URLConnection.setDoOutput介绍

[英]Sets the flag indicating whether this URLConnection allows output. It cannot be set after the connection is established.
[中]设置指示此URLConnection是否允许输出的标志。连接建立后无法设置。

代码示例

代码示例来源:origin: stackoverflow.com

  1. URLConnection connection = new URL(url).openConnection();
  2. connection.setDoOutput(true); // Triggers POST.
  3. connection.setRequestProperty("Accept-Charset", charset);
  4. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
  5. try (OutputStream output = connection.getOutputStream()) {
  6. output.write(query.getBytes(charset));
  7. }
  8. InputStream response = connection.getInputStream();
  9. // ...

代码示例来源:origin: javax.activation/activation

  1. /**
  2. * The getOutputStream method from the URL. First an attempt is
  3. * made to get the URLConnection object for the URL. If that
  4. * succeeds, the getOutputStream method on the URLConnection
  5. * is returned.
  6. *
  7. * @return the OutputStream.
  8. */
  9. public OutputStream getOutputStream() throws IOException {
  10. // get the url connection if it is available
  11. url_conn = url.openConnection();
  12. if (url_conn != null) {
  13. url_conn.setDoOutput(true);
  14. return url_conn.getOutputStream();
  15. } else
  16. return null;
  17. }

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL("http://www.google.com");
  2. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  3. con.setDoOutput(true);
  4. String responseMsg = con.getResponseMessage();
  5. int response = con.getResponseCode();

代码示例来源:origin: jphp-group/jphp

  1. public URLConnection getURLConnection() throws IOException {
  2. if (urlConnection == null) {
  3. if (proxy != null) {
  4. urlConnection = url.openConnection(proxy);
  5. } else {
  6. urlConnection = url.openConnection();
  7. }
  8. urlConnection.setDoInput(false);
  9. urlConnection.setDoOutput(false);
  10. if (getMode().startsWith("r") || getMode().startsWith("w") || getMode().startsWith("a")) {
  11. urlConnection.setDoInput(true);
  12. }
  13. if (getMode().startsWith("w") || getMode().startsWith("a")) {
  14. urlConnection.setDoOutput(true);
  15. }
  16. }
  17. return urlConnection;
  18. }

代码示例来源:origin: stackoverflow.com

  1. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  2. connection.setDoOutput(true);
  3. connection.setRequestMethod("POST");
  4. FileBody fileBody = new FileBody(new File(fileName));
  5. MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
  6. multipartEntity.addPart("file", fileBody);
  7. connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());
  8. OutputStream out = connection.getOutputStream();
  9. try {
  10. multipartEntity.writeTo(out);
  11. } finally {
  12. out.close();
  13. }
  14. int status = connection.getResponseCode();
  15. ...

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL("http://www.example.com/resource");
  2. HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
  3. httpCon.setDoOutput(true);
  4. httpCon.setRequestMethod("PUT");
  5. OutputStreamWriter out = new OutputStreamWriter(
  6. httpCon.getOutputStream());
  7. out.write("Resource content");
  8. out.close();
  9. httpCon.getInputStream();

代码示例来源:origin: geoserver/geoserver

  1. static OutputStream output(URL url, Resource configDir) throws IOException {
  2. // check for file url
  3. if ("file".equalsIgnoreCase(url.getProtocol())) {
  4. File f = URLs.urlToFile(url);
  5. if (!f.isAbsolute()) {
  6. // make relative to config dir
  7. return configDir.get(f.getPath()).out();
  8. } else {
  9. return new FileOutputStream(f);
  10. }
  11. } else {
  12. URLConnection cx = url.openConnection();
  13. cx.setDoOutput(true);
  14. return cx.getOutputStream();
  15. }
  16. }

代码示例来源:origin: googleapis/google-cloud-java

  1. @Test
  2. public void testPostSignedUrl() throws IOException {
  3. if (storage.getOptions().getCredentials() != null) {
  4. assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner);
  5. }
  6. String blobName = "test-post-signed-url-blob";
  7. BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
  8. assertNotNull(storage.create(blob));
  9. URL url =
  10. storage.signUrl(blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST));
  11. URLConnection connection = url.openConnection();
  12. connection.setDoOutput(true);
  13. connection.connect();
  14. Blob remoteBlob = storage.get(BUCKET, blobName);
  15. assertNotNull(remoteBlob);
  16. assertEquals(blob.getBucket(), remoteBlob.getBucket());
  17. assertEquals(blob.getName(), remoteBlob.getName());
  18. }

代码示例来源:origin: geotools/geotools

  1. /**
  2. * @see org.geotools.data.ows.HTTPClient#post(java.net.URL, java.io.InputStream,
  3. * java.lang.String)
  4. */
  5. public HTTPResponse post(
  6. final URL url, final InputStream postContent, final String postContentType)
  7. throws IOException {
  8. URLConnection connection = openConnection(url);
  9. if (connection instanceof HttpURLConnection) {
  10. ((HttpURLConnection) connection).setRequestMethod("POST");
  11. }
  12. connection.setDoOutput(true);
  13. if (postContentType != null) {
  14. connection.setRequestProperty("Content-type", postContentType);
  15. }
  16. connection.connect();
  17. OutputStream outputStream = connection.getOutputStream();
  18. try {
  19. byte[] buff = new byte[512];
  20. int count;
  21. while ((count = postContent.read(buff)) > -1) {
  22. outputStream.write(buff, 0, count);
  23. }
  24. } finally {
  25. outputStream.flush();
  26. outputStream.close();
  27. }
  28. return new SimpleHTTPResponse(connection);
  29. }

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL("http://www.example.com/resource");
  2. HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
  3. httpCon.setDoOutput(true);
  4. httpCon.setRequestProperty(
  5. "Content-Type", "application/x-www-form-urlencoded" );
  6. httpCon.setRequestMethod("DELETE");
  7. httpCon.connect();

代码示例来源:origin: javax.xml.bind/jaxb-api

  1. URLConnection con = url.openConnection();
  2. con.setDoOutput(true);
  3. con.setDoInput(false);
  4. con.connect();
  5. return new StreamResult(con.getOutputStream());

代码示例来源:origin: stackoverflow.com

  1. HttpURLConnection httpUrlConnection = null;
  2. URL url = new URL("http://example.com/server.cgi");
  3. httpUrlConnection = (HttpURLConnection) url.openConnection();
  4. httpUrlConnection.setUseCaches(false);
  5. httpUrlConnection.setDoOutput(true);
  6. httpUrlConnection.setRequestMethod("POST");
  7. httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
  8. httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
  9. httpUrlConnection.setRequestProperty(
  10. "Content-Type", "multipart/form-data;boundary=" + this.boundary);

代码示例来源:origin: stackoverflow.com

  1. URLConnection connection = new URL(url).openConnection();
  2. connection.setDoOutput(true);
  3. connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
  4. OutputStream output = connection.getOutputStream();
  5. PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
  6. ) {

代码示例来源:origin: stackoverflow.com

  1. public class a {
  2. public static void main(String [] a) throws Exception {
  3. java.net.URLConnection c = new java.net.URL("https://mydomain.com/").openConnection();
  4. c.setDoOutput(true);
  5. c.getOutputStream();
  6. }
  7. }

代码示例来源:origin: stackoverflow.com

  1. public class a {
  2. public static void main(String [] a) throws Exception {
  3. java.net.URLConnection c = new java.net.URL("https://google.com/").openConnection();
  4. c.setDoOutput(true);
  5. c.getOutputStream();
  6. }
  7. }

代码示例来源:origin: stackoverflow.com

  1. String urlParameters = "param1=a&param2=b&param3=c";
  2. byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
  3. int postDataLength = postData.length;
  4. String request = "http://example.com/index.php";
  5. URL url = new URL( request );
  6. HttpURLConnection conn= (HttpURLConnection) url.openConnection();
  7. conn.setDoOutput( true );
  8. conn.setInstanceFollowRedirects( false );
  9. conn.setRequestMethod( "POST" );
  10. conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
  11. conn.setRequestProperty( "charset", "utf-8");
  12. conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
  13. conn.setUseCaches( false );
  14. try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
  15. wr.write( postData );
  16. }

代码示例来源:origin: stackoverflow.com

  1. URL myURL = new URL(serviceURL);
  2. HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
  3. String userCredentials = "username:password";
  4. String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
  5. myURLConnection.setRequestProperty ("Authorization", basicAuth);
  6. myURLConnection.setRequestMethod("POST");
  7. myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  8. myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
  9. myURLConnection.setRequestProperty("Content-Language", "en-US");
  10. myURLConnection.setUseCaches(false);
  11. myURLConnection.setDoInput(true);
  12. myURLConnection.setDoOutput(true);

代码示例来源:origin: stackoverflow.com

  1. String rawData = "id=10";
  2. String type = "application/x-www-form-urlencoded";
  3. String encodedData = URLEncoder.encode( rawData );
  4. URL u = new URL("http://www.example.com/page.php");
  5. HttpURLConnection conn = (HttpURLConnection) u.openConnection();
  6. conn.setDoOutput(true);
  7. conn.setRequestMethod("POST");
  8. conn.setRequestProperty( "Content-Type", type );
  9. conn.setRequestProperty( "Content-Length", String.valueOf(encodedData.length()));
  10. OutputStream os = conn.getOutputStream();
  11. os.write(encodedData.getBytes());

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL("http://www.example.com/login");
  2. URLConnection con = url.openConnection();
  3. HttpURLConnection http = (HttpURLConnection)con;
  4. http.setRequestMethod("POST"); // PUT is another valid option
  5. http.setDoOutput(true);

代码示例来源:origin: stackoverflow.com

  1. HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
  2. httpcon.setDoOutput(true);
  3. httpcon.setRequestProperty("Content-Type", "application/json");
  4. httpcon.setRequestProperty("Accept", "application/json");
  5. httpcon.setRequestMethod("POST");
  6. httpcon.connect();
  7. byte[] outputBytes = "{'value': 7.5}".getBytes("UTF-8");
  8. OutputStream os = httpcon.getOutputStream();
  9. os.write(outputBytes);
  10. os.close();

相关文章

URLConnection类方法