package com.properties;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class ReadPro01 {
public static void main(String[] args) {
Properties properties = new Properties();
try {
FileInputStream inputStream = new FileInputStream
("E:\\IDEA\\idea_work\\项目01\\src\\main\\java\\com\\properties\\env.properties");
properties.load(inputStream);
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String verify_code = properties.getProperty("verify_code");
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.addHeader("content-Type","application/x-www-form-urlencoded; charset=UTF-8");
List<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("username",username));
list.add(new BasicNameValuePair("password",password));
list.add(new BasicNameValuePair("verify_code",verify_code));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
post.setEntity(entity);
long l1 = System.currentTimeMillis();
CloseableHttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity);
System.out.println(string);
}
long l2 = System.currentTimeMillis();
long time = l2-l1;
System.out.println("响应时间:"+time+"mm");
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
}
}
HttpClient请求响应时间
最新推荐文章于 2023-11-15 19:22:32 发布