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. python Aspscheduler 定时任务框架使用

    前几日,爬虫基本能爬点东西出来了,现在需要实现定时把数据爬到DB里去,可以使用windows定时任务执行py脚本,但好像不彻底,要做一个纯(jiao)粹(qing)的程序员,定时任务的重任落到了Asp ...

  2. springMVC学习(1)

    spring mvc的位置: springMVC只是spring的一个模块:   第一步:发起请求到前端控制器(DispatcherServlet) 第二步:DispatcherServlet请求Ha ...

  3. 前端input选中状态时的蓝框

    input标签在选中状态时,我们的浏览器不同时,会有不同效果. 一般我们chrome时可能会没有这个效果,但是如果是safari时,这个效果就... 这时为了达到效果统一,我们不得不去掉这个蓝框,就要 ...

  4. asp.net 分页类

    PaginatedList.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; n ...

  5. pptv web前端面试题答案

    这是星期一考完试,答应星期三补上的,代码很简单,就不写注释了 //php快排 function quickSort(&$arr){   $arr_left=new array();   $ar ...

  6. C语言 对数组名取地址

    作者 : 卿笃军 你有没有想过,对一个一维数组名取地址,然后用这个地址进行加减运算.这会出现什么样的结果呢? 演示样例: int a[5] = {1,2,3,4,5}; int *p = (int * ...

  7. Hibernate介绍

    1.什么是Hibernate? 首先,Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多比如:iBATIS,myBatis,Nhibernate,Siena等等. 并且Hibern ...

  8. android屏蔽状态栏显示

    framework/base/packages/SystemUI/res/layout/status_bar_expanded.xml 要屏蔽哪个设置哪个属性为: android:visibility ...

  9. 【转】MVP和MVC的区别

    转自:http://www.cnblogs.com/end/archive/2011/06/02/2068512.html MVC和MVP到底有什么区别呢? 从这幅图可以看到,我们可以看到在MVC里, ...

  10. Android(工具・集成)

    ■ NDK Android从始(2009)至终是支持JNI的. 只不过一开始没有NDK.需要自己安装交叉编译器创建so,然后放到assert之类捆绑so发布. 有了NDK.有了什么? libc lib ...