资源简介
用于HTTP请求的工具类,支持GET,POST,HTTP,HTTPS
代码简单易懂
代码片段和文件信息
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpUtil
{
public static final String CHARSET_UTF8 = “UTF-8“;
public static final String CHARSET_GBK = “GBK“;
public static int HTTP_TIMEOUT = 15;
public static boolean GZIP = false;
public static String ENCODING = “UTF-8“;
private static TrustManager myX509TrustManager = new X509TrustManager()
{
public void checkClientTrusted(X509Certificate[] arg0 String arg1)
throws CertificateException
{
}
public void checkServerTrusted(X509Certificate[] arg0 String arg1)
throws CertificateException
{
}
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
};
public static Map createCommonHeader()
{
Map header = new HashMap();
header.put(“User-Agent“ “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML like Gecko) Chrome/44.0.2403.155 Safari/537.36“);
return header;
}
public static String doPost(String url Map header String data)
{
if (url.contains(“https://“))
return doPostSSL(url header data);
try
{
URL uri = new URL(url);
HttpURLConnection conn = (HttpURLConnection)uri.openConnection();
conn.setRequestMethod(“POST“);
conn.setDoInput(true);
conn.setReadTimeout(HTTP_TIMEOUT * 1000);
conn.setConnectTimeout(HTTP_TIMEOUT * 1000);
if (header != null) {
for (String key : header.keySet()) {
conn.setRequestProperty(key (String)header.get(key));
}
}
if (data != null) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
os.flush();
os.close();
}
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is ENCODING));
StringBuffer resultsb = new StringBuffer();
String temp = null;
while ((temp = reader.readLine()) != null) {
resultsb.append(temp);
}
return resultsb.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String doGet(String url Map header)
{
if (url.contains(“https://“))
return doGetSSL(url header);
try
{
URL uri = new URL(
相关资源
- okhttp-2.4.0和okio-1.4.0库
- org.apache.http.legacy.jar -sdk 23
- Android电子拍卖系统
- Android Http实现文件的上传和
- httpmime-4.1.2.jar
- Java 调用Http Rest接口 说明2
- http客户端java程序
- HttpClient jar包
- httpcore-4.1.2.jar
- Java 解析http请求处理结果
- httpclient-4.3.4.jar
- httpclient-4.3.6.jar
- org.apache.http.entity.mime
- Android使用OkHttp访问自签名证书的接口
- java访问https网址文件(含证书)
- HttpServletRequest源码 HttpServletResponse源码
- okHttp-3.11.0
- ApacheJMeter_http.jar60720
- OKHttp3 所有Jarb包
- http接口需要的jar包
- jstl.jar和standard.jar 报错Can not find \“
- java访问https网址文件
- httpclient get/post请求工具类map参数封装
- HttpClient4.5 实现https忽略SSL证书验证
- JAVA发送HTTP请求操作类
- webmagic修复HTTPS下无法抓取只支持TLS
- HttpTools2.4
- Java实现HTTP文件
- httpmime-4.0.jar|apache-mime4j-0.6.jar
- 使用JAVA实现HTTP多线程
评论
共有 条评论