美文网首页
java发送http请求

java发送http请求

作者: 陈灬大灬海 | 来源:发表于2019-08-26 16:07 被阅读0次

restTemplate

get请求

@Test
public void sendRequestByRestTemplateGet() throws RestClientException, URISyntaxException{
    String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm?username=13121939122&password=123456";
    RestTemplate rest = new RestTemplate();
    rest.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    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")));
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
    map.add("username","13121939122");
    map.add("password","123456");
    String postForObject = rest.postForObject(new URI(url), map, String.class);
    System.out.println(postForObject);
}

apache.http.client

get请求

@Test
public void sendRequestByHttpclientGet() throws RestClientException, URISyntaxException, InterruptedException, IOException {
    String url = "http://api.sms.bambika.co.ke:8555/?target=BANGCASINO&msisdn=254721567415&text=This+is+a+test+message&login=bang&pass=ea62ee1413b5ae1e79f9a844e711bba9";
    //创建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", "13121939122");
    BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
    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());
}

okhttp(支持异步和同步)

下面是异步请求,在@Test里面测试的时候并没有返回结果,但是睡眠一下就可以了,因为就是异步原因。

@Test
public void sendRequest() throws RestClientException, URISyntaxException, InterruptedException{
    String url = "http://www.xinghengedu.com/autotele/simpleLogin.htm?username=dahai&password=123456";
    OkHttpClient okHttpClient = new OkHttpClient();
    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());
        }
    });
}

相关文章

网友评论

      本文标题:java发送http请求

      本文链接:https://www.haomeiwen.com/subject/uxkdectx.html