package com.dahai.httpclientTest;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.ClientProtocolException;
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.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
CloseableHttpClient clientGet = HttpClients.createDefault();
CloseableHttpClient clientPost = HttpClients.createDefault();
CloseableHttpClient clientSoap = HttpClients.createDefault();
/**********第一种创建GET请求的方法:************/
HttpGet httpGet1 = new HttpGet("http://localhost:8080/mobilePhone?model=iPhone+6S");
/**********第二种创建GET请求的方法:************/
//1.通过URIBuilder()创建一个uri,setScheme设置协议,setHost设置IP,setPort设置端口号,
//setPath设置路径,setParameter设置参数,build()进行构建
URI uri = null;
try {
uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8080)
.setPath("/mobilePhone").setParameter("model","iPhone 6S").build();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建GET请求
HttpGet httpGet2 = new HttpGet(uri);
/***************创建POST请求*****************/
//1.设置POST的uri
HttpPost httpPost = new HttpPost("http://localhost:8080/mobilePhone");
//2.创建一个JSON串,并为请求设置请求体格式
StringEntity stringEntityPost = new StringEntity("{\"brand\":\"Motorola\",\"model\":\"moto Z Play\",\"os\":\"ANDROID\"}",ContentType.APPLICATION_JSON);
//3.将JSON串填入POST请求
httpPost.setEntity(stringEntityPost);
/***************创建SOAP请求*****************/
//1.设置SOAP的uri
HttpPost httpSoap = new HttpPost("http://localhost:8080/MobilePhones");
//2.创建一个XML串
String soapString ="<SOAP-ENV:Envelope "
+ "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
+ "xmlns:hi=\"http://www.lujiatao.com/httpinterface/MobilePhones\">"
+ " <SOAP-ENV:Header/>\r\n"
+ " <SOAP-ENV:Body>\r\n"
+ " <hi:getMobilePhoneRequest>\r\n"
+ " <hi:model>iPhone 6S</hi:model>\r\n"
+ " </hi:getMobilePhoneRequest>\r\n"
+ " </SOAP-ENV:Body>\r\n"
+ "</SOAP-ENV:Envelope>";
//3.为请求设置请求体格式
StringEntity stringEntitySoap = new StringEntity(soapString,ContentType.TEXT_XML);
//4.将XML串填入SOAP请求
httpSoap.setEntity(stringEntitySoap);
/*******************创建一个响应******************/
CloseableHttpResponse responseGet1 = null ;
CloseableHttpResponse responseGet2 = null ;
CloseableHttpResponse responsePost = null ;
CloseableHttpResponse responseSoap = null ;
/*******************发送请求并获得响应(execute:执行)******************/
try {
responseGet1 = clientGet.execute(httpGet1);
responseGet2 = clientGet.execute(httpGet2);
responsePost = clientPost.execute(httpPost);
responseSoap = clientSoap.execute(httpSoap);
/*******************打印响应报文(getEntity:生成应答报文,EntityUtils.toString:)******************/
System.out.println(EntityUtils.toString(responseGet1.getEntity()));
System.out.println(EntityUtils.toString(responseGet2.getEntity()));
System.out.println(EntityUtils.toString(responsePost.getEntity()));
System.out.println(EntityUtils.toString(responseSoap.getEntity()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}