• httpclient工具使用(org.apache.httpcomponents.httpclient)


    原文地址:https://www.cnblogs.com/achengmu/p/11075007.html

    httpclient工具使用(org.apache.httpcomponents.httpclient)

    引入依赖

    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

    get请求

    public static void main(String[] args) throws Exception {
             
            //创建httpclient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建Http get请求
            HttpGet httpGet = new HttpGet("http://www.xxx.com/rest/content?categoryId=4&page=1&rows=20");
            //接收返回值
            CloseableHttpResponse response = null;
             
            try {
                //请求执行
                response = httpClient.execute(httpGet);
                if(response.getStatusLine().getStatusCode()==200){
                    String content = EntityUtils.toString(response.getEntity(), "utf-8");
                    System.out.println("--------->" + content);
                }
            }finally{
                if(response!=null){
                    response.close();
                }
                httpClient.close();
            }

    get带参数请求

    public static void main(String[] args) throws Exception{
             
            //创建httpClient对象
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //定义http get参数
            URI uri = new URIBuilder("http://www.xxxx.com/rest/content").setParameter("categoryId", "4")
                    .setParameter("page", "1").setParameter("rows", "30").build();
            System.out.println(uri);
            //创建http get请求
            HttpGet httpGet = new HttpGet(uri);
             
            //接受请求返回的定义
            CloseableHttpResponse response = null;
            try {
                //执行get请求
                response = httpClient.execute(httpGet);
                if(response.getStatusLine().getStatusCode()==200){
                    String content = EntityUtils.toString(response.getEntity(), "utf-8");
                    System.out.println(content);
                }
            }finally{
                if(response!=null){
                    response.close();
                }
                httpClient.close();
            }
        }

    http post请求

    public static void main(String[] args) throws Exception {
     
           // 创建Httpclient对象
           CloseableHttpClient httpclient = HttpClients.createDefault();
     
           // 创建http POST请求
           HttpPost httpPost = new HttpPost("http://www.oschina.net/");
           // 伪装成浏览器
           httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
     
           CloseableHttpResponse response = null;
           try {
               // 执行请求
               response = httpclient.execute(httpPost);
               // 判断返回状态是否为200
               if (response.getStatusLine().getStatusCode() == 200) {
                   String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                   System.out.println(content);
               }
           } finally {
               if (response != null) {
                   response.close();
               }
               httpclient.close();
           }
     
       }

    http post带参数请求

    public static void main(String[] args) throws Exception{
            //创建httpclient
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //创建http post
            HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
            //模拟浏览器设置头
            httpPost.setHeader(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
         
            //设置请求数据
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("scope", "project"));
            params.add(new BasicNameValuePair("q", "java"));
            params.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
            //构建表单
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);
            //将表达请求放入到httpost
            httpPost.setEntity(formEntity);
             
            //返回类型
            CloseableHttpResponse response = null;
             
            try {
                response = httpClient.execute(httpPost);
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                System.out.println(content);
            }finally{
                if(response!=null){
                    response.close();
                }
                httpClient.close();
            }
             
         
        }
  • 相关阅读:
    struts_login实例
    myEclipse 7.0快捷键
    Visual C# 2008+SQL Server 2005 数据库与网络开发 8.1 数据绑定
    Visual C# 2008+SQL Server 2005 数据库与网络开发 7.2 ADO .NET与各种数据库的连接
    Visual C# 2008+SQL Server 2005 数据库与网络开发8.1.2 创建DataSet
    Visual C# 2008+SQL Server 2005 数据库与网络开发8.2.1 使用连接字符串
    Visual C# 2008+SQL Server 2005 数据库与网络开发第8章 使用数据绑定和DataSet
    Visual C# 2008+SQL Server 2005 数据库与网络开发 7.4 小结
    Visual C# 2008+SQL Server 2005 数据库与网络开发8.2.2 用户数据操作的并发
    Visual C# 2008+SQL Server 2005 数据库与网络开发 7.3 使用ADO .NET处理数据
  • 原文地址:https://www.cnblogs.com/dyh004/p/12201020.html
Copyright © 2020-2023  润新知