代码改变世界

delphi之http通讯

2017-01-18 16:16  绿贝  阅读(1047)  评论(0编辑  收藏  举报

最近写接口对接,需要建立通讯。现将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;