1.commons-httpclient 简洁快速模拟HTTP请求
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
(1)
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* Hello world!
*
*/
public class App {
private static final HttpClient client = new HttpClient();
public static void main(String[] args) throws HttpException, IOException, InterruptedException {
for (int i = 0; i < 2000; i++) {
PostMethod method = new PostMethod("http://open.kaduoyun.com/api/v1.0/sendMsg.htm");
//http://localhost:8080/vs-rest/rs/user/register/gaozz/jerrison/[email protected]/123456/test/0/30/addrtest/18600635193/touxiang/1100
method.getParams().setContentCharset("UTF-8");
method.addParameter("appkey", "38F325796F584656946801146D177C18");
method.addParameter("appsecret", "4451E66CDBAE8C01FC1B12D70CDFA077");
method.addParameter("title", "测试标题");
method.addParameter("content", "测试内容"+i);
//method.addParameter("clientid", "");
client.executeMethod(method);
System.out.println(method.getResponseBodyAsString()+i);
Thread.sleep(10);
}
}
}
(2)
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* Hello world!
*
*/
public class UserTestApp {
private static final HttpClient client = new HttpClient();
private static final String urlStr = "http://192.168.0.117:8080";
//private static final String urlStr = "http://192.168.0.110:8080/vs-rest";
public static void main(String[] args) throws HttpException, IOException, InterruptedException {
//testLogin();
//testSearchCity();
//testRegister();
testFindPassword();
//testVerifyUnique();
//testUpdateUserInfo();
//testSearch();
//testSchoolCompetitor();
//testSignUp();
//testVote();
}
public static void testRegister() throws HttpException, IOException, InterruptedException{
PostMethod method = new PostMethod(urlStr+"/rs/user/register");
//http://localhost:8080/vs-rest/rs/user/register/gaozz/jerrison/[email protected]/123456/test/0/30/addrtest/18600635193/touxiang/1100
method.getParams().setContentCharset("UTF-8");
method.addParameter("userName", "gaozz");
method.addParameter("nickName", "jerrison11");
method.addParameter("email", "[email protected]");
method.addParameter("password", "123456");
method.addParameter("equipmentId", "test818");
method.addParameter("sex", "0");
method.addParameter("age", "30");
method.addParameter("addr", "addrtest");
method.addParameter("phone", "18600635193");
method.addParameter("avatar", "touxiang");
method.addParameter("birthday", "1100");
client.executeMethod(method);
System.out.println(method.getResponseBodyAsString());
Thread.sleep(10);
}
public static void testLogin() throws HttpException, IOException, InterruptedException{
PostMethod method = new PostMethod(urlStr+"/rs/user/login");
method.getParams().setContentCharset("UTF-8");
method.addParameter("userName", "gaozz");
method.addParameter("password", "654321");
client.executeMethod(method);
System.out.println(method.getResponseBodyAsString());
Thread.sleep(10);
}
public static void testFindPassword() throws HttpException, IOException, InterruptedException{
PostMethod method = new PostMethod(urlStr+"/rs/user/findPassword");
method.getParams().setContentCharset("UTF-8");
method.addParameter("userName", "slaton");
method.addParameter("verifyCode", "060468");
method.addParameter("newPassword", "12345678");
client.executeMethod(method);
System.out.println(method.getResponseBodyAsString());
Thread.sleep(10);
}
}
2.apache org.apache.httpcomponents httpclient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
importorg.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils;
new version 4.5:
public static String httpPostUrlForPdf(String url) { // 设置HTTP请求参数 String result = null; CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); try { CloseableHttpResponse response = client.execute(httpGet); result = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (ClientProtocolException e) { logger.error("http接口调用异常:url is::" + url, e); return null; } catch (IOException e) { logger.error("http接口调用异常:url is::" + url, e); return null; } finally { try { client.close(); } catch (IOException e) { logger.error("http接口调用异常:url is::" + url, e); } } return result; }
old version 4.3:
public static String httpPost(String url, String jsonString) { // 设置HTTP请求参数 String result = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); try { httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);//设置请求超时时间 10s StringEntity entity = new StringEntity(jsonString); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpEntity resEntity = httpClient.execute(httpPost).getEntity(); result = EntityUtils.toString(resEntity, "UTF-8"); } catch (Exception e) { logger.error("http接口调用异常:url is::" + url, e); return null; } finally { httpClient.getConnectionManager().shutdown(); } return result; }
调用:
ReqChedaiParamDataVo dataVo = new ReqChedaiParamDataVo(); dataVo.setIdCard(idCard); Map<String, Object> map = new HashMap<String, Object>(); map.put("content", dataVo); String jsonString = JSONObject.toJSONString(map); LOGGER.info("请求对象json::" + jsonString); String response = HttpUtils.httpPost(queryMemberInfoHttpUrl, jsonString); LOGGER.info("响应字符串::" + response);
Another:
HttpPost httpPost22 = new HttpPost("http://test.com/abc"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); try { httpPost22.setEntity(new UrlEncodedFormEntity(nvps)); httpClient.execute(httpPost); } catch (IOException e) { e.printStackTrace(); }