GET
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class httpGet_Test {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/jwd/get");
uriBuilder.setParameter("jd","33");
uriBuilder.setParameter("wd","232");
HttpGet httpGet = new HttpGet(uriBuilder.build());
System.out.println("::"+httpGet);
CloseableHttpResponse response = null;
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content);
}
}
}
POST
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class httpPost_Test {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/jwd/getp");
uriBuilder.setParameter("jd","jd12");
uriBuilder.setParameter("wd","wd66");
HttpPost httpPostt = new HttpPost(uriBuilder.build());
System.out.println("::"+httpPostt);
CloseableHttpResponse response = null;
response = httpClient.execute(httpPostt);
if(response.getStatusLine().getStatusCode() == 200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content);
}
}
}