zoukankan      html  css  js  c++  java
  • 1.HTTP与HTTPS区别

     

    1.HTTP与HTTPS区别

    HTTP协议默认采用80端口 

    HTTPS协议默认采用443     

    HTTPS相较于HTTP协议更安全一些,采用SSL+安全证书,但是效率低

    2.使用代码格式模拟HTTP请求(HTTPClient)

    2.1 使用HttpClient模拟get请求

    复制代码
    //get请求
        public static void getRequest() throws IOException {
            //创建一个默认链接
            CloseableHttpClient client= HttpClients.createDefault();
            //创建一个请求
            HttpGet httpGet=new HttpGet("https://www.baidu.com");
            //执行请求获取响应的结果
            CloseableHttpResponse response=client.execute(httpGet);
            //获取响应的状态码
            System.out.println("服务器返回的状态码:"+response.getStatusLine().getStatusCode());
            //服务器正常响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应的结果
                System.out.println(EntityUtils.toString(response.getEntity(),"UTF-8"));
    
            }
            //关闭结果对象
            response.close();
            //关闭连接
            client.close();
        }
    复制代码

    2.2 运行结果

     、

    2.3 模拟post请求

    编写一个servlet

    复制代码
    @WebServlet("/HttpClientServlet")
    public class HttpClientServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取数据
            String username = req.getParameter("username");
            System.out.println("接收的数据:"+username);
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("服务器接收到数据啦~");
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req,resp);
        }
    }
    复制代码

    2.4 post请求

    复制代码
    //模拟post请求
        public static void postRequest()throws IOException{
            //创建连接
            CloseableHttpClient client=HttpClients.createDefault();
            //创建请求
            HttpPost post=new HttpPost("http://localhost:8080/HttpClientServlet");
            //创建参数队列
            List<NameValuePair> pairs=new ArrayList<>();
            pairs.add(new BasicNameValuePair("username","张三"));
            //创建请求体,封装参数
            UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairs,"UTF-8");
            //将请求体交给当前请求
            post.setEntity(entity);
    
            //发送请求
            CloseableHttpResponse response=client.execute(post);
            System.out.println("接收到的结果为:"+EntityUtils.toString(response.getEntity(),"UTF-8"));
            //关闭资源
            response.close();
            client.close();
    
        }
    }
  • 相关阅读:
    Week3 Teamework from Z.XML-团队分工及贡献分分配办法
    软件工程项目组Z.XML会议记录 2013/09/25
    Week2 Teamework from Z.XML 软件分析与用户需求调查(五)从对比中看见必应助手发展空间
    Week2 Teamework from Z.XML 软件分析与用户需求调查(三)必应助手体验评测
    Week2 Teamework from Z.XML 软件分析与用户需求调查(二)应用助手功能评测
    Week2 Teamework from Z.XML
    软件工程项目组Z.XML会议记录 2013/09/18
    [Go]条件语句
    Go常量与枚举类型
    Go内建变量类型
  • 原文地址:https://www.cnblogs.com/danxun/p/12268435.html
Copyright © 2011-2022 走看看