如何用DELPHI 发送HTTP请求???(100分)

  • 主题发起人 主题发起人 Chinester
  • 开始时间 开始时间
C

Chinester

Unregistered / Unconfirmed
GUEST, unregistred user!
或许这样说不太对.我的意思就是.
如何在一个如DISCUZ!的论坛 用DELPHI 发贴?

偶知道一个CURL 可以实现,不过CURL已经是一个命令行的程序了..

相关的CURLPAS 没有资料.... 不知道如何用.

有别的控件可以实现吗?

高分求解,
 
你是要做广告暴力发布程序吧……非常厌恶这个。
so,知道也不告诉你。
 
////////////////////////
// http get component //
// program designer //
// wangchangyu //
// 2004-06 //
////////////////////////
unit HttpGet;

interface

uses
Windows, SysUtils, WinInet;

type
TSaveType=(stFile,stString);

type
THTTPGet = class
private
FAcceptTypes: String;
FAgent: String;
FBinaryData: Boolean;
FURL: String;
Ffilesize:Integer;
FUseCache: Boolean;
FFileName: String;
FUserName: String;
FPassword: String;
FPostQuery: String;
FReferer: String;
fStringResult:String;
FResult: Boolean;
fSaveType:TSaveType;
BytesToRead, BytesReaded: DWord;
Function Exectue:Boolean;
protected
{}
public
constructor Create;
//destructor Destroy;
Function GetFile(var aFileName:String; var aFileSize:Cardinal):Boolean;
Function GetString(var aResult:String):Boolean;
published
property AcceptTypes: String read FAcceptTypes write FAcceptTypes;
property Agent: String read FAgent write FAgent;
property BinaryData: Boolean read FBinaryData write FBinaryData default false;
property URL: String read FURL write FURL;
property UseCache: Boolean read FUseCache write FUseCache default false;
property FileName: String read FFileName write FFileName;
property UserName: String read FUserName write FUserName;
property Password: String read FPassword write FPassword;
property PostQuery: String read FPostQuery write FPostQuery;
property Referer: String read FReferer write FReferer;
end;

implementation

constructor THttpGet.Create;
begin
FAcceptTypes := '*/*';
FAgent := 'zzutrain Soft HTTPGet';
fBinaryData:=False;
fUseCache:=False;
fSaveType:=stString;
end;

{destructor THttpGet.Destroy;
begin

end;
}

Function THttpGet.Exectue:Boolean;
var
hSession, hConnect, hRequest: hInternet;
HostName, FileName: String;
f: File;
Buf: Pointer;
dwBufLen, dwIndex: DWord;
Data: Array[0..$400] of Char;
TempStr: String;
RequestMethod: PChar;
InternetFlag: DWord;
AcceptType: LPStr;
TcpPort:Word;
procedure ParseURL(URL: String; var HostName, FileName: String);
var
i: Integer;
procedure ReplaceChar(c1, c2: Char; var St: String);
var
p: Integer;
begin
while True do
begin
p := Pos(c1, St);
if p = 0 then Break
else St[p] := c2;
end;
end;
begin
if Pos('http://', LowerCase(URL)) <> 0 then System.Delete(URL, 1, 7);
i := Pos('/', URL);
HostName := Copy(URL, 1, i-1);
TcpPort:=Pos(':',HostName);
if TcpPort>0 then begin
FileName:=Copy(HostName,1,TcpPort-1);
Delete(HostName,1,TcpPort);
TcpPort:=StrToInt(HostName);
HostName:=FileName;
end else TcpPort:=INTERNET_DEFAULT_HTTP_PORT;
FileName := Copy(URL, i, Length(URL) - i + 1);
if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
SetLength(HostName, Length(HostName) - 1);
end;
procedure CloseHandles;
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
end;
begin
try
ParseURL(FURL, HostName, FileName);
if FAgent <> '' then
hSession := InternetOpen(PChar(FAgent),INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0)
else
hSession := InternetOpen(nil,INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hSession, PChar(HostName),TcpPort, PChar(FUserName), PChar(FPassword), INTERNET_SERVICE_HTTP, 0, 0);
if FPostQuery = '' then RequestMethod := 'GET' else RequestMethod := 'POST';
if FUseCache then InternetFlag := 0 else InternetFlag := INTERNET_FLAG_RELOAD;
AcceptType := PChar('Accept: ' + FAcceptTypes);
hRequest := HttpOpenRequest(hConnect, RequestMethod, PChar(FileName), 'HTTP/1.0',PChar(FReferer), @AcceptType, InternetFlag, 0);
if FPostQuery = '' then
HttpSendRequest(hRequest, nil, 0, nil, 0)
else
HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded', 47,PChar(FPostQuery), Length(FPostQuery));
dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);
FResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, Buf, dwBufLen, dwIndex);
if FResult or not FBinaryData then begin
if FResult then FFileSize := StrToInt(StrPas(Buf));
BytesReaded := 0;
Case fSaveType of
stFile: begin
AssignFile( f, FFileName);
Rewrite(f, 1);
end
else FStringResult := '';
end;
while True do begin
if not InternetReadFile(hRequest, @Data, SizeOf(Data), BytesToRead) then Break
else
if BytesToRead = 0 then Break else begin
if fSaveType=stfile then BlockWrite(f, Data, BytesToRead) else begin
TempStr := Data;
SetLength(TempStr, BytesToRead);
FStringResult := FStringResult + TempStr;
end;
inc(BytesReaded, BytesToRead);
//if Assigned(FProgress) then UpdateProgress;
end;
end;
if fSavetype=stFile then
FResult := FFileSize = Integer(BytesReaded)
else begin
SetLength(FStringResult, BytesReaded);
FResult := BytesReaded <> 0;
end;
if FSaveType=stFile then CloseFile(f);
end;
FreeMem(Buf);
CloseHandles;
Result:=FResult;
except
Result:=False;
end;
end;

Function THttpGet.GetFile(var aFileName:String; var aFileSize:Cardinal):Boolean;
begin
fSaveType:=stFile;
fBinaryData:=True;
Result:=Exectue;
aFileName:=fFileName;
aFileSize:=fFileSize;
end;

Function THttpGet.GetString(var aResult:String):Boolean;
begin
fSaveType:=stString;
fBinaryData:=False;
Result:=Exectue;
aResult:=fStringResult;
end;


end.
 
其实2楼的冤枉我了.[:(][:(][:(]

我只是想发贴快点.

因为我玩的一个游戏是电信线路....而我是网通....

正在测试3楼的.... OK了就回来.
 
[:(] 搞了好久.... 行不通...

能给个使用的例子吗?....
 
用IDHTTP控件就可以了呀.

POST
 
Http.URL:=HttpUrl+GateWay.GetWayName+eUrl;
if Http.GetString(rs) then begin
CheckReciveString(rs);
if rs[1]='1' then begin
Delete(rs,1,1);
ListVar.Text:=rs;
Result:=True;
end;
end else Errmsg:=rs;
 
哎,给你控件都不会用,代码也不长。
var //这个例子是http Get 方法
http: Thttpget
rs:string;
begin
http:=thttpget.create;
Http.URL:=HttpUrl+GateWay.GetWayName+eUrl; //http地址
if Http.GetString(rs) then showmessage(rs); //获取数据 并显示
http.free;

end;



var //http Post 方法 此例是 向Getway 发送数据 把返回的结果页面显示到WebBrowser1
http:Thttpget;
rs:String;
vv: Variant;
HTMLDocument: IHTMLDocument2;

var
Flags: OLEVariant;
begin
WebBrowser1.Navigate('about:blank');
// WebBrowser1.Navigate(WideString(HtmlInfo.EleStr), Flags, Flags, Flags, Flags);
http:=Thttpget.Create; //创建
try
http.URL:=HtmlInfo.HtmlUrl; //url 为http地址 如: http://aa/getcode.asp
http.PostQuery:=HtmlInfo.EleStr; //为表单元素 S1=文本区&T1=单行文本{//HtmlInfo.EleStr:=HtmlInfo.HtmlEleName+'='+StringGrid1.Cells[ChkType.ArrayLen+ChkType.ViewFieldLen+1,StringGrid1.Row];}
if http.GetString(rs) then begin
vv := VarArrayCreate([0,0],varVariant);
vv[0] := rs;
HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
HTMLDocument.write(PSafeArray(TVarData(vv).VArray));
HTMLDocument.Close;
end;
finally
http.free;
end;
end;
 
zzutrain,谢谢你. 因为 上传附件时要选择本地文件.请问如何写POSE?
写成:att=c:/1.bmp
还是att=@c:/1.bmp

偶真的有点苯.[:(]
 
后退
顶部