中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Delphi通過IdHTTP怎么發送Http請求到Java 后臺

發布時間:2021-12-20 14:07:39 來源:億速云 閱讀:332 作者:柒染 欄目:互聯網科技

這篇文章將為大家詳細講解有關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 后臺就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

营口市| 岳西县| 宜宾县| 巧家县| 伊通| 中山市| 兴国县| 九江市| 瑞金市| 尚义县| 商洛市| 浦东新区| 香港| 甘孜| 永安市| 兴海县| 卢湾区| 西乡县| 桓仁| 二连浩特市| 宁化县| 益阳市| 庆城县| 昂仁县| 中宁县| 马关县| 社旗县| 怀宁县| 永宁县| 宜宾县| 当涂县| 绥芬河市| 阳城县| 静宁县| 莒南县| 新密市| 东乡| 金川县| 朔州市| 谷城县| 北京市|