Here you can find the source of post(URL url, String contentType, byte[] data)
Parameter | Description |
---|---|
url | the URL to connect to |
contentType | the data's content type |
data | the data to send to the server |
Parameter | Description |
---|---|
IOException | an exception |
ProtocolException | an exception |
public static InputStream post(URL url, String contentType, byte[] data) throws IOException, ProtocolException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; public class Main { /**/* www.java 2s. c o m*/ * Sends HTTP POST request * @param url the URL to connect to * @param contentType the data's content type * @param data the data to send to the server * @return Streamed response from server * @throws IOException * @throws ProtocolException */ public static InputStream post(URL url, String contentType, byte[] data) throws IOException, ProtocolException { HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("content-Type", contentType); OutputStream out = con.getOutputStream(); try { out.write(data, 0, data.length); out.flush(); } finally { out.close(); } return con.getInputStream(); } }