HTTP-java模拟GET/POST请求(忽略/不忽略证书)
1、请求的形式
- GET请求:把请求的数据放在URL上,用?分割URL和传输的数据,多个参数的话用&符号连接。
- POST请求:把请求的数据放在HTTP请求包的包体中(requrest body)。
POST请求包含两个部分:请求头(header)和请求体(body)
1.Java发送post请求:
代码示例如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import org.apache.log4j.Logger;
import com.xwtech.parser.PostRequestHtmlParser;
/**
* Post请求类——得到HTML响应
*/
public class PostRequest extends Thread {
private String url = "https://b2b.10086.cn/b2b/main/listVendorNoticeResult.html?noticeBean.noticeType=";
private String params;
private Logger logger;
public PostRequest(int noticeType, int perPageSize) {
logger = Logger.getLogger(PostRequest.class);
this.url = this.url + noticeType; //拼接URL请求,不包含参数
params = "page.currentPage=" + currentPage + "&page.perPageSize=" + perPageSize
+ "¬iceBean.sourceCH=¬iceBean.source="
+ "¬iceBean.title=¬iceBean.startDate=¬iceBean.endDate=";
}
public void run() {
PrintWriter out = null;
BufferedReader in = null;
URLConnection conn = null;
String result = "";
try {
conn = new URL(url).openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36");
conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
logger.error(currentThread().getName() + "线程Post请求出现问题!\n" + e.getMessage() + "\n");
} finally {// 使用finally块来关闭输出流、输入流
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
logger.error(currentThread().getName() + "线程Post请求数据流出现问题!\n" + ex.getMessage() + "\n");
}
}
//获取到相应结果result ,可以直接在这里进行下一步处理,或者放入到全局字段中,通过其他方式获取......
}
}
2.Java发送get请求:
代码示例如下:
1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.net.HttpURLConnection;
5 import java.net.URL;
6 8 import javax.net.ssl.HttpsURLConnection;10
11 import org.apache.log4j.Logger;13 16 /*
17 * GET请求类,非线程类
18 */
19 public class GetRequest {
20 private String url = "https://b2b.10086.cn/b2b/main/viewNoticeContent.html?noticeBean.id=";
21 private Logger logger;
22 public GetRequest() {
23 logger = Logger.getLogger(GetRequest.class);
24 }
25
26 public ExtendCandidate getData(String id) {
27 this.url = url + id;
28 BufferedReader in = null;
29 HttpURLConnection conn = null;
30 String result = "";
31 try {
32 conn = (HttpURLConnection)new URL(url).openConnection();
33 // 发送GET请求必须设置如下两行
34 conn.setDoInput(true);
35 conn.setRequestMethod("GET");
36 // flush输出流的缓冲
37 in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
38 String line;
39 while ((line = in.readLine()) != null) {
40 result += line;
41 }
42 } catch (Exception e) {
43 logger.error("发送 GET 请求出现异常!\t请求ID:"+id+"\n"+e.getMessage()+"\n");
44 } finally {// 使用finally块来关闭输出流、输入流
45 try {
46 if (in != null) {
47 in.close();
48 }
49 } catch (IOException ex) {
50 logger.error("关闭数据流出错了!\n"+ex.getMessage()+"\n");
51 }
52 }
53 //获取到结果result,可以直接添加处理,或者存储到全局变量......
54 }
55 }
3.Java发送get/post请求(忽略证书信任问题):
java程序在访问https资源时,出现如下报错:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alert.createSSLException(Alert.java:131)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:353)
......
at java.lang.Thread.run(Thread.java:748)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:439)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:306)
at sun.security.validator.Validator.validate(Validator.java:271)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:312)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:221)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:128)
at sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:636)
... 107 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:434)
... 113 more
下面解决示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import org.apache.log4j.Logger;
import org.htmlparser.util.ParserException;
import com.xwtech.parser.GetRequestHtmlParser;
import com.xwtech.pojo.ExtendCandidate;
/*
* GET请求类
*/
public class GetRequest {
private String url = "https://b2b.10086.cn/b2b/main/viewNoticeContent.html?noticeBean.id=";
private Logger logger;
public GetRequest() {
logger = Logger.getLogger(GetRequest.class);
}
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
//为更好的演示,去掉了不相关的代码
public void getData(String id) {
this.url = url + id;
BufferedReader in = null;
HttpURLConnection conn = null;
String result = "";
try { //该部分必须在获取connection前调用
trustAllHttpsCertificates();
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
logger.info("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
conn = (HttpURLConnection)new URL(url).openConnection();
// 发送GET请求必须设置如下两行
conn.setDoInput(true);
conn.setRequestMethod("GET");
// flush输出流的缓冲
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
logger.error("发送 GET 请求出现异常!\t请求ID:"+id+"\n"+e.getMessage()+"\n");
} finally {// 使用finally块来关闭输出流、输入流
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
logger.error("关闭数据流出错了!\n"+ex.getMessage()+"\n");
}
}
// 获得相应结果result,可以直接处理......
}
static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
}
就是在进行连接前需要显示调用以下进行忽略证书信任
trustAllHttpsCertificates(); // 这是一个方法,具体见上面
HttpsURLConnection.setDefaultHostnameVerifier(hv); //这里HttpsURLConnection是类名,hv参数需要自己创建,具体可以参考上面。
Post请求需要忽略证书信任与这个一样,在获取连接前,加上trustAllHttpsCertificates方法。