原文:https://blog.csdn.net/wild46cat/article/details/52513012
利用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);
- }
- }