Delphi 通過IdHTTP 傳送Http請求到Java 後臺

gdutllf2006發表於2017-02-09
###################################################
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;
這兩個註釋掉,否則重複點選的時候會卡住.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10248702/viewspace-2133304/,如需轉載,請註明出處,否則將追究法律責任。

相關文章