zoukankan      html  css  js  c++  java
  • delphi之http通讯

    最近写接口对接,需要建立通讯。现将delphi用到的通讯列出:

    HTTP

     发送post请求

    需要用到TIdHttp控件

    procedure TFrmMain.Button5Click(Sender: TObject);
    var
      HttpClient: TIdHttp;
      ParamList: TStringList;
    begin
      HttpClient := TIdHttp.Create();
      ParamList := TStringList.Create;
      ParamList.Add('code=a123');
      ShowMessage(HttpClient.Post('http://127.0.0.1:9000/status', ParamList));
    end;

    路由如下:

    POST    /status  CodeApplication.find

    java代码如下:

    public static void find(String code){
            //处理action逻辑。
            renderText(findSchedulestatus);
        }

    现在写个复杂一点的,发送参数name=abc,然后接受服务端处理后返回的值。

    delphi客户端:

    procedure TFrmMain.PostDemo();
      var
        IdHttp : TIdHTTP;
        Url : string;//请求地址
        ResponseStream : TStringStream; //返回信息
        ResponseStr : string;
    
        RequestList : TStringList;     //请求信息
        RequestStream : TStringStream;
     begin
       //创建IDHTTP控件
       IdHttp := TIdHTTP.Create(nil);
    
       //TStringStream对象用于保存响应信息
       ResponseStream := TStringStream.Create('');
    
      // RequestStream := TStringStream.Create('');
       RequestList := TStringList.Create;
       try
         Url := 'http://127.0.0.1:9000/a';
         try
           //以列表的方式提交参数
           RequestList.Add('name=abc');
           IdHttp.Post(Url,RequestList,ResponseStream);
           //以流的方式提交参数
          // RequestStream.WriteString('name=love');
          // IdHttp.Post(Url,RequestStream,ResponseStream);
         except
           on e : Exception do
           begin
             ShowMessage(e.Message);
           end;
         end;
         //获取网页返回的信息
         ResponseStr := ResponseStream.DataString;
         //网页中的存在中文时,需要进行UTF8解码
         ResponseStr := UTF8Decode(ResponseStr);
         ShowMessage(ResponseStr);
       finally
         IdHttp.Free;
         RequestList.Free;
         RequestStream.Free;
         ResponseStream.Free;
       end;
     end;

    java服务端:

    import play.*;
    import play.mvc.*;
    
    import java.util.*;
    
    import models.*;
    
    public class Application extends Controller {
        
        private String name;
    
        public static void indexpost(String name){
            System.out.println("dosomething"+name);
            renderText("hello client!"+name);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }

    服务端routes文件:

    # Routes
    # This file defines all application routes (Higher priority routes first)
    # ~~~~
    
    # Home page
    GET     /                                       Application.index
    POST    /a                                      Application.indexpost
    
    # Ignore favicon requests
    GET     /favicon.ico                            404
    
    # Map static resources from the /app/public folder to the /public path
    GET     /public/                                staticDir:public
    
    # Catch all
    *       /{controller}/{action}                  {controller}.{action}

    HTTP

     发送GET请求

    procedure TFrmMain.GetDemo();
    
      var
        IdHttp : TIdHTTP;
        Url : string;//请求地址
        ResponseStream : TStringStream; //返回信息
        ResponseStr : string;
      begin
        //创建IDHTTP控件
        IdHttp := TIdHTTP.Create(nil);
       //TStringStream对象用于保存响应信息
      ResponseStream := TStringStream.Create('');
      try
         //请求地址
         Url := 'http://127.0.0.1:9000';
         try
           IdHttp.Get(Url,ResponseStream);
         except
           on e : Exception do
           begin
             ShowMessage(e.Message);
           end;
         end;
         //获取网页返回的信息
         ResponseStr := ResponseStream.DataString;
         //网页中的存在中文时,需要进行UTF8解码
         ResponseStr := UTF8Decode(ResponseStr);
       finally
         IdHttp.Free;
         ResponseStream.Free;
       end;
     end;
  • 相关阅读:
    ngInclude与script加载模板
    ng-model-options
    angular模板加载 ----ng-template
    ngBind ngBindTemplate ngBindHtml
    ng-switch
    ng-show与ng-if区别
    运维教给我什么
    路漫漫其修远兮,吾将上下而求索
    那些让我们继续坚持下去句子
    随手记
  • 原文地址:https://www.cnblogs.com/feiyunaima/p/6297130.html
Copyright © 2011-2022 走看看