1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.io.PrintWriter; 5 import java.net.URL; 6 import java.net.URLConnection; 7 8 import javax.net.ssl.HostnameVerifier; 9 import javax.net.ssl.HttpsURLConnection; 10 import javax.net.ssl.SSLSession; 11 12 import org.apache.log4j.Logger; 13 14 import com.xwtech.parser.PostRequestHtmlParser; 15 16 /** 17 * Post请求类——得到HTML响应 18 */ 19 public class PostRequest extends Thread { 20 private String url = "https://b2b.10086.cn/b2b/main/listVendorNoticeResult.html?noticeBean.noticeType="; 21 private String params; 22 private Logger logger; 23 24 public PostRequest(int noticeType, int perPageSize) { 25 logger = Logger.getLogger(PostRequest.class); 26 this.url = this.url + noticeType; //拼接URL请求,不包含参数 27 params = "page.currentPage=" + currentPage + "&page.perPageSize=" + perPageSize 28 + "¬iceBean.sourceCH=¬iceBean.source=" 29 + "¬iceBean.title=¬iceBean.startDate=¬iceBean.endDate="; 30 } 31 32 public void run() { 33 PrintWriter out = null; 34 BufferedReader in = null; 35 URLConnection conn = null; 36 String result = ""; 37 try { 38 conn = new URL(url).openConnection(); 39 conn.setUseCaches(false); 40 conn.setRequestProperty("accept", "*/*"); 41 conn.setRequestProperty("connection", "Keep-Alive"); 42 conn.setRequestProperty("user-agent", 43 "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"); 44 conn.setRequestProperty("X-Requested-With", "XMLHttpRequest"); 45 // 发送POST请求必须设置如下两行 46 conn.setDoOutput(true); 47 conn.setDoInput(true); 48 49 // 获取URLConnection对象对应的输出流 50 out = new PrintWriter(conn.getOutputStream()); 51 // 发送请求参数 52 out.print(params); 53 // flush输出流的缓冲 54 out.flush(); 55 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 56 String line; 57 while ((line = in.readLine()) != null) { 58 result += line; 59 } 60 } catch (Exception e) { 61 logger.error(currentThread().getName() + "线程Post请求出现问题! " + e.getMessage() + " "); 62 } finally {// 使用finally块来关闭输出流、输入流 63 try { 64 if (out != null) { 65 out.close(); 66 } 67 if (in != null) { 68 in.close(); 69 } 70 } catch (IOException ex) { 71 logger.error(currentThread().getName() + "线程Post请求数据流出现问题! " + ex.getMessage() + " "); 72 } 73 } 74 //获取到相应结果result ,可以直接在这里进行下一步处理,或者放入到全局字段中,通过其他方式获取...... 75 } 76 }
上面对应的参数以及url等信息可以使用chrome,firefox等浏览器network组件查看。
相关文章
java-get请求: http://www.cnblogs.com/husky/p/6377577.html
java https请求忽略安全证书信任: http://www.cnblogs.com/husky/p/6378290.html