这里需要明确说明必须使用的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) {
} catch (Exception e) {
}
}
我将为您翻译此内容:
我正在使用Tomcat v7.0服务器,然后上面的类接收一个任务并将其发送到链接中特定的servlet http://localhost:8080/TesteHttpRequest/TesteDoLucas。
我的动态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;
}
}
Servlet类:
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&numberExecutions=10
为了修复编码问题,您可以查看这里:
HttpServletRequest UTF-8 编码。
HttpGet
(在查询字符串中设置参数),对于HttpPost
、HttpPut
或HttpDelete
没有任何影响。 尽管每个方法都有一个接受URI
参数的构造函数,但我不认为URIBuilder
隐式地知道如何将查询字符串参数转换为HttpPost
POST变量等。 因此,尽管我将使用带有完整查询字符串的URI传递非HttpGet
方法,但我不认为它们会知道如何将该查询字符串转换为他们知道如何处理的数据格式。 - user1768830setParameter
方法会覆盖现有的值。因此,如果要在 URI 中设置List
变量,例如/search?q=1&q=2&y=3
,那么这里的q
是一个列表,其最终值将是 2 而不是 [1,2]。为了避免这种情况,可以使用 URIBuilder 的addParameter
方法。文档:http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html#addParameter%28java.lang.String,%20java.lang.String%29 - Siddharth Trikha