使用JAVA Get POST 抓网页的练习代码

来源:互联网 发布:流量互刷软件 编辑:程序博客网 时间:2024/06/06 01:43

private static void sendGet(String strUrl) {
try {
URL url = new URL(strUrl);
URLConnection urlc = url.openConnection();
urlc.setRequestProperty("accept", "*/*");
urlc.setRequestProperty("connection", "Keep-Alive");
urlc.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ");
urlc.connect();
Map<String, List<String>> map = urlc.getHeaderFields();
System.out.println("开始写出报文头部");
for (String strKey : map.keySet())
for (String strValue : map.get(strKey))
System.out.println(strKey + " : " + strValue);
System.out.println("写出报文头部完成");
InputStreamReader isr = new InputStreamReader(urlc.getInputStream());
BufferedReader br = new BufferedReader(isr);
FileWriter fw = new FileWriter("C:\\user_data\\21MyTempFile\\temp_get.txt");
String strTemp = "";
while ((strTemp = br.readLine()) != null){
fw.write(strTemp+"\n");
}
System.out.println("get写出报文体完成");
fw.close();
br.close();
isr.close();
} catch (Exception e) {
System.out.println("抛出异常,异常信息是: " + e.getMessage());
}
}


private static void sendPost(String strUrl,String strPostBody) {
try {
URL url = new URL(strUrl);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
urlc.setRequestMethod("POST");
urlc.setRequestProperty("accept", "application/json,text/javascript,*/*");
urlc.setRequestProperty("connection", "keep-alive");
urlc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
urlc.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
urlc.setRequestProperty("Accept-Encoding", "gzip, deflate");
// urlc.setRequestProperty("Cookie", "main[UTMPUSERID]=liji136136; main[UTMPKEY]=44822869; main[UTMPNUM]=17446; main[PASSWORD]=h%2521%251C_%2504%257EkfH%2501%2560%2507%2512yWC%2501%257B%257D%2500%255Eu%2505X; Hm_lvt_9c7f4d9b7c00cb5aba2c637c64a41567=1493689265,1493736449,1493786700,1493792295; main[XWJOKE]=hoho; Hm_lpvt_9c7f4d9b7c00cb5aba2c637c64a41567=1493810487");
urlc.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0");

urlc.setDoOutput(true);  //发送post请求必须设置本行
// urlc.setDoInput(true);
byte[] arb=strPostBody.getBytes("UTF-8");
OutputStream os=urlc.getOutputStream();
os.write(arb);
os.flush();
urlc.connect();
Map<String, List<String>> map = urlc.getHeaderFields();
System.out.println("开始写出报文头部");
for (String strKey : map.keySet())
for (String strValue : map.get(strKey))
System.out.println(strKey + " : " + strValue);
System.out.println("写出报文头部完成");
// System.out.println("response code : "+urlc);
InputStream isTemp=urlc.getInputStream();
InputStreamReader isr = new InputStreamReader(isTemp);   //这步报错了
BufferedReader br = new BufferedReader(isr);
FileWriter fw = new FileWriter("C:\\user_data\\21MyTempFile\\temp_post.txt");
String strTemp = "";
while ((strTemp = br.readLine()) != null){
fw.write(strTemp+"\n");
}
System.out.println("post写出报文体完成");
fw.close();
br.close();
isr.close();
isTemp.close();
} catch (Exception e) {
System.out.println("抛出异常,异常信息是: " + e.getMessage());
e.printStackTrace();
}
}

0 0