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

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

本文整理了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

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();
// ...

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

/**
 * 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;
}

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

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

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

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;
}

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

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();
...

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

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();

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

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();
  }
}

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

@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());
}

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

/**
 * @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);
}

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

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();

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

URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(false);
con.connect();
return new StreamResult(con.getOutputStream());

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

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);

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

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);
) {

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

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();
  }
}

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

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();
  }
}

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

String urlParameters  = "param1=a&param2=b&param3=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 );
}

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

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);

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

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());

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

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);

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

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();

相关文章

URLConnection类方法