<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
api6:
httpurl: xxx
@Value("${api.httpurl}")
private String url;
List listj = new LinkedList<>();
BasicNameValuePair params3 = new BasicNameValuePair("Search", "");
// BasicNameValuePair params4 = new BasicNameValuePair("DeviceType", "");
BasicNameValuePair params5 = new BasicNameValuePair("page", "1");
BasicNameValuePair params6 = new BasicNameValuePair("pageSize", "15");
import org.apache.http.HttpEntity;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.List;
//s
public class httpclient {
public static String doPostByJson(String apiUrl, List list,String token) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
// List lists = new LinkedList<>();
// BasicNameValuePair param1 = new BasicNameValuePair(“username”, “acrel”);
// BasicNameValuePair param2 = new BasicNameValuePair(“password”, “acrel001”);
// BasicNameValuePair param3 = new BasicNameValuePair(“userid”, “47”);
// list.add(param1);
// list.add(param2);
// list.add(param3);
try {
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entityParam);
httpPost.setEntity(entityParam);
// 浏览器表示
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 传输的类型
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (token!=null&&token!=""){
httpPost.addHeader("token", token);
}
// 执行请求
response = httpClient.execute(httpPost);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return httpStr;
}
public static String doPostByJsons(String apiUrl, List list,String token) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
// List lists = new LinkedList<>();
// BasicNameValuePair param1 = new BasicNameValuePair(“username”, “acrel”);
// BasicNameValuePair param2 = new BasicNameValuePair(“password”, “acrel001”);
// BasicNameValuePair param3 = new BasicNameValuePair(“userid”, “47”);
// list.add(param1);
// list.add(param2);
// list.add(param3);
try {
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entityParam);
httpPost.setEntity(entityParam);
// 浏览器表示
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 传输的类型
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.addHeader("mark", "relay");
//httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
if (token!=null&&token!=""){
httpPost.addHeader("token", token);
}
// 执行请求
response = httpClient.execute(httpPost);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return httpStr;
}
public static String doPostByJsonss(String apiUrl, List list,String token) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
// List lists = new LinkedList<>();
// BasicNameValuePair param1 = new BasicNameValuePair(“username”, “acrel”);
// BasicNameValuePair param2 = new BasicNameValuePair(“password”, “acrel001”);
// BasicNameValuePair param3 = new BasicNameValuePair(“userid”, “47”);
// list.add(param1);
// list.add(param2);
// list.add(param3);
try {
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entityParam);
httpPost.setEntity(entityParam);
// 浏览器表示
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 传输的类型
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.addHeader("mark", "relay");
//httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
if (token!=null&&token!=""){
httpPost.addHeader("token", token);
}
// 执行请求
response = httpClient.execute(httpPost);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return httpStr;
}
// public static void close(){
// try {
// response.close();
// httpclient.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}