本文整理汇总了Java中javax.net.ssl.HttpsURLConnection.setDoOutput方法的典型用法代码示例。如果您正苦于以下问题:Java HttpsURLConnection.setDoOutput方法的具体用法?Java HttpsURLConnection.setDoOutput怎么用?Java HttpsURLConnection.setDoOutput使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.net.ssl.HttpsURLConnection
的用法示例。
在下文中一共展示了HttpsURLConnection.setDoOutput方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendData
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* Sends the data to the bStats server.
*
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(JsonObject data) throws Exception {
Validate.notNull(data, "Data cannot be null");
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
// Send data
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(compressedData);
outputStream.flush();
outputStream.close();
connection.getInputStream().close(); // We don't care about the response - Just send our data :)
}
示例2: importToCloudCard
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
private static String importToCloudCard(CardHolder cardHolder, Properties properties) {
try {
URL url = new URL(properties.getProperty(BASE_URL) + "/api/people");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
setConnectionHeaders(connection, properties);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(cardHolder.toJSON().getBytes());
outputStream.flush();
if (connection.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
return "Failed : HTTP error code : " + connection.getResponseCode();
}
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return "Success";
}
示例3: get
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 鍙戦�丟et璇锋眰
* @param url
* @return
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws IOException
* @throws KeyManagementException
*/
public static String get(String url,Boolean https) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 杩炴帴瓒呮椂
http.setConnectTimeout(25000);
// 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(25000);
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴杩炴帴
http.disconnect();
}
return bufferRes.toString();
}
示例4: sendData
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* Sends the data to the bStats server.
*
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(JSONObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null!");
}
if (Bukkit.isPrimaryThread()) {
throw new IllegalAccessException("This method must not be called from the main thread!");
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
// Send data
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(compressedData);
outputStream.flush();
outputStream.close();
connection.getInputStream().close(); // We don't care about the response - Just send our data :)
}
示例5: sendData
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* Sends the data to the bStats server.
*
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(JsonObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null");
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
// Send data
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(compressedData);
outputStream.flush();
outputStream.close();
connection.getInputStream().close(); // We don't care about the response - Just send our data :)
}
示例6: HttpsPost
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
*
* @param requestURL
* @param entity
* @throws MalformedURLException
* @throws IOException
*/
public HttpsPost(String requestURL, MultipartEntity entity) throws MalformedURLException, IOException {
String boundary = "e2a540ab4e6c5ed79c01157c255a2b5007e157d7";
URL url = new URL(requestURL);
connection = (HttpsURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
process(entity, connection);
}
开发者ID:B-V-R,项目名称:VirusTotal-public-and-private-API-2.0-implementation-in-pure-Java,代码行数:19,代码来源:HttpsPost.java
示例7: doExecute
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
final String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
if (StringUtils.isBlank(gRecaptchaResponse)) {
LOGGER.warn("Recaptcha response is missing from the request");
return getError(requestContext);
}
try {
final URL obj = new URL(recaptchaProperties.getVerifyUrl());
final HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", WebUtils.getHttpServletRequestUserAgent());
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
final String postParams = "secret=" + recaptchaProperties.getSecret() + "&response=" + gRecaptchaResponse;
LOGGER.debug("Sending 'POST' request to URL: [{}]", obj);
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.writeBytes(postParams);
wr.flush();
}
final int responseCode = con.getResponseCode();
LOGGER.debug("Response Code: [{}]", responseCode);
if (responseCode == HttpStatus.OK.value()) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
final String response = in.lines().collect(Collectors.joining());
LOGGER.debug("Google captcha response received: [{}]", response);
final JsonNode node = READER.readTree(response);
if (node.has("success") && node.get("success").booleanValue()) {
return null;
}
}
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return getError(requestContext);
}
示例8: deleteKfAccount
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 删除客服帐号
*
* @param keFu
* @return
*/
public static boolean deleteKfAccount(KeFu keFu) {
boolean isOk = false;
String token = WeiXinUtils.getToken();
if (token != null) {
String urlString = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=" + token;
try {
URL url = new URL(urlString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
String kfAccountString = JSONObject.toJSONString(keFu);
httpsURLConnection.setRequestProperty("Content-length", String.valueOf(kfAccountString.length()));
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setDoInput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream());
dataOutputStream.write(kfAccountString.getBytes());
dataOutputStream.flush();
dataOutputStream.close();
DataInputStream dataInputStream = new DataInputStream(httpsURLConnection.getInputStream());
StringBuffer stringBuffer = new StringBuffer();
int inputByte = dataInputStream.read();
while (inputByte != -1) {
stringBuffer.append((char) inputByte);
inputByte = dataInputStream.read();
}
String kfString = stringBuffer.toString();
JSONObject jsonObject = JSON.parseObject(kfString);
if (jsonObject.containsKey("errcode")) {
int errcode = jsonObject.getIntValue("errcode");
if (errcode == 0) {
isOk = true;
} else {
//TODO 添加客服账号失败
isOk = false;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return isOk;
}
示例9: insertKfAccount
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 添加客服帐号
*
* @param keFu
* @return
*/
public static boolean insertKfAccount(KeFu keFu) {
boolean isOk = false;
String token = WeiXinUtils.getToken();
if (token != null) {
String urlString = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + token;
try {
URL url = new URL(urlString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
String kfAccountString = JSONObject.toJSONString(keFu);
httpsURLConnection.setRequestProperty("Content-length", String.valueOf(kfAccountString.length()));
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setDoInput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream());
dataOutputStream.write(kfAccountString.getBytes());
dataOutputStream.flush();
dataOutputStream.close();
DataInputStream dataInputStream = new DataInputStream(httpsURLConnection.getInputStream());
StringBuffer stringBuffer = new StringBuffer();
int inputByte = dataInputStream.read();
while (inputByte != -1) {
stringBuffer.append((char) inputByte);
inputByte = dataInputStream.read();
}
String kfString = stringBuffer.toString();
JSONObject jsonObject = JSON.parseObject(kfString);
if (jsonObject.containsKey("errcode")) {
int errcode = jsonObject.getIntValue("errcode");
if (errcode == 0) {
isOk = true;
} else {
//TODO 添加客服账号失败
isOk = false;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return isOk;
}
示例10: sendFileMsg
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 发送多媒体文件
*
* @return
*/
public static String sendFileMsg(String touser, String toparty, String totag, int agentid, String media_id,
boolean safe) throws IOException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser", touser);// 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
jsonObject.put("toparty", toparty);// 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
jsonObject.put("totag", totag);// 标签ID列表,多个接收者用‘|’分隔。当touser为@all时忽略本参数
jsonObject.put("agentid", agentid + "");// 企业应用的id,整型。可在应用的设置页面查看
jsonObject.put("msgtype", "file");// 消息类型,此时固定为:text
JSONObject text = new JSONObject();
text.put("media_id", media_id);
jsonObject.put("file", text);// 消息内容
if (safe) {
jsonObject.put("safe", "1");// 表示是否是保密消息,0表示否,1表示是,默认0
} else {
jsonObject.put("safe", "0");// 表示是否是保密消息,0表示否,1表示是,默认0
}
System.out.println(jsonObject);
String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
+ WeiXinCompanyUtils.getToken();
String parameters = jsonObject.toString();
URL url = new URL(urlStr);
System.out.println("url:" + urlStr);
System.out.println("parameters:" + parameters);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream output = conn.getOutputStream();
output.write(parameters.getBytes("utf-8"));
output.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String s = null;
StringBuilder sb = new StringBuilder();
while ((s = reader.readLine()) != null) {
sb.append(s);
}
reader.close();
System.out.println(sb.toString());
return sb.toString();
}
示例11: upload
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static String upload(File file) throws IOException {
String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" + WeiXinCompanyUtils.getToken()
+ "&type=file";
// 定义数据分隔符
String boundary = "------------7da2e536604c8";
URL uploadUrl = new URL(urlStr);
HttpsURLConnection uploadConn = (HttpsURLConnection) uploadUrl.openConnection();
uploadConn.setDoOutput(true);
uploadConn.setDoInput(true);
uploadConn.setRequestMethod("POST");
// 设置请求头Content-Type
uploadConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// 获取媒体文件上传的输出流(往微信服务器写数据)
OutputStream outputStream = uploadConn.getOutputStream();
// 从请求头中获取内容类型
String contentType = "text";
// 根据内容类型判断文件扩展名
String ext = file.getName().substring(file.getName().lastIndexOf("."));
String name = file.getName();
// 请求体开始
outputStream.write(("--" + boundary + "\r\n").getBytes());
String aaa = String
.format("Content-Disposition: form-data; name=\"media\"; filename=\"" + name + "." + "%s\"\r\n", ext);
outputStream.write(aaa.getBytes());
String bbb = String.format("Content-Type: %s\r\n\r\n", contentType);
outputStream.write(bbb.getBytes());
// 获取媒体文件的输入流(读取文件)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[8096];
int size = 0;
while ((size = bis.read(buf)) != -1) {
// 将媒体文件写到输出流(往微信服务器写数据)
outputStream.write(buf, 0, size);
}
// 请求体结束
outputStream.write(("\r\n--" + boundary + "--\r\n").getBytes());
outputStream.close();
bis.close();
// 获取媒体文件上传的输入流(从微信服务器读数据)
InputStream inputStream = uploadConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
uploadConn.disconnect();
System.out.println(buffer.toString());
return buffer.toString();
}
示例12: upload
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static String upload(String fileName, File file) throws IOException {
String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" + WeiXinCompanyUtils.getToken()
+ "&type=file";
// 定义数据分隔符
String boundary = "------------7da2e536604c8";
URL uploadUrl = new URL(urlStr);
HttpsURLConnection uploadConn = (HttpsURLConnection) uploadUrl.openConnection();
uploadConn.setDoOutput(true);
uploadConn.setDoInput(true);
uploadConn.setRequestMethod("POST");
// 设置请求头Content-Type
uploadConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// 获取媒体文件上传的输出流(往微信服务器写数据)
OutputStream outputStream = uploadConn.getOutputStream();
// 从请求头中获取内容类型
String contentType = "text";
// 根据内容类型判断文件扩展名
@SuppressWarnings("unused")
String[] f = fileName.split("\\.");
// 请求体开始
outputStream.write(("--" + boundary + "\r\n").getBytes());
// String aaa = String.format("Content-Disposition: form-data;
// name=\"media\"; filename=\""+f[0]+"."+"%s\"\r\n", f[1]);
String aaa = "Content-Disposition: form-data; name=\"media\"; filename=\"" + fileName + "\"\r\n";
outputStream.write(aaa.getBytes());
String bbb = String.format("Content-Type: %s\r\n\r\n", contentType);
outputStream.write(bbb.getBytes());
// 获取媒体文件的输入流(读取文件)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[8096];
int size = 0;
while ((size = bis.read(buf)) != -1) {
// 将媒体文件写到输出流(往微信服务器写数据)
outputStream.write(buf, 0, size);
}
// 请求体结束
outputStream.write(("\r\n--" + boundary + "--\r\n").getBytes());
outputStream.close();
bis.close();
// 获取媒体文件上传的输入流(从微信服务器读数据)
InputStream inputStream = uploadConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
uploadConn.disconnect();
System.out.println(buffer.toString());
return buffer.toString();
}
示例13: uploadAttachment
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
private byte[] uploadAttachment(String method, String url, InputStream data,
long dataSize, byte[] key, ProgressListener listener)
throws IOException
{
URL uploadUrl = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) uploadUrl.openConnection();
connection.setDoOutput(true);
if (dataSize > 0) {
connection.setFixedLengthStreamingMode((int) AttachmentCipherOutputStream.getCiphertextLength(dataSize));
} else {
connection.setChunkedStreamingMode(0);
}
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("Connection", "close");
connection.connect();
try {
OutputStream stream = connection.getOutputStream();
AttachmentCipherOutputStream out = new AttachmentCipherOutputStream(key, stream);
byte[] buffer = new byte[4096];
int read, written = 0;
while ((read = data.read(buffer)) != -1) {
out.write(buffer, 0, read);
written += read;
if (listener != null) {
listener.onAttachmentProgress(dataSize, written);
}
}
data.close();
out.flush();
out.close();
if (connection.getResponseCode() != 200) {
throw new IOException("Bad response: " + connection.getResponseCode() + " " + connection.getResponseMessage());
}
return out.getAttachmentDigest();
} finally {
connection.disconnect();
}
}
示例14: httpsRequest
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new HttpsX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json;;charset=UTF-8");
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}