接入第三方接口
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
import org.apache.http.HttpStatus;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public static String httpPostWithForm(String resUrl){
String resultData = "";
try{
HttpPost httppost = new HttpPost(resUrl);
List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairList,"UTF-8");
httppost.setEntity(entity);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httppost);
if ((response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)){
resultData = EntityUtils.toString(response.getEntity(),"UTF-8");
}else{
throw new RuntimeException("接口连接失败!");
}
} catch (Exception e){
throw new RuntimeException("接口连接失败!");
}
return resultData;
}