我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Delphi通过IdHTTP怎么发送Http请求到Java后台

这篇文章将为大家详细讲解有关Delphi通过IdHTTP怎么发送Http请求到Java 后台,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联致力于互联网品牌建设与网络营销,包括成都网站制作、成都网站建设、SEO优化、网络推广、整站优化营销策划推广、电子商务、移动互联网营销等。创新互联为不同类型的客户提供良好的互联网应用定制及解决方案,创新互联核心团队十多年专注互联网开发,积累了丰富的网站经验,为广大企业客户提供一站式企业网站建设服务,在网站建设行业内树立了良好口碑。

###################################################
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.

*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet.

*
* 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.

*
* 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.

*
* @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 后台就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享题目:Delphi通过IdHTTP怎么发送Http请求到Java后台
标题路径:http://mswzjz.cn/article/iiecoh.html

其他资讯