java发起http请求

  • Post author:
  • Post category:JAVA
  • Post comments:0评论

HttpClient

get

    @Test
    public void sendRequestByHttpclientGet() throws RestClientException, URISyntaxException, InterruptedException, IOException {
        String url = "http://api.sms.bambika.co.ke:8555/?target=aaa79f9a844e711bba9";
        //创建CloseableHttpClient
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient client = builder.build();
        //执行
        HttpUriRequest httpGet = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String entityStr = EntityUtils.toString(entity, "utf-8");
            System.out.println(entityStr);
        }
        System.out.println(response.toString());
    }

post

    @Test
    public void sendRequestByHttpclientPost() throws RestClientException, URISyntaxException, InterruptedException, IOException {
        String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm";
        //创建CloseableHttpClient
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient client = builder.build();
        //执行
        HttpPost httpPost = new HttpPost(url);

        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("username", "131");
        BasicNameValuePair param2 = new BasicNameValuePair("password", "aaa");
        list.add(param1);
        list.add(param2);

        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");

        httpPost.setEntity(entityParam);
        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String entityStr = EntityUtils.toString(entity, "utf-8");
            System.out.println(entityStr);
        }
        System.out.println(response.toString());
    }

RestTemplate

get

    @Test
    public void sendRequestByRestTemplateGet() throws RestClientException, URISyntaxException {
        String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm?username=131&password=aaa";
        RestTemplate rest = new RestTemplate();
        rest.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        // get请求
        String resString = rest.getForObject(new URI(url), String.class);
        System.out.println(resString);
    }

post

    @Test
    public void sendRequestByRestTemplatePost() throws RestClientException, URISyntaxException {
        String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm";
        RestTemplate rest = new RestTemplate();
        rest.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        // post请求
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");

        org.springframework.http.HttpEntity<MultiValueMap> requestEntity = new org.springframework.http.HttpEntity<MultiValueMap>(body, headers);

        body.add("username", "131");
        body.add("password", "aaa");

        String postForObject = rest.postForObject(new URI(url), requestEntity, String.class);
        System.out.println(postForObject);
    }

OkHttp

get

    @Test
    public void sendRequestByOkHttpGet() throws RestClientException, URISyntaxException, InterruptedException {
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm?username=131&password=aaa";
        final Request request = new Request.Builder()
            .url(url)
            .get()//默认就是GET请求,可以不写
            .build();
        Call call = okHttpClient.newCall(request);
        System.out.println(call);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(call);
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });
        TimeUnit.SECONDS.sleep(1);
    }

OkHttp

post

    @Test
    public void sendRequestByOkHttpPost() throws RestClientException, URISyntaxException, InterruptedException, IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm";

        RequestBody body = new FormBody.Builder()
            .add("username", "131")
            .add("password", "aaa").build();

        Request request = new Request.Builder().url(url).post(body).build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });
//        System.out.println(call.execute().cacheResponse().body());
        TimeUnit.SECONDS.sleep(1);
    }

Dylan

面朝大海·春暖花开

发表评论