zoukankan      html  css  js  c++  java
  • HttpClient案例

    依赖

    <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
        <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
          <scope>provided</scope>
        </dependency>

    GET

    public static void main(String[] args)throws Exception {
            //步骤一:创建一个链接
            CloseableHttpClient client= HttpClients.createDefault();
            //创建一个请求
            HttpGet httpGet=new HttpGet("https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2020-02-05&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=SHH&purpose_codes=ADU");
            //执行请求获取节国
            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();
        }

    POST

    public static void main(String[] args) throws Exception{
            //步骤一:创建一个链接
            CloseableHttpClient client= HttpClients.createDefault();
            //创建请求
            HttpPost post=new HttpPost("http://localhost:8080/post");
            //创建参数队列
            List<NameValuePair> pairList=new ArrayList<>();
            pairList.add(new BasicNameValuePair("uname","梅川酷子"));
            //创建请求体,封装参数
            UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairList,"UTF-8");
            //将请求体交给当前请求
            post.setEntity(entity);
    
            //发送请求,接收结果
            CloseableHttpResponse response = client.execute(post);
            System.out.println("接收到的结果为:"+ EntityUtils.toString(response.getEntity(),"UTF-8"));
    
    
            //关闭资源
            response.close();
            client.close();
    
        }

    Servlet

        @WebServlet("/post")
        public class postServlet extends HttpServlet {
        
        
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                super.doPost(req, resp);
            }
        
            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                String uname = req.getParameter("uname");
                System.out.println("接收到的值"+uname);
                resp.setContentType("text/html");
                resp.setCharacterEncoding("UTF-8");
                resp.getWriter().write("梅川酷子");
            }
        }
  • 相关阅读:
    MTK 定时器 休眠时的动作
    Troubleshooting MySQL Memory Usage
    disruptor
    Google Protocol Buffer 的使用和原理
    百度贴吧10亿量级LAMP架构分享
    nginx 不带www到www域名的重定向
    配置电信网通双线双IP的解决办法
    Create a W3C validated anchor link with target=“_blank”
    Could not update ICEauthority file /home/username/.ICEauthority
    Can't load IA 32bit .dll on a AMD 64bit platform
  • 原文地址:https://www.cnblogs.com/whtt/p/12263670.html
Copyright © 2011-2022 走看看