D
DieGhost
Unregistered / Unconfirmed
GUEST, unregistred user!
传送中没任何错误,可服务器生成的gif就只有4个字节
如果用TMemoryStream就没有任何问题,传送的文件是正确的
而我不想用TMemoryStream实现
帮忙看看,谢谢,代码如下
function PostURL(const aUrl: string;const strPostOkResult: string = 'OK'): Boolean;
const
sHeader='Content-Type: multipart/form-data; boundary=---------------------------7d56b1cf06a6'
+#13#10+'Accept-Encoding: gzip, deflate';
crlf=#13#10;
var
hSession: HINTERNET;
PFHandle: HWND;
ISi,ISize: Integer;
hConnect, hRequest: hInternet;
lpBuffer: array[0..1024 + 1] of Char;
dwBytesRead: DWORD;
HttpStr: string;
HostName, FileName: string;
FTResult: Boolean;
AcceptType: LPStr;
Buf: Pointer;
BufA,Buffchar;
sFileName:string;
sbuf,sbuf1:string;
dwBufLen, dwIndex: DWord;
procedure ParseURL(URL: string; var HostName, FileName: string);
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;
var
i: Integer;
begin
if Pos(UpperCase('http://'), UpperCase(URL)) <> 0 then
System.Delete(URL, 1, 7);
i := Pos('/', URL);
HostName := Copy(URL, 1, i);
FileName := Copy(URL, i, Length(URL) - i + 1);
if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
SetLength(HostName, Length(HostName) - 1);
end;
begin
ParseURL(aUrl, HostName, FileName);
sfilename:='c:/test.gif';
sbuf:='---------------------------7d56b1cf06a6'+crlf;
sbuf:=sbuf+'Content-Disposition: form-data; name="filepath"'+crlf+crlf+'.'+crlf;
sbuf:=sbuf+'---------------------------7d56b1cf06a6'+crlf;
sbuf:=sbuf+'Content-Disposition: form-data; name="file1"; filename="c:/test.gif"'+crlf;
sbuf:=sbuf+'Content-Type: application/octet-stream'+crlf+crlf;
sbuf1:=crlf+'---------------------------7d56b1cf06a6'+crlf;//分割符
sbuf1:=sbuf1+'---------------------------7d56b1cf06a6--';//结束符
ISi:=length(sbuf)+length(sbuf1);
PFHandle:= FileOpen(sfilename, fmOpenRead);
Try
IF PFHandle > 0 then
Begin
ISize:= FileSeek(PFHandle, 0, 2);
GetMem(Buff, ISize);
FileSeek(PFHandle, 0, 0);
FileRead(PFHandle, buff^, ISize);
End;
Finally
FileClose(PFHandle);
End;
ISi:=ISi + ISize;
GetMem(BufA,Isi+1);
strCopy(BufA,pchar(sbuf));
strCat(BufA,Buff);
strCat(BufA,pchar(sbuf1));
Result := False;
hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
if Assigned(hSession) then
begin
hConnect := InternetConnect(hSession, PChar(HostName),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
AcceptType := PChar('Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*');
hRequest := HttpOpenRequest(hConnect, 'POST', PChar(FileName), 'HTTP/1.0',
nil, @AcceptType, INTERNET_FLAG_RELOAD, 0);
//
HttpSendRequest(hRequest, pchar(sHeader), length(sHeader),Pointer(BufA),ISi);
dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);
FTResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,
Buf, dwBufLen, dwIndex);
if FTResult = True then
try
while True do
begin
dwBytesRead := 1024;
InternetReadFile(hRequest, @lpBuffer, 1024, dwBytesRead);
if dwBytesRead = 0 then break;
lpBuffer[dwBytesRead] := #0;
HttpStr := HttpStr + lpBuffer;
end;
Result := pos(strPostOkResult, HttpStr) > 0;
finally
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
end;
end;
finally
InternetCloseHandle(hSession);
end;
end;
如果用TMemoryStream就没有任何问题,传送的文件是正确的
而我不想用TMemoryStream实现
帮忙看看,谢谢,代码如下
function PostURL(const aUrl: string;const strPostOkResult: string = 'OK'): Boolean;
const
sHeader='Content-Type: multipart/form-data; boundary=---------------------------7d56b1cf06a6'
+#13#10+'Accept-Encoding: gzip, deflate';
crlf=#13#10;
var
hSession: HINTERNET;
PFHandle: HWND;
ISi,ISize: Integer;
hConnect, hRequest: hInternet;
lpBuffer: array[0..1024 + 1] of Char;
dwBytesRead: DWORD;
HttpStr: string;
HostName, FileName: string;
FTResult: Boolean;
AcceptType: LPStr;
Buf: Pointer;
BufA,Buffchar;
sFileName:string;
sbuf,sbuf1:string;
dwBufLen, dwIndex: DWord;
procedure ParseURL(URL: string; var HostName, FileName: string);
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;
var
i: Integer;
begin
if Pos(UpperCase('http://'), UpperCase(URL)) <> 0 then
System.Delete(URL, 1, 7);
i := Pos('/', URL);
HostName := Copy(URL, 1, i);
FileName := Copy(URL, i, Length(URL) - i + 1);
if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
SetLength(HostName, Length(HostName) - 1);
end;
begin
ParseURL(aUrl, HostName, FileName);
sfilename:='c:/test.gif';
sbuf:='---------------------------7d56b1cf06a6'+crlf;
sbuf:=sbuf+'Content-Disposition: form-data; name="filepath"'+crlf+crlf+'.'+crlf;
sbuf:=sbuf+'---------------------------7d56b1cf06a6'+crlf;
sbuf:=sbuf+'Content-Disposition: form-data; name="file1"; filename="c:/test.gif"'+crlf;
sbuf:=sbuf+'Content-Type: application/octet-stream'+crlf+crlf;
sbuf1:=crlf+'---------------------------7d56b1cf06a6'+crlf;//分割符
sbuf1:=sbuf1+'---------------------------7d56b1cf06a6--';//结束符
ISi:=length(sbuf)+length(sbuf1);
PFHandle:= FileOpen(sfilename, fmOpenRead);
Try
IF PFHandle > 0 then
Begin
ISize:= FileSeek(PFHandle, 0, 2);
GetMem(Buff, ISize);
FileSeek(PFHandle, 0, 0);
FileRead(PFHandle, buff^, ISize);
End;
Finally
FileClose(PFHandle);
End;
ISi:=ISi + ISize;
GetMem(BufA,Isi+1);
strCopy(BufA,pchar(sbuf));
strCat(BufA,Buff);
strCat(BufA,pchar(sbuf1));
Result := False;
hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
if Assigned(hSession) then
begin
hConnect := InternetConnect(hSession, PChar(HostName),
INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
AcceptType := PChar('Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*');
hRequest := HttpOpenRequest(hConnect, 'POST', PChar(FileName), 'HTTP/1.0',
nil, @AcceptType, INTERNET_FLAG_RELOAD, 0);
//
HttpSendRequest(hRequest, pchar(sHeader), length(sHeader),Pointer(BufA),ISi);
dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);
FTResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,
Buf, dwBufLen, dwIndex);
if FTResult = True then
try
while True do
begin
dwBytesRead := 1024;
InternetReadFile(hRequest, @lpBuffer, 1024, dwBytesRead);
if dwBytesRead = 0 then break;
lpBuffer[dwBytesRead] := #0;
HttpStr := HttpStr + lpBuffer;
end;
Result := pos(strPostOkResult, HttpStr) > 0;
finally
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
end;
end;
finally
InternetCloseHandle(hSession);
end;
end;