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

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

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

URLConnection.getOutputStream介绍

[英]Returns an OutputStream for writing data to this URLConnection. It throws an UnknownServiceException by default. This method must be overridden by its subclasses.
[中]返回用于将数据写入此URLConnection的OutputStream。默认情况下,它会抛出UnknownServiceException。此方法必须由其子类重写。

代码示例

代码示例来源: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: stackoverflow.com

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
    new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();

conn.connect();

代码示例来源: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

OkHttpClient client = new OkHttpClient();
OutputStream out = null;
try {
  URL url = new URL("http://www.example.com");
  HttpURLConnection connection = client.open(url);
  for (Map.Entry<String, String> entry : multipart.getHeaders().entrySet()) {
    connection.addRequestProperty(entry.getKey(), entry.getValue());
  }
  connection.setRequestMethod("POST");
  // Write the request.
  out = connection.getOutputStream();
  multipart.writeBodyTo(out);
  out.close();

  // Read the response.
  if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new IOException("Unexpected HTTP response: "
        + connection.getResponseCode() + " " + connection.getResponseMessage());
  }
} finally {
  // Clean up.
  try {
    if (out != null) out.close();
  } catch (Exception e) {
  }
}

代码示例来源: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: xalan/xalan

url = new URL(systemId);
_tohFactory.setOutputStream(
  _ostream = new FileOutputStream(url.getFile()));
    url = new URL(systemId);
    final URLConnection connection = url.openConnection();
_tohFactory.setOutputStream(_ostream = connection.getOutputStream());
return _tohFactory.getSerializationHandler();

代码示例来源: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: 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

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: 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

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

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

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

HttpURLConnection con = (HttpURLConnection) new URL("https://www.example.com").openConnection();
con.setRequestMethod("POST");
con.getOutputStream().write("LOGIN".getBytes("UTF-8"));
con.getInputStream();

代码示例来源: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();

代码示例来源: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("Data you want to put");
out.close();

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

String urlParameters = "param1=a&param2=b&param3=c";
URL url = new URL("http://example.com/index.php");
URLConnection conn = url.openConnection();

conn.setDoOutput(true);

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

writer.write(urlParameters);
writer.flush();

String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

while ((line = reader.readLine()) != null) {
  System.out.println(line);
}
writer.close();
reader.close();

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

URL u = new URL("http://www.example.com/"); 
 HttpURLConnection huc = (HttpURLConnection)u.openConnection(); 
 huc.setRequestMethod("GET"); 
 huc.connect() ; 
 OutputStream os = huc.getOutputStream(); 
 int code = huc.getResponseCode();

相关文章

URLConnection类方法