您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。