当前位置: 首页>>代码示例>>Java>>正文


Java HttpsURLConnection.setDoInput方法代码示例

本文整理汇总了Java中javax.net.ssl.HttpsURLConnection.setDoInput方法的典型用法代码示例。如果您正苦于以下问题:Java HttpsURLConnection.setDoInput方法的具体用法?Java HttpsURLConnection.setDoInput怎么用?Java HttpsURLConnection.setDoInput使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.net.ssl.HttpsURLConnection的用法示例。


在下文中一共展示了HttpsURLConnection.setDoInput方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getUserInfo

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static String getUserInfo(String openId) {
	String token = WeiXinUtils.getToken();
	if (token != null) {
		String urlString = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid="
				+ openId;
		try {
			URL url = new URL(urlString);
			HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
			httpsURLConnection.setDoInput(true);
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(httpsURLConnection.getInputStream()));
			String line = null;
			StringBuilder stringBuilder = new StringBuilder();
			while ((line = bufferedReader.readLine()) != null) {
				stringBuilder.append(line);
			}
			String kfString = stringBuilder.toString();
			return kfString;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	return null;
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:25,代码来源:WeiXinUserUtils.java

示例2: getAllKfAccount

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 * 获取所有客服帐号
 *
 * @return
 */
public static String getAllKfAccount() {
    String token = WeiXinUtils.getToken();
    if (token != null) {
        String urlString = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + token;
        try {
            URL url = new URL(urlString);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setDoInput(true);
            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();
            return kfString;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return null;
}
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:29,代码来源:WeiXinKFUtils.java

示例3: initTicket

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static void initTicket() {
	if (jsapi_ticket == null || expires_in == null || ticketTime == null
			|| System.currentTimeMillis() - ticketTime >= expires_in) {
		String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token="
				+ WeiXinCompanyUtils.getToken();
		try {
			URL url = new URL(urlStr);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

			conn.setDoInput(true);
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuilder sb = new StringBuilder();
			String s = null;
			while ((s = reader.readLine()) != null) {
				sb.append(s);
			}
			reader.close();

			JSONObject jsonObject = JSON.parseObject(sb.toString());
			String errcode = jsonObject.get("errcode").toString();
			if (errcode.equals("0")) {
				jsapi_ticket = jsonObject.get("ticket").toString();
				expires_in = jsonObject.getLong("expires_in");
				ticketTime = System.currentTimeMillis();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
开发者ID:guokezheng,项目名称:automat,代码行数:31,代码来源:WeiXinCompanyTicket.java

示例4: reSend

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 * 重发
 * 
 * @param urlStr
 * @param parameters
 * @param count
 */
private static void reSend(String urlStr, String parameters, Map<String, Integer> count) throws IOException {
	if (count.get("times") == null) {
		count.put("times", 0);
	}
	int times = count.get("times");
	if (times < 5) {
		URL url = new URL(urlStr);
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);
		conn.setReadTimeout(3000);
		conn.setConnectTimeout(3000);

		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();
		JSONObject jsonObject = JSONObject.parseObject(sb.toString());
		String errcode = jsonObject.get("errcode").toString();
		if (!errcode.equals("0")) {
			count.put("times", count.get("times") + 1);
			reSend(urlStr, parameters, count);
		}
	}
}
 
开发者ID:guokezheng,项目名称:automat,代码行数:42,代码来源:WeiXinCompanySendMsg.java

示例5: 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();
}
 
开发者ID:bubicn,项目名称:bubichain-sdk-java,代码行数:46,代码来源:HttpKit.java

示例6: post

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 *  鍙戦�丳ost璇锋眰
 * @param url
 * @param params
 * @return
 * @throws IOException 
 * @throws NoSuchProviderException 
 * @throws NoSuchAlgorithmException 
 * @throws KeyManagementException 
 */
public static String post(String url, String params,Boolean https) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, 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(50000);
    // 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
    http.setReadTimeout(50000);
    http.setRequestMethod("POST");
    http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    http.setSSLSocketFactory(ssf);
    http.setHostnameVerifier(new Verifier());
    http.setDoOutput(true);
    http.setDoInput(true);
    http.connect();

    OutputStream out = http.getOutputStream();
    out.write(params.getBytes("UTF-8"));
    out.flush();
    out.close();

    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();
}
 
开发者ID:bubicn,项目名称:bubichain-sdk-java,代码行数:52,代码来源:HttpKit.java

示例7: 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;
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:48,代码来源:WeiXinKFUtils.java

示例8: progressNotic

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
     * 报修处理进展通知
     */
    public static boolean progressNotic() {
        boolean isOk = false;
        String token = WeiXinUtils.getToken();
        if (token != null) {
            String urlString = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
            try {
//                op-51t-YG9RE_Pcfr9WD8e9MRYN0
                WXMessasgeTemplate wxMessasgeTemplate = new WXMessasgeTemplate();
//                wxMessasgeTemplate.setTouser("op-51t5m2L_VLsDvXXDdSm-BOToY");
                wxMessasgeTemplate.setTouser("op-51t-YG9RE_Pcfr9WD8e9MRYN0");
                wxMessasgeTemplate.setTemplate_id("7GabJuZ-w-0ZTKMdajfmXB3WBVCp55NAmnrUS_IqU3Y");
                wxMessasgeTemplate.setUrl("http://weixin.qq.com/download");
                JSONObject jsonObject = new JSONObject();
                JSONObject firstJsonObject = new JSONObject();
                firstJsonObject.put("value", "尊敬的耿adfasf:您的报修有新的进展。");
                jsonObject.put("first", firstJsonObject);

                JSONObject key1 = new JSONObject();
                key1.put("value", "郑汴路dfasfdf");
                jsonObject.put("keyword1", key1);

                JSONObject key2 = new JSONObject();
                key2.put("value", "郑汴路adfasf1");
                jsonObject.put("keyword2", key2);

                JSONObject key3 = new JSONObject();
                key3.put("value", "郑汴路2adfasf");
                jsonObject.put("keyword3", key3);

                JSONObject key4 = new JSONObject();
                key4.put("value", "郑汴路asdfasf3");
                jsonObject.put("keyword4", key4);

                JSONObject key5 = new JSONObject();
                key5.put("value", "郑汴路afdasf4");
                jsonObject.put("keyword5", key5);

                JSONObject remark = new JSONObject();
                remark.put("value", "3ks!");
                jsonObject.put("remark", remark);
                wxMessasgeTemplate.setData(jsonObject);
                URL url = new URL(urlString);
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
                String kfAccountString = JSONObject.toJSONString(wxMessasgeTemplate);
                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();
                System.out.println(kfString);
                JSONObject jsonObject1 = JSON.parseObject(kfString);
                if (jsonObject1.containsKey("errcode")) {
                    int errcode = jsonObject1.getIntValue("errcode");
                    if (errcode == 0) {
                        isOk = true;
                    } else {
                        //TODO 添加客服账号失败
                        isOk = false;
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return isOk;
    }
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:81,代码来源:WeiXinWYUtils.java

示例9: 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();
}
 
开发者ID:guokezheng,项目名称:automat,代码行数:56,代码来源:WeiXinCompanySendMsg.java

示例10: updateKfAccount

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 * 修改客服帐号
 *
 * @param keFu
 * @return
 */
public static boolean updateKfAccount(KeFu keFu) {
    boolean isOk = false;
    String token = WeiXinUtils.getToken();
    if (token != null) {
        String urlString = "https://api.weixin.qq.com/customservice/kfaccount/update?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;
}
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:48,代码来源:WeiXinKFUtils.java

示例11: 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;
}
 
开发者ID:youngMen1,项目名称:JAVA-,代码行数:48,代码来源:WeiXinKFUtils.java

示例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();
}
 
开发者ID:guokezheng,项目名称:automat,代码行数:61,代码来源:WeiXinCompanyUpload.java

示例13: httpsRequest

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
     * 发起https请求并获取结果
     * 
     * @param requestUrl
     *            请求地址
     * @param requestMethod
     *            请求方式(GET、POST)
     * @param outputStr
     *            提交的数据
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
     */
    public static JSONObject httpsRequest(String requestUrl,String requestMethod,String outputStr){
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象,并使用我们指定的信任管理器初始化
            TrustManager[] tm = { new SaicX509TrustManager() };
//          SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            SSLContext sslContext = SSLContext.getInstance("TLS", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();

            URL url = new URL(requestUrl);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);

            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod(requestMethod);

            if ("GET".equalsIgnoreCase(requestMethod))
                httpUrlConn.connect();

            // 当有数据需要提交时
            if (null != outputStr) {
                OutputStream outputStream = httpUrlConn.getOutputStream();
                // 注意编码格式,防止中文乱码
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }

            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (ConnectException ce) {
            logger.error("connection timed out cause by " + ce.getMessage());
        } catch (Exception e) {
            logger.error("https request error : " + e.getMessage());
        }
        return jsonObject;
    }
 
开发者ID:tojaoomy,项目名称:private-WeChat,代码行数:68,代码来源:CommonUtil.java


注:本文中的javax.net.ssl.HttpsURLConnection.setDoInput方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。