java httpclient 发送post请求
1. pom 配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>TestNgDemo</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <!--testng打jar包运行的插件引入--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile> ./src/main/resources/testng.xml </suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.10</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.14</version> </dependency> <!--引入测试报告相关包--> <dependency> <groupId>com.vimalselvam</groupId> <artifactId>testng-extentsreport</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>3.0.6</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.8</version> </dependency> </dependencies> </project>
请求代码
package com.testng; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; public class Testng { public static void main(String[] args) { List list = new ArrayList(); list.add(new BasicNameValuePair("username","royfans")); list.add(new BasicNameValuePair("password","Zlf111")); String result = httpPostList(list); System.out.println("List post:" + result); JSONObject object = new JSONObject(); object.put("username","royfans"); object.put("password","Zlf111"); String result2 = httpPostJson(object); System.out.println(result2); } public static String httpPostList(List list){ // 调用https 方法,实现https请求 DefaultHttpClient httpClient = null; try { httpClient = new SSLClient(); } catch (Exception e) { e.printStackTrace(); } String loginUrl = "https://localhost/web/v1/login"; String result = null; HttpPost post = new HttpPost(loginUrl); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(list,"UTF-8"); post.setEntity(entity); CloseableHttpResponse response = httpClient.execute(post); HttpEntity entity1 = response.getEntity(); if (entity1 != null){ // System.out.println(EntityUtils.toString(entity1,"UTF-8")); result = EntityUtils.toString(entity1,"UTF-8"); } // System.out.println(entity1); httpClient.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } public static String httpPostJson(JSONObject jsonObject){ DefaultHttpClient httpClient = new DefaultHttpClient(); String loginUrl = "http://172.30.154.111:8088/web/v1/login"; String result = null; HttpPost post = new HttpPost(loginUrl); post.setHeader("content-type","application/json"); StringEntity entity = new StringEntity(jsonObject.toString(),"UTF-8"); post.setEntity(entity); try { HttpResponse response = httpClient.execute(post); result = EntityUtils.toString(response.getEntity(),"UTF-8"); } catch (IOException e) { e.printStackTrace(); } return result; } } //用于进行Https请求的HttpClient class SSLClient extends DefaultHttpClient{ public SSLClient() throws Exception{ super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } }