HttpClient

使用Java发送get post请求

导入依赖

<!--    GetPost请求-->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.13</version>
</dependency>
<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.75</version>
</dependency>

Get请求

    public static JSONObject get(String url){
        JSONObject r=null;
        //HttpClient client=new DefaultHttpClient(); //新建一个请求对象(过时了)
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet get=new HttpGet(url);//将请求的网址封装在一个get对象中
        get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
      //服务器禁止抓取,设置头文件欺瞒服务器
        try {
            HttpResponse response=client.execute(get); //执行请求
            if(response.getStatusLine().getStatusCode()==200){//如果请求返回的状态码是200,代表正常返回
                HttpEntity ent=response.getEntity(); //返回结果转为实体并再转为支持中文的字符串
                String str= EntityUtils.toString(ent,"utf-8");
                r = JSONObject.parseObject(str);
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return r;
    }

Post请求

    public static JSONObject post(String url,String param){
        JSONObject r=null;
        //HttpClient client=new DefaultHttpClient(); //新建一个请求对象(过时了)
        HttpClient client = HttpClientBuilder.create().build();//获取DefaultHttpClient请求
        HttpPost post=new HttpPost(url);//将请求的网址封装在一个get对象中
        try {
            HttpEntity entity = new StringEntity(param,"utf-8");
            post.setEntity(entity);
            post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
          //服务器禁止抓取,设置头文件欺瞒服务器
            HttpResponse response=client.execute(post); //执行请求
            if(response.getStatusLine().getStatusCode()==200){//如果请求返回的状态码是200,代表正常返回
                HttpEntity ent=response.getEntity(); //返回结果转为实体并再转为支持中文的字符串
                String str= EntityUtils.toString(ent,"utf-8");
                r = JSONObject.parseObject(str);
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return r;
    }

URLDownLoad

public static JSONObject getData(int pageLimit,int pageStart){
        String url = "https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=time&page_limit=Page_LIMIT&page_start=PAGE_START";
        url.replace("Page_LIMIT",pageLimit+"").replace("PAGE_START",pageStart+"");
        //System.out.println(url);
        JSONObject json = null;
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//服务器禁止抓取,用以欺瞒服务器
            InputStream inputStream = connection.getInputStream();
            String resultData = null;      //需要返回的结果
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            int len = 0;
            while((len = inputStream.read(data)) != -1) {
                byteArrayOutputStream.write(data, 0, len);
            }
            resultData = new String(byteArrayOutputStream.toByteArray());
            json = JSONObject.parseObject(resultData);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }
posted on 2021-03-06 12:34  长相厮守  阅读(21)  评论(0)    收藏  举报