這篇文章將為大家詳細講解有關Delphi通過IdHTTP怎么發(fā)送Http請求到Java 后臺,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設與網(wǎng)絡營銷,包括成都網(wǎng)站制作、成都網(wǎng)站建設、SEO優(yōu)化、網(wǎng)絡推廣、整站優(yōu)化營銷策劃推廣、電子商務、移動互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應用定制及解決方案,創(chuàng)新互聯(lián)核心團隊十多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗,為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設服務,在網(wǎng)站建設行業(yè)內(nèi)樹立了良好口碑。
###################################################
Delphi 通過IdHTTP 發(fā)送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;//請求參數(shù)列表
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;
//獲取網(wǎng)頁返回的信息
ResponseStr := ResponseStream.DataString;
//網(wǎng)頁中的存在中文時,需要進行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;
//獲取網(wǎng)頁返回的信息
ResponseStr := ResponseStream.DataString;
//網(wǎng)頁中的存在中文時,需要進行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怎么發(fā)送Http請求到Java 后臺就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。