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. adjtimex使用

    adjtimex使用 今天遇到一个ntp的同步问题.服务器上配置好了ntpd,在启动前也手动进行过同步,但是过段时间ntpq查询发现服务器即便能选出同步服务器,但是系统的时间偏差越来越大. 服务器上实 ...

  2. Linux + Apache + MySql+ Php 配置虚拟主机

    win7:------------------------------------------------------------------------ NameVirtualHost *:80&l ...

  3. C#运行时鼠标移动控件 - 调用Windows API(ReleaseCapture)

    [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool SendMes ...

  4. Apache+Subversion+TortoiseSVN

    Key words: dav_svn, apache, subversion, tortoisesvn # install apache2 sudo apt-get install libapache ...

  5. source insight3.5中文乱码解决方案

    source insight3.5中文乱码,网上看别人说改变宽字体.宋体等方法都不起效.根本原因是,source insight 3.5 不支持Unicode编码,所以导致中文的乱码,将文件转为gb2 ...

  6. 网页JavaScript1

    DOM的操作 windows对象操作 属性: opener,打开当前窗口的源窗口,首次启动 是null. dialogArgument,对话框的返回值 子对象: history , location ...

  7. html.css溢出

    <!DOCTYPE html><!DOCTYPE html><html><head> <title></title> <m ...

  8. 生成getter()、setter()方法去掉变量前缀

    当定义的变量名有前缀但是不想在生成它的getter()和setter方法的时候让前缀出现,比如今天项目的部分代码: public class Crime { private UUID mId; //标 ...

  9. Android-操作栏之副标题

    我们的目标是在操作栏右侧加上一个选项菜单,点击它就可显示或者隐藏操作栏的副标题. 由于操作栏是在API11级以后出现的,因此必须考虑兼容性问题.我们直接让低于API11的设备根本看不到选项菜单即可.建 ...

  10. java下tcp的socket连接

    serverDemo package cn.stat.p4.ipdemo; import java.io.IOException; import java.io.InputStream; import ...