博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTTP请求工具类
阅读量:4181 次
发布时间:2019-05-26

本文共 2859 字,大约阅读时间需要 9 分钟。

public class MyX509TrustManager {

private static class TrustAnyHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}

/** * post方式请求服务器(https协议) * * @param url *            请求地址 *            编码 * @return * @throws java.security.NoSuchAlgorithmException * @throws java.security.KeyManagementException * @throws java.io.IOException */public static String getResult1(String url) throws NoSuchAlgorithmException,		KeyManagementException, IOException {	URL console = new URL(url);	HttpURLConnection conn = (HttpURLConnection) console.openConnection();	conn.connect();	InputStream is = conn.getInputStream();	if (is != null) {		ByteArrayOutputStream outStream = new ByteArrayOutputStream();		byte[] buffer = new byte[1024];		int len = 0;		while ((len = is.read(buffer)) != -1) {			outStream.write(buffer, 0, len);		}		is.close();		return new String(outStream.toByteArray(),"utf-8");	}	return null;}/** * post方式请求服务器(https协议) *  * @param url *            请求地址 *            编码 * @return * @throws java.security.NoSuchAlgorithmException * @throws java.security.KeyManagementException * @throws java.io.IOException */public static String getResult(String url) throws NoSuchAlgorithmException,		KeyManagementException, IOException {		URL console = new URL(url);	HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();	conn.setHostnameVerifier(new TrustAnyHostnameVerifier());	conn.connect();	InputStream is = conn.getInputStream();	if (is != null) {		ByteArrayOutputStream outStream = new ByteArrayOutputStream();		byte[] buffer = new byte[1024];		int len = 0;		while ((len = is.read(buffer)) != -1) {			outStream.write(buffer, 0, len);		}		is.close();				return new String(outStream.toByteArray(),"utf-8");	}	return null;}/** * 向post发送post请求 * @param context 发送的内容 url:地址 * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws IOException */public static String postHttps(byte[] context,String url) throws NoSuchAlgorithmException,		KeyManagementException, IOException {	URL console = new URL(url);	HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();	conn.setHostnameVerifier(new TrustAnyHostnameVerifier());	conn.setRequestMethod("POST");	conn.setRequestProperty("Content-Length",			String.valueOf(context.length));	conn.setUseCaches(false);	conn.setDoOutput(true);	conn.getOutputStream().write(context);	conn.getOutputStream().flush();	conn.getOutputStream().close();	conn.connect();	InputStream is = conn.getInputStream();	if (is != null) {		ByteArrayOutputStream outStream = new ByteArrayOutputStream();		byte[] buffer = new byte[1024];		int len = 0;		while ((len = is.read(buffer)) != -1) {			outStream.write(buffer, 0, len);		}		is.close();		return new String(outStream.toByteArray(), "utf-8");	}	return null;}

}

转载地址:http://gkgai.baihongyu.com/

你可能感兴趣的文章
CSDN-markdown编辑器
查看>>
拷贝整个目录到另一台服务器并排除log目录
查看>>
拜托,面试别再问我跳表了!
查看>>
android ArrayList<String> 转 String[]
查看>>
RecyclerView baseadapter
查看>>
Android中应用程序如何获得系统签名权限
查看>>
Recycler表格(excelPanel)
查看>>
android一行代码实现沉浸式布局效果
查看>>
json, recyclerView问题
查看>>
cmake处理多源文件目录的方法
查看>>
Service Intent must be explicit
查看>>
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>