delphi call java http server

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    IdHTTP1: TIdHTTP;
    mmo1: TMemo;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  End ; 

var 
  the Form1: TForm1; 

Implementation 

{ $ R & lt *. Dfm } 

Procedure TForm1.btn1Click (Sender: TObject);
 var 
  the Url: String ; // request address 
  ParamList: a TStringList; // Parameter request list 
  the ResponseStream: TStringStream; // Returns information 
  ResponseStr: String ;
 the begin 
  ParamList: . = a TStringList the Create ; 
  ParamList.Add ( ' User linlf = ' ); 
  ParamList.Add ( ' pwd = XXXX '  );
  the ResponseStream: = TStringStream. The Create ( '' );
   the try 
    // request address 
    the Url: = ' 8080 / delphiJava / ServletDelphi: HTTP: // localhost ' ;
     the try 
    ; IdHTTP1.Post (the Url, ParamList, the ResponseStream) 
    //       IdHTTP1.Get (the Url, the ResponseStream); 
    the except 
      ON E: Exception do 
      the begin 
        the ShowMessage (e.Message); 
      End ;
     End ;
     // get information on the page is returned 
    ResponseStr: = ResponseStream.DataString;
     // presence Chinese page, the need for UTF8 decoding 
    ResponseStr: =UTF8Decode (ResponseStr);
    mmo1.Text: =    ResponseStr; 

  the finally 

    // IdHTTP1.Free; 
//     ResponseStream.Free; 
  End ;
 End ; 

Procedure TForm1.btn2Click (Sender: TObject);
 var 
  the Url: String ; // address request 
  the ResponseStream: TStringStream; // Returns information 
  ResponseStr: String ;
 the begin 
   the ResponseStream: = TStringStream. the Create ( '' );
   the try 
    // request address 
    the Url: = ' HTTP: // localhost: 8080 / delphiJava / ServletDelphi' ;
     The try 
       IdHTTP1.Get (the Url, the ResponseStream); 
    the except 
      ON E: Exception do 
      the begin 
        the ShowMessage (e.Message); 
      End ;
     End ;
     // get information on the page is returned 
    ResponseStr: = ResponseStream.DataString;
     // page is present when Chinese, required UTF8 decoding 
    ResponseStr: = UTF8Decode (ResponseStr); 
    mmo1.Text: =    ResponseStr;
   the finally 


    // IdHTTP1.Free; 
//     ResponseStream.Free; 
  End ;
 End ; 


End .



<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    delphiJava</display-name>
    <servlet>
        <description>
        </description>
        <display-name>
        ServletDelphi</display-name>
        <servlet-name>ServletDelphi</servlet-name>
        <servlet-class>
        ServletDelphi</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletDelphi</servlet-name>
        <url-pattern>/ServletDelphi</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

 

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("Hellox Servletx, " + request.getParameter("user")
        +";"+request.getParameter("pwd")+ "!");
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}

 

Guess you like

Origin www.cnblogs.com/tobetterlife/p/12171430.html