使用Delphi的TIdHttp控件发起一个POST请求很简单,定义一个TStringList实例,往上面添加参数就行:
procedure TForm1.Button1Click(Sender: TObject);
var
HttpClient: TIdHttp;
ParamList: TStringList;
begin
HttpClient := TIdHttp.Create();
ParamList := TStringList.Create;
ParamList.Add('user=pan');
ShowMessage(HttpClient.Post('http://192.168.0.105:8080/Servlets/Submit', ParamList));
end;
运行代码,使用WireShark抓包工具看到这个POST请求发出来了,Content-Type是application/x-www-form-urlencoded,这种类型的意思是将参数打包成key-value的形式。
然后来看看服务端的Java代码:
public class Submit extends HttpServlet {
...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().