首页 > 其他分享 >idhttp get 与 post 方法

idhttp get 与 post 方法

时间:2023-07-26 18:14:43浏览次数:29  
标签:end string get url idhttp requestList using post responsestream

idHttp 两种传输数据的方法,即get 和 post 总结

*服务端用c# 模拟 WebApi

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace webapitest1.Controllers
{
    public class ProductController : ApiController
    {
        public string Get()
        {
            return "调用Get 无参方法";
        }
        [HttpGet]
        public string Get([FromUri]string msg) {
            return $"调用Get有参方法:{msg}";
        }
        [HttpPost]
        public string Post(dynamic obj) {
            return $"调用post方法,传入参数:{obj.name},{obj.age}";
        }
    }
}

  • 客户端用Delphi 测试(Delphi中默认的字符编码为Ansi,而网络中基本上使用的都是utf8,因此注意转码
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    IdHTTP1: TIdHTTP;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  url:string;
  responsestream:TStringStream;
  ResponseStr:string;
  requestList:TStringList;
  RequestStream:TStringStream;
  jsonStr:string;
begin
  responsestream:=TStringStream.Create('');
  RequestStream:=TStringStream.Create('');
  requestList:=TStringList.Create;
  url:='http://localhost:55867/api/product';
  requestList.Add('name=张三');
  requestList.Add('age=22');
  try
   //IdHTTP1.Post(url,requestList,responsestream); //列表方式提交
   //responsestream.Position:=0;
   RequestStream.WriteString(AnsiToUtf8('{"name":"张三","age":22}'));//这里用转码是为了传输带有中文的参数
   IdHTTP1.Request.ContentType:='application/json';  //指定传输类型为json,主要为了方便服务端dynamic 动态推断类型,即可以把json字符串自动转换成object 对象
   IdHTTP1.Post(url,RequestStream,responsestream); //流方式提交
   responsestream.Position:=0;
   Memo1.Lines.Add(StringReplace(Utf8ToAnsi(responsestream.DataString),'"','',[rfReplaceAll])); //这里去除了双引号 "
   responsestream.Free;
   RequestStream.Free;
   requestList.Free;
  except on e:Exception do
  begin
    ShowMessage(e.Message);
  end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  stream:TStringStream;
  url:string;
begin
  url:='http://localhost:55867/api/product';
  try
    stream:=TStringStream.Create('');
    IdHTTP1.Get(url,stream);
    stream.Position:=0;
    Memo1.Lines.Add(Utf8ToAnsi(stream.DataString));
  finally
    stream.Free;
  end;
end;

end.

image

标签:end,string,get,url,idhttp,requestList,using,post,responsestream
From: https://www.cnblogs.com/sundh1981/p/17583220.html

相关文章

  • 国产化的接口测试、接口自动化测试工具Apipost的介绍及使用
    Apipost介绍:Apipost是API文档、API调试、APIMock、API自动化测试一体化的研发协作赋能平台,它的定位Postman+Swagger+Mock+JMeter。Apipost是接口管理、开发、测试全流程集成工具,能支撑整个研发技术团队同平台工作,主要使用者为前端开发、后端开发、测试人员。Apipo......
  • poj 2886 Who Gets the Most Candies? (线段树单点更新应用)
                           poj2886WhoGetstheMostCandies?DescriptionNchildrenaresittinginacircletoplayagame.Thechildrenarenumberedfrom1toNinclockwiseorder.Eachofthemhasacardwithanon-zerointegeronit......
  • GET chrome-extension://invalid/ net::ERR_FAILED是什么错误
    GETchrome-extension://invalid/net::ERR_FAILED是什么错误错误信息"GETchrome-extension://invalid/net::ERR_FAILED"通常表示在Chrome浏览器中发生了一个资源加载失败的问题。该错误信息表明浏览器尝试获取一个Chrome扩展(chrome-extension)中的资源,但该资源的URL地址是无效......
  • postman 常用脚本
    1.登录成功之后,在header中获取cookie,并设置成环境变量//获取cookie值var jsondata = postman.getResponseHeader("set-cookie");console.log(jsondata)//设置成环境变量data = jsondata.split(";")[0];data = data.split("=")console.log(data)postman.setEnviro......
  • ubuntu包管理命令apt-get-apt和dpkg的用法n
    ubuntu包管理命令apt-get/apt和dpkg的用法1.apt-get命令:apt-get是debian,ubuntu发行版的包管理工具,与红帽中的yum工具非常类似,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索、安装、升级、卸载软件或操作系统。apt-get在安装包的时候是根据/etc/apt/sources.li......
  • 为什么 PostgreSQL 的适用性很强?
    说起使用数量最大的数据库SQLite它是全球最广泛部署的数据库引擎。它存在于你的手机中,存在于你的浏览器中,如果你搜索你的电脑,你也会在其中找到它的.db文件。SQLite受到Postgres的启发。其作者RichardHipp称SQLite是Postgres的“概念分支”。两者没有共享代码,但是Po......
  • python的request.data.get()
    Python中的request.data.get()实现步骤在Python中,我们可以使用request.data.get()来获取请求的数据。它是一种用于获取POST请求数据的方法。下面是实现request.data.get()的步骤:步骤描述1导入必要的库2创建一个POST请求3获取请求数据现在让我们一步一步地......
  • PG-DBA培训07:PostgreSQL体系结构深入与源码解析 原创
    PostgreSQL体系结构深入解析,PostgreSQL数据库源码解析,initdb源码解析PostgreSQL数据库体系架构PostgreSQL数据库存储结构PostgreSQL数据库进程结构PostgreSQL数据库内存结构PostgreSQL数据库源码解析使用gdb跟踪分析PostgreSQL源码PostgreSQL源码解析之initdb初始化过程Postgre......
  • PG-DBA培训09:PostgreSQL用户权限与安全管理
    PostgreSQL数据库用户角色管理,访问控制管理,ssl访问,密码策略,审计管理,等保评测PostgreSQL数据库用户角色管理PostgreSQL访问控制管理PostgreSQLSSL安全访问PostgreSQL密码策略管理PostgreSQL审计管理PostgreSQL等保评测安全建议方案课程地址: https://edu.51cto.com/course/3......
  • PG-DBA培训08:PostgreSQL实例管理与参数文件
    PostgreSQL管理工具,参数文件源码分析,控制文件损坏恢复,系统表索引损坏,插件开发PostgreSQLPSQL管理工具使用PostgreSQL数据库参数文件PostgreSQL如何读取参数文件(源码)PostgreSQL数据库控制文件PostgreSQL控制文件损坏恢复案例PostgreSQL数据库日志文件PostgreSQL系统表与系......