这篇文章将为大家详细讲解有关Delphi通过IdHTTP怎么发送Http请求到Java 后台,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
###################################################
Delphi 通过IdHTTP 发送Http请求到Java 后台
###################################################
1 定义一个Java Servlet "ServletDelphi"处理请求;具体参考Servlet的文档
文件:ServletDelphi.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDelphi extends HttpServlet {
/**
* Constructor of the object.
*/
public ServletDelphi() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Hello Servlet Delphi!");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Hello Servlet, " + request.getParameter("user") + "!");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
2 在Delphi上定义两个Button单击事件
//Post请求
procedure TForm1.Button1Click(Sender: TObject);
var
Url : string;//请求地址
ParamList :TStringList;//请求参数列表
ResponseStream : TStringStream; //返回信息
ResponseStr : string;
begin
ParamList := TStringList.Create;
ParamList.Add('user=linlf');
ResponseStream := TStringStream.Create('');
try
//请求地址
Url := 'http://localhost:8080/testdelphi/servlet/ServletDelphi';
try
IdHTTP1.Post(Url, ParamList,ResponseStream);
// IdHTTP1.Get(Url,ResponseStream);
except
on e : Exception do
begin
ShowMessage(e.Message);
end;
end;
//获取网页返回的信息
ResponseStr := ResponseStream.DataString;
//网页中的存在中文时,需要进行UTF8解码
ResponseStr := UTF8Decode(ResponseStr);
Memo1.Text := ResponseStr;
finally
//IdHTTP1.Free;
// ResponseStream.Free;
end;
end;
//Get请求
procedure TForm1.Button2Click(Sender: TObject);
var
Url : string;//请求地址
ResponseStream : TStringStream; //返回信息
ResponseStr : string;
begin
ResponseStream := TStringStream.Create('');
try
//请求地址
Url := 'http://localhost:8080/testdelphi/servlet/ServletDelphi';
try
IdHTTP1.Get(Url,ResponseStream);
except
on e : Exception do
begin
ShowMessage(e.Message);
end;
end;
//获取网页返回的信息
ResponseStr := ResponseStream.DataString;
//网页中的存在中文时,需要进行UTF8解码
ResponseStr := UTF8Decode(ResponseStr);
Memo1.Text := ResponseStr;
finally
//IdHTTP1.Free;
// ResponseStream.Free;
end;
end;
3 验证通过
Memo1输出正确的结果
4 问题记录
1)IdHTTP控件在Indy Clients的栏目中
2) finally
//IdHTTP1.Free;
// ResponseStream.Free;
这两个注释掉,否则重复点击的时候会卡住.
关于Delphi通过IdHTTP怎么发送Http请求到Java 后台就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。