使用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();
}
}
- 使用JAVA Get POST 抓网页的练习代码
- fiddler抓包软件模拟GET和POST的使用
- c#使用post,get请求网页
- java httpclient 抓取网页 POST GET
- POST、GET的测试代码
- 使用HttpURLConnection抓网页代码示例
- HttpClient的Post和Get访问网页
- jsp“抓”网页代码的程序
- JSP“抓”网页代码的程序
- java代码 发送GET、POST请求
- GET和POST的使用
- java使用HttpClient模拟发送http的get、post请求
- 使用java的过滤器filter备忘(get和post)
- java使用httpclient封装post请求和get的请求
- 抓包工具Fiddler的使用教程(十一):如何知道HTTP请求是POST方法还是GET方法
- Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
- 代码中的post和get的方法
- OKhttp3 get/post使用及获取网页源代码
- 第十一周(Dynamic ProgrammingV)
- C语言入门(一)
- Boyer Moore算法分析总结
- Bash循环处理带有空格的文件名
- 21---包管理(rpm & yum)
- 使用JAVA Get POST 抓网页的练习代码
- 类
- 如何避免javascript中的冲突
- Gem Port和T-CONT实现业务复用
- 欢迎使用CSDN-markdown编辑器
- 二分贪心 B
- Web前端开发面试题——将字符串转成驼峰写法
- [UE4]方法标签(总结)
- cocos Creator计时器schedule的使用和坑