Delphi7下实现HTTP的Post操作

unit Unit1;

interface

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

type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function httpPost(postUrl:string;Params:TStrings):string;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
url : string;
Params: TStrings;
begin
Params := TStringList.Create;
Params.Add('username=kenter1983');
Params.Add('password=111111');
url := 'http://www.cnlive.com/index/?action=login';
ShowMessage(httpPost(url,Params));

end;

function TForm1.httpPost(postUrl:string;Params:TStrings):string;
var
idhtp1: TIdHTTP;
begin
idhtp1:= TidHTTp.create(self);
idhtp1.AllowCookies:=True;
idhtp1.HTTPOptions:=[hoForceEncodeParams];
idhtp1.ProtocolVersion:=pv1_1;
idhtp1.Request.ContentType:='application/x-www-form-urlencoded';
idhtp1.Request.CacheControl:='no-cache';
idhtp1.Request.UserAgent:='User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1';
idhtp1.Request.Accept:='Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
idhtp1.Request.AcceptEncoding:='Accept-Encoding=gzip,deflate';
idhtp1.Request.AcceptCharSet:='Accept-Charset=gb2312,utf-8;q=0.7,*;q=0.7';
idhtp1.Request.Connection:='Connection=keep-alive';
try
result := idhtp1.Post(postUrl,Params);
except
Result := 'error';
end;
end;

end.

文件流

Const
CRLF = #13#10;
var
s,s1,filename:String;
response:TStringStream;
source,source1:TMemoryStream;
Url:string;
i,cnt:integer;
begin
idhttp.Request.ContentType:='multipart/form-data';
Response := TStringStream.Create('');
url:='Http://'+host+dir;
cnt:=files.Count;
if (not dead) then
begin
for i:=0 to cnt-1 do
begin
filename:=files[i];
if fileexists(filename) then
begin
try
S := '-----------------------------7cf1d6c47c' + CRLF +
'Content-Disposition: form-data; name="file1"; filename="'+filename+'"'+CRLF +
'Content-Type: application/octet-stream' + CRLF + CRLF;
//上传文件内容
s1:='file one content. Contant-Type can be application/octet-stream or if'+
'you want you can ask your OS fot the exact type.' + CRLF +
'-----------------------------7cf1d6c47c' + CRLF + //分界符,用于分隔表单(Form)中的各个域
'Content-Disposition: form-data; name="text1"' + CRLF + CRLF +
'hello2' + CRLF +
'-----------------------------7cf1d6c47c--';
//提交的下一个表单内容域的内容
s1:=CRLF +'-----------------------------7cf1d6c47c' + CRLF +
'Content-Disposition: form-data; name="text1"' + CRLF + CRLF +
'hello2' + CRLF +
'-----------------------------7cf1d6c47c--';
Source := TMemoryStream.Create;
Source1 := TMemoryStream.Create;
Source1.LoadFromFile(filename);
Response:=TStringStream.Create('') ;
Response.CopyFrom(source1,source1.Size);
s:=s+Response.DataString;//因为只能传字符串
Source.Position :=0;
Source.Write(s[1],length(s));
Source.Position :=source.Size ;
Source.Write(s1[1],length(s1));
Response.Position :=0;
try
idHTTP.Post(url, Source, Response);
finally
if not uploadsuccess(Response.DataString) then
begin
dead:=true;
self.idhttp.Disconnect;
end;
Source.Free;
Response.Free;
end;

Delphi7下实现HTTP的Post操作 转的更多相关文章

  1. centos 6x系统下源码安装mysql操作记录

    在运维工作中经常部署各种运维环境,涉及mysql数据库的安装也是时常需要的.mysql数据库安装可以选择yum在线安装,但是这种安装的mysql一般是系统自带的,版本方面可能跟需求不太匹配.可以通过源 ...

  2. 可能 delphi7 下稳定的最后一版本 GDIPLUS

    可能 delphi7 下稳定的最后一版本 GDIPLUS 万一的 blog 说"终于, Delphi XE2 携带 GDI+ 库了 使用了较早的 http://www.progdigy.co ...

  3. 截图技巧——鼠标按着不懂,同时点击截图快捷键(QQ为ctrl+alt+a)即可避免按下快捷键鼠标按下等操作消失截不到的尴尬。

    截图技巧--鼠标按着不懂,同时点击截图快捷键(QQ为ctrl+alt+a)即可避免按下快捷键鼠标按下等操作消失截不到的尴尬.

  4. linux下MySQL安装登录及操作

    linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-cl ...

  5. vsphere 出现“在主机的当前连接状况下不允许执行该操作”

    问题: 登录vsphere client启动虚拟机出现“在主机的当前连接状况下不允许执行该操作” 解决方法: 1.选择主机->右键->断开 2.选择主机->右键->连接 3.在 ...

  6. 在windows下配置对github的操作--基本操作

    一.下载安装 git for widows软件 git for widows 是专门用来在windows下操作 github的软件,提供bash(命令行) 和 gui两种方式. 在bash下,其实就是 ...

  7. cmd 下登陆ftp及相关操作

    cmd 下登陆ftp及相关操作 2011-08-09 20:34:28|  分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应 ...

  8. Java CAS同步机制 原理详解(为什么并发环境下的COUNT自增操作不安全): Atomic原子类底层用的不是传统意义的锁机制,而是无锁化的CAS机制,通过CAS机制保证多线程修改一个数值的安全性。

    精彩理解:  https://www.jianshu.com/p/21be831e851e ;  https://blog.csdn.net/heyutao007/article/details/19 ...

  9. .Net 环境下比较各种数据库插入操作的性能

    1.简介 再说Windows的异步I/O操作前,先聊聊一些题外话,能帮助我们更好的理解异步I/O操作,常规的Web程序,当用户发起一次请求,当请求通过管道到达客户端的这个过程,会唤起一个线程池线程(后 ...

随机推荐

  1. OI生涯中三届NOIP(2012-2014)流水账

    NOIP2012: 才摸了三四个月OI就上阵当炮灰,果然一下就被轰得渣都不剩了. D1看到T1这道模拟水题时很激动,立马就把它A了.然后T2就不会了,果断写了个阶乘的暴力,根本没有想过什么排序贪心.T ...

  2. javascript表单验证-邮箱验证

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. delphi算法

    /  求余 mod 取模 var a1,a2,a3 : Integer; b1,b2,b3 : Integer; c1,c2 : Integer;begin a1 := 987; //ShowMess ...

  4. lesson5:Condition的原理分析及demo展示

    Condition 将 Object 监视器方法(wait,notify,和notifyAll)分解成截然不同的对象,以便通过将这些对象与任意Lock实现组合使用,为每个对象提供多个等待 set(wa ...

  5. # MongoDB学习笔记(持续更新)

    启动mongo服务 sodo mongo 显示数据库(显示数据库名称和大小,单位GB) > show dbs admin (empty) local 0.078GB test 0.078GB t ...

  6. Android 中使用 html 作布局文件

    在Android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...

  7. 类型转换操作符static_cast、const_cast、dynamic_cast、reinterpret_cast

    一.static_cast 对于类型转换,我们常常这么做: (type) expression 引进了static_cast类型转换操作符后,我们只需这样做: static_cast<type& ...

  8. sqlserver 2008 sa登陆的一些问题

    sqlserver 2008 sa登陆 无法连接到(local)? 遇到这个问题请确保SQL主服务是开启状态: ok接下来把服务器名换成 计算机名\实例名  再次使用sa登陆,如下: 是不是可以了呢? ...

  9. mysql求时间差

    SELECT TIMESTAMPDIFF(SECOND, now(), "2012-11-11 00:00:00") 语法为:TIMESTAMPDIFF(unit,datetime ...

  10. 把Nginx加入系统服务 service nginx (start | stop | restart | reload)

    vim /etc/init.d/nginx 1 #!/bin/bash  2 # nginx Startup script for the Nginx HTTP Server  3 # it is v ...