一个项目去请求另一个项目资源,此时作为客户端。
一个项目被另一个项目请求获取资源,此时作为服务端。
一个项目可能既是客户端又是服务端。
下面是client接口和server接口开发例子,可直接运行,亲测有效。
请求接口使用HttpClient:
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.HttpGet;
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.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class MyFirstHttpReq {
public static void doGet() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8080/httplearn_Web_exploded/MyfirstHttpResp?name=zhengym&id=111");
CloseableHttpResponse response = httpClient.execute(httpGet);
//建立的http连接,仍旧被response保持着,允许我们从网络socket中获取返回的数据
//为了释放资源,我们必须手动消耗掉response或者取消连接(使用CloseableHttpResponse类的close方法)
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} finally {
response.close();
}
}
public void doPost() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/httplearn_Web_exploded/MyfirstHttpResp");
//拼接参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpClient.execute(httpPost);
try
{
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}
finally
{
response.close();
}
}
public static void main(String[] args) throws IOException {
MyFirstHttpReq.doGet();
}
}
作为服务端接口使用servlet:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
public class MyFirstHttpResp extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String ip = req.getRequestURI();
Map<String, String[]> map = req.getParameterMap();
StringBuffer inputString = new StringBuffer();
for (String key:map.keySet()) {
inputString.append(key);
inputString.append("=");
inputString.append(Arrays.toString(map.get(key)));
}
System.out.println(inputString.toString());
resp.setContentType("application/json;charset=utf-8");
resp.setCharacterEncoding("utf-8");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String ip = req.getRequestURI();
BufferedReader reader = req.getReader();
String line = "";
StringBuffer inputString = new StringBuffer();
while ((line = reader.readLine())!=null) {
inputString.append(line);
}
System.out.println(inputString);
resp.setContentType("application/json;charset=utf-8");
resp.setCharacterEncoding("utf-8");
}
}