这里很重要的一点是明确地说出你必须使用的 apache 的包,因为有不同的方法来实现一个 get 请求。
例如,您可以使用Apache Commons
或HttpComponents
。在这个例子中,我将使用HttpComponents (org.apache.http.*)
请求类:
package request;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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 Task;
public void sendRequest(Task task) throws URISyntaxException {
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme("http")
.setHost("localhost")
.setPort(8080)
.setPath("/TesteHttpRequest/TesteDoLucas")
.addParameter("className", task.getClassName())
.addParameter("dateExecutionBegin", task.getDateExecutionBegin())
.addParameter("dateExecutionEnd", task.getDateExecutionEnd())
.addParameter("lastDateExecution", task.getDateLastExecution())
.addParameter("numberExecutions", Integer.toString(task.getNumberExecutions()))
.addParameter("idTask", Integer.toString(task.getIdTask()))
.addParameter("numberExecutions" , Integer.toString(task.getNumberExecutions()));
URI uri = uriBuilder.build();
HttpGet getMethod = new HttpGet(uri);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
response = httpclient.execute(getMethod);
} catch (IOException e) {
//handle this IOException properly in the future
} catch (Exception e) {
//handle this IOException properly in the future
}
}
我正在使用 Tomcat v7.0 服务器,然后上面的类接收一个任务并将其发送到链接http://localhost:8080/TesteHttpRequest/TesteDoLucas中的特定 servlet 。
我的动态 Web 项目名为TesteHttpRequest,我的 servlet 由 url /TesteDoLucas 参与
任务类:
package bean;
public class Task {
private int idTask;
private String taskDescription;
private String dateExecutionBegin;
private String dateExecutionEnd;
private String dateLastExecution;
private int numberExecutions;
private String className;
public int getIdTask() {
return idTask;
}
public void setIdTask(int idTask) {
this.idTask = idTask;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
public String getDateExecutionBegin() {
return dateExecutionBegin;
}
public void setDateExecutionBegin(String dateExecutionBegin) {
this.dateExecutionBegin = dateExecutionBegin;
}
public String getDateExecutionEnd() {
return dateExecutionEnd;
}
public void setDateExecutionEnd(String dateExecutionEnd) {
this.dateExecutionEnd = dateExecutionEnd;
}
public String getDateLastExecution() {
return dateLastExecution;
}
public void setDateLastExecution(String dateLastExecution) {
this.dateLastExecution = dateLastExecution;
}
public int getNumberExecutions() {
return numberExecutions;
}
public void setNumberExecutions(int numberExecutions) {
this.numberExecutions = numberExecutions;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
小服务程序类:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/TesteDoLucas")
public class TesteHttpRequestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getQueryString();
System.out.println(query);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
发送的查询参数将显示在控制台。
className=java.util.Objects%3B&dateExecutionBegin=2016%2F04%2F07+22%3A22%3A22&dateExecutionEnd=2016%2F04%2F07+06%3A06%3A06&lastDateExecution=2016%2F04%2F07+11%3A11%3A11&numberExecutions=10&idTask=1 10
要修复编码,您可以查看此处:HttpServletRequest UTF-8 Encoding