往INTERNET上写文件的方法?哪位高手指点一下(50分)

  • 主题发起人 主题发起人 lanlyli
  • 开始时间 开始时间
L

lanlyli

Unregistered / Unconfirmed
GUEST, unregistred user!
我用以下代码,但是不行,不知道是怎么回事.


var
httpContext: Integer;
HttpServer, UserID, Passwd: string;
BufferIn: INTERNET_BUFFERS;

RemoteFile: string;
LocalFile: string;
httpFile: THandle;

readInF: DWORD;
oBytesWritten: DWORD;

rRead: BOOL;
iPort:integer;

Count: DWORD;

HttpNet, HttpConnect, HttpRequest: HINTERNET;
pBuffer: string;
begin
LocalFile := 'c:/prodcode.txt';
RemoteFile := '/aaa.txt';
HttpServer := 'www.qw111.com';
UserID := 'qw111.com';
Passwd := 'ys68nurw';
iPort := 128;
HttpNet := InternetOpen(PChar(HttpServer), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
httpContext := 0;
HttpConnect := InternetConnect(HttpNet, PChar(HttpServer), INTERNET_DEFAULT_HTTP_PORT, PChar(UserID),
PChar(Passwd), INTERNET_SERVICE_HTTP, 0, httpContext);
if HttpConnect=nil then
ShowMessage('ftp 连接失败.')
else
ShowMessage('ftp 连接成功.');


//设定服务端目录
FtpSetCurrentDirectory(HttpConnect,'/update');
if FileExists(LocalFile) then
begin
if FtpPutFile(HttpConnect,pchar(LocalFile),'test.txt',INTERNET_FLAG_TRANSFER_BINARY,0) then
ShowMessage('发送完毕')
else
ShowMessage('发送失败');
end
else
ShowMessage('要发送的文件不存在');

InternetCloseHandle(HttpConnect);
 
以下是我的代码,整个过程都没有出错,但是在服务器上就是找不到A.TXT

[:(][:(][:(]


procedure WriteWebFile;
var
httpContext: Integer;
HttpServer, UserID, Passwd: string;
BufferIn: INTERNET_BUFFERS;

RemoteFile: string;
LocalFile: string;
hFile: THandle;

readInF: DWORD;
oBytesWritten: DWORD;

rRead: BOOL;
iPort:integer;

Count: DWORD;

hNet, hConnect, hRequest: HINTERNET;
pBuffer: string;
begin
LocalFile := 'c:/prodcode.txt';
RemoteFile := '/a.txt'; //Update/tax/
HttpServer := 'www.qwwww.com';
UserID := 'wwwwom';
Passwd := 'ywwwwnurw';
iPort := 218;

try
hNet := InternetOpen('WebWriteFile_xsq_Test', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if hNet = nil then exit;
httpContext := 0;
hConnect := InternetConnect(hNet, PChar(HttpServer), iPort, PChar(UserID),
PChar(Passwd), INTERNET_SERVICE_HTTP, 0, httpContext);
if hConnect=nil then
begin
ShowMessage(' 连接失败.');
exit;
end;

hRequest := HttpOpenRequest(hConnect, 'PUT', PChar(RemoteFile),
// nil, nil, nil, 0, 0); //'GET'
nil, nil, nil, 0, 0); //'GET'
if hRequest = NIL then
begin
ShowMessage('打开失败.');
exit;
end;


hFile := CreateFile(PChar(LocalFile), GENERIC_READ, FILE_SHARE_READ,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

ReadInF := 0;

FillChar(BufferIn, SizeOf(BufferIn), 0);
BufferIn.dwStructSize := SizeOf(BufferIn);
BufferIn.dwBufferLength := cBuffsize;
BufferIn.dwBufferTotal := GetFileSize(hFile, nil);
//HttpSendRequestEx(hRequest, @BufferIn, nil, HSR_INITIATE, 0);
if( not HttpSendRequestEx( hRequest, @BufferIn, nil, HSR_INITIATE, 0)) then
begin
ShowMessage(' 请求失败');
exit;
end;

repeat
SetLength(pBuffer, cBuffsize);
ReadFile(hFile, Pointer(pBuffer)^, cBuffsize, Count, nil);
ReadInF := ReadInF + Count;
rRead := InternetWriteFile(hRequest, Pointer(pBuffer), Count, oBytesWritten);
if not rRead then
begin
ShowMessage(' WRITE失败');
exit;
end;
until ReadInF = BufferIn.dwBufferTotal;
finally
CloseHandle(hFile);
if not HttpEndRequest(hRequest, nil, 0, 0) then
begin
ShowMessage(' Close失败');
end;
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hNet);
end;
end;
 
需要服务器端有相应的程序配合。
否则没权限。
要是可以直接写别人网站世界就乱了:)

看错了,你有密码的,你看看服务器是否打开权限?
帮你顶
 
后退
顶部