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 後臺
###################################################
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- .net 後臺 傳送http請求HTTP
- java傳送http請求JavaHTTP
- 通過PowerShell傳送TCP請求TCP
- java傳送http的get、post請求JavaHTTP
- perl傳送http請求HTTP
- 使用httpclient傳送http請求HTTPclient
- Android 傳送HTTP GET POST 請求以及通過 MultipartEntityBuilder 上傳檔案(二)AndroidHTTPUI
- 使用Feign傳送HTTP請求HTTP
- python傳送HTTP POST請求PythonHTTP
- Java傳送Post請求Java
- 在Java中,使用HttpUtils實現傳送HTTP請求JavaHTTP
- 前端傳送的請求,是如何請求到後端服務的?前端後端
- 通過 Apache Commons HttpClient 傳送 HTTPS 請求ApacheHTTPclient
- 使用requests庫來傳送HTTP請求HTTP
- nodejs使用request傳送http請求NodeJSHTTP
- 【轉】怎麼用PHP傳送HTTP請求(POST請求、GET請求)?PHPHTTP
- 解決.NET Core Ajax請求後臺傳送引數過大請求失敗問題
- Go使用net/http庫傳送GET請求GoHTTP
- Go HTTP GET 請求可以傳送 body 嗎GoHTTP
- PHP 傳送 HTTP 請求的幾種方式PHPHTTP
- Windows Phone 8.1 傳送http 網路請求。WindowsHTTP
- php curl_multi批量傳送http請求PHPHTTP
- java傳送GET和post請求Java
- 理解ASP.NET Core - 傳送Http請求(HttpClient)ASP.NETHTTPclient
- wireshark抓包curl傳送http2請求HTTP
- 使用 request 和 cheerio 庫來傳送 HTTP 請求HTTP
- oracle使用utl_http包傳送post請求OracleHTTP
- java傳送post請求 ,請求資料放到body裡Java
- 傳送 options 請求 後端返回 405 的解決過程後端
- Java用HttpClient3傳送http/https協議get/post請求,傳送map,jsoJavaHTTPclient協議JS
- java傳送get請求帶引數Java
- Jmeter —— jmeter利用取樣器中http傳送請求JMeterHTTP
- 如何使用 Postman 傳送 SAP OData Batch 請求到 ABAP 後臺伺服器試讀版PostmanBAT伺服器
- 傳入http請求的UserAgent 根據它判斷是手機還是電腦傳送過來的請求HTTP
- Postman傳送Post請求Postman
- 傳送GET請求 示例
- HttpClient--傳送請求HTTPclient
- Vue中通過Axios向SpringBoot傳送get和post請求VueiOSSpring Boot