httpclient发送http请求

    public static void main( String[] args ) throws ClientProtocolException, IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException{
    	SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
    	HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
    	
//        HttpClient httpClient = HttpClients.createDefault();
//        HttpGet httpGet = new HttpGet("https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.6");
//        HttpResponse response = httpClient.execute(httpGet);
//        String html = EntityUtils.toString(response.getEntity());
//        System.out.println(html);
        
        HttpPost httpPost = new HttpPost("https://183.63.49.83:7777/OA/DoLogin");
        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
        JSONObject params = new JSONObject();
        params.put("username", "802755");
        params.put("password", "123456");
        HttpEntity httpEntity = new StringEntity("username=802755&password=123456","UTF-8");
        httpPost.setEntity(httpEntity);
        HttpResponse response2 = httpClient.execute(httpPost);
        String html2 = EntityUtils.toString(response2.getEntity());
        System.out.println(html2);
    }

 

//spring restTemplate
@Test
public void springHttpTemplate() throws ClientProtocolException, IOException {
    HttpClient httpClient = HttpClients
                            .custom()
                            .setSSLHostnameVerifier(newNoopHostnameVerifier())
                            .build();
    HttpComponentsClientHttpRequestFactory requestFactory  = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);
 
    ResponseEntity<String> response 
      = new RestTemplate(requestFactory).exchange(
      urlOverHttps, HttpMethod.GET, null, String.class);
    assertThat(response.getStatusCode().value(), equalTo(200));
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值