java 模拟浏览器进行get和post请求
来源:互联网 发布:网络舆情汇报 编辑:程序博客网 时间:2024/04/30 22:06
java 模拟浏览器进行get和post请求——本文参考《疯狂java讲义》
利用java的URLConnection能够模拟浏览器访问服务器的资源,下面是一个简答的例子:
package com.xueyoucto.xueyou;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 java.util.List;import java.util.Map;/** * Hello world! */class GetPostTest { public static String sendGet(String url, String param) { String result = ""; String urlName = url + "?" + param; try { URL realURL = new URL(urlName); URLConnection conn = realURL.openConnection(); 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/47.0.2526.106 Safari/537.36"); conn.connect(); Map<String, List<String>> map = conn.getHeaderFields(); for (String s : map.keySet()) { System.out.println(s + "-->" + map.get(s)); } BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result += "\n" + line; } } catch (IOException e) { e.printStackTrace(); } return result; } public static String sendPost(String url,String param){ String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); 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/47.0.2526.106 Safari/537.36"); //post设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.print(param); out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8")); String line; while((line = in.readLine()) != null){ result +="\n" + line; } } catch (IOException e) { e.printStackTrace(); } return result; }}public class App { public static void main(String[] args) throws Exception { System.out.println("hello world"); String sendRecvGet =GetPostTest.sendGet("http://localhost:8989/testServletEncoding/encodingTest","param=xxxxx嘻嘻嘻"); System.out.println(sendRecvGet); String sendRecvPost =GetPostTest.sendPost("http://localhost:8989/testServletEncoding/encodingTest","param=就是我"); System.out.println(sendRecvPost); }}
下面是运行结果:
服务器端就是简单的servlet,根据客户端给出的参数进行返回:
package com.xueyoucto.xueyou;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;/** * Created by Administrator on 2016-09-08. */@WebServlet(name = "EncodingTestServlet",urlPatterns = "/encodingTest")public class EncodingTestServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setHeader("content-type","text/html;charset=UTF-8"); String s = request.getParameter("param"); System.out.println(s); PrintWriter printWriter = response.getWriter(); printWriter.write(s); }}
1 0
- java 模拟浏览器进行get和post请求
- java后台模拟浏览器请求--GET/POST
- java http get post 各种请求,模拟浏览器请求
- java模拟get/post请求
- java使用httpclient模拟post请求和get请求示例
- HttpWebRequest模拟浏览器的GET、POST请求
- Java模拟HTTP的Get和Post请求
- Java模拟HTTP的Get和Post请求
- Java模拟HTTP的Get和Post请求
- java HttpUrlConnection get和post 模拟请求 调用接口
- java 模拟http的get和post请求
- [引用]java模拟游览器进行post和get
- JAVA利用HttpClient进行POST和GET请求(HTTPS)
- 使用java模拟Get与Post请求
- Java 后台模拟发送 POST GET 请求
- Java利用Http 模拟POST、GET请求
- java模拟HTTP post,get请求
- Java模拟Http post、get请求
- PHP+mysql链接
- jQuery 放大镜(面向过程)
- RecyclerView的使用教程1
- Linux-C基础知识学习:C语言作业-把100~200之间的不能被3整除的数输出
- <link>标签中的rel="sheetstyle"含义
- java 模拟浏览器进行get和post请求
- CSS3 animation--仿写炫酷的下拉菜单
- Dex分包
- Android微信支付重点说明
- 线程的生命周期
- CSDN code使用教程之git用法详解
- MyBatis高级查询
- java实现Socket通信(一)---单线程服务
- [BZOJ1085【骑士精神】]