关于cgi的问题!!!!!!!!!!(50分)

  • 主题发起人 主题发起人 mawei
  • 开始时间 开始时间
M

mawei

Unregistered / Unconfirmed
GUEST, unregistred user!
返回报错CGI的头不对,应该怎么改???
program counter;
{$APPTYPE CONSOLE}
uses SysUtils,
Windows,
Graphics,
JPEG,
Classes;
var
BitmapImage: TBitmap;
JPEGImage: TJPEGImage;
NumWidth: Byte;
NumStr: String[10];
Rect: TRect;
MemStream: TMemoryStream;
HandleStr: THandleStream;
CountValue: LongInt;

begin
{ Create the bitmap for the counter }
BitmapImage := TBitmap.Create;
{ Load the Font attributes }
with BitmapImage.Canvas.Fontdo
begin
Name := 'Arial';
Height := 16;
{ Pixels }
Color := clRed;
end;
{ Set the background color }
with BitmapImage.Canvas.Brushdo
begin
Color := clBlack;
end;

NumWidth := 5;
CountValue :=6356;
{ Create the count as a string }
NumStr := IntToStr(CountValue);
if NumWidth > 0 then
while Length(NumStr) < NumWidthdo
NumStr := '0' + NumStr;
{----------------------------------------------------------------}
{----------------- Build the image -----------------------------}
{----------------------------------------------------------------}
with BitmapImage, BitmapImage.Canvasdo
begin
Height := TextHeight(NumStr)+4;
Width := TextWidth(NumStr)+4;
with Rectdo
begin
Top := 0;
Left := 0;
Bottom := Height;
Right := Width;
end;
FillRect(Rect);
TextOut(2, 2, NumStr);
end;

{---------------------------------------------------------------}
{---------- Write the image to stdout --------------------------}
{---------------------------------------------------------------}
{ Assign the bitmap to the JPEG }
JPEGImage := TJPEGImage.Create;
JPEGImage.Assign(BitmapImage);
JPEGImage.CompressionQuality := 100;
{ Best quality }
{ Create a memory stream to get the contents of the JPEG file. This is
only to determine the size of the JPEG. If I knew how to get the
size of the JPEG right from the JPEGImage, then
the saving to the
memory stream step can be avoided }
MemStream := TMemoryStream.Create;
JPEGImage.SaveToStream(MemStream);
{ Create a stream connected to stdout }
HandleStr := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));
{ Write out the content type to stdout }

writeLn('http/1.1 200 ');
writeLn('Date: Thu, 01 Apr 1999 05:07:31 GMT');
writeLn('Last-Modified: Mon, 17 Aug 1998 00:01:33 GMT');
WriteLn('Content-type: image/jpeg');
WriteLn('Content-length: '+intToStr(SizeOf(MemStream)));
WriteLn(' ');
WriteLn(' ');
WriteLn(' ');
WriteLn(' ');
WriteLn('');

{ Write the contents of the memory stream (i.e. the JPEG image) to stdout }
MemStream.SaveToStream(HandleStr);
{ Free everything }
HandleStr.Free;
MemStream.Free;
BitmapImage.Free;
JPEGImage.Free;

end.
 
?
问题也许出在
writeLn('http/1.1 200 ');
writeLn('Date: Thu, 01 Apr 1999 05:07:31 GMT');
writeLn('Last-Modified: Mon, 17 Aug 1998 00:01:33 GMT');
WriteLn('Content-type: image/jpeg');
WriteLn('Content-length: '+intToStr(SizeOf(MemStream)));
WriteLn(' ');
WriteLn(' ');
WriteLn(' ');
WriteLn(' ');
WriteLn('');
 
是不是应该加上一局
response.context :=mystring;
mystring.add......
 
?什么意思?
怎么没有人回答我?
程序看不懂吗?
是一个关于返回数字的图象的程序!
拜托了!!!!
 
?什么意思?
怎么没有人回答我?
程序看不懂吗?
是一个关于返回数字的图象的程序!
拜托了!!!!
 
看来SizeOf(MemStream)似乎有问题,我还没试。
我认为应该是MemStream.Size才对。
 
CGI中, content-type句子后要在加一个回车. Writeln;
 
在CGI规范中, Content-type定义后必须加两个回车.
具体信息, 如下:
Content-type是浏览器处理的信息类型,主要是MIME的类型。定义的方法为类型和子类型
的组合。常用的MIME类型有:Text、Multipart、Message、Application、Image、Audio和
Video。
例如,CGI程序要向浏览器传送一个HTML文档前,应先传送text/html。用Perl书写如下:
print "Content-type:text/html/n/n"
print "<h1> Hi everyone! </h1>/n"

WriteLn('Content-type: image/jpeg');
WriteLn;
WriteLn('Content-length: '+intToStr(SizeOf(MemStream)));
如果您有兴趣, 可以去http://www.chinabyte.com看看我写的CGI教程. :)
 
我已经知道问题的原因了,是因为STD_OUTPUT已128字节为段缓冲输出,没有Flush
标准输出,所以出问题了,应该用无缓冲输出。
解决办法马上就来,请稍等。
 
答案来了。
program Project2;
{$APPTYPE CONSOLE}
uses SysUtils,
Windows,
Graphics,
JPEG,
Classes;
var
BitmapImage: TBitmap;
JPEGImage: TJPEGImage;
NumWidth: Byte;
NumStr: String[10];
Rect: TRect;
MemStream: TMemoryStream;
HandleStr: THandleStream;
CountValue: LongInt;

begin
{ Create the bitmap for the counter }
BitmapImage := TBitmap.Create;
{ Load the Font attributes }
with BitmapImage.Canvas.Fontdo
begin
Name := 'Arial';
Height := 16;
{ Pixels }
Color := clRed;
end;
{ Set the background color }
with BitmapImage.Canvas.Brushdo
begin
Color := clBlack;
end;

NumWidth := 5;
CountValue :=6356;
{ Create the count as a string }
NumStr := IntToStr(CountValue);
if NumWidth > 0 then
while Length(NumStr) < NumWidthdo
NumStr := '0' + NumStr;
{----------------------------------------------------------------}
{----------------- Build the image -----------------------------}
{----------------------------------------------------------------}
with BitmapImage, BitmapImage.Canvasdo
begin
Height := TextHeight(NumStr)+4;
Width := TextWidth(NumStr)+4;
with Rectdo
begin
Top := 0;
Left := 0;
Bottom := Height;
Right := Width;
end;
FillRect(Rect);
TextOut(2, 2, NumStr);
end;

{---------------------------------------------------------------}
{---------- Write the image to stdout --------------------------}
{---------------------------------------------------------------}
{ Assign the bitmap to the JPEG }
JPEGImage := TJPEGImage.Create;
JPEGImage.Assign(BitmapImage);
JPEGImage.CompressionQuality := 100;
{ Best quality }
{ Create a memory stream to get the contents of the JPEG file. This is
only to determine the size of the JPEG. If I knew how to get the
size of the JPEG right from the JPEGImage, then
the saving to the
memory stream step can be avoided }
MemStream := TMemoryStream.Create;
JPEGImage.SaveToStream(MemStream);
{ Create a stream connected to stdout }
HandleStr := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));
{ Write out the content type to stdout }
writeLn('http/1.1 200 ');
writeLn('Date: Thu, 01 Apr 1999 05:07:31 GMT');
writeLn('Last-Modified: Mon, 17 Aug 1998 00:01:33 GMT');
WriteLn('Content-type: image/jpeg');
WriteLn('Content-length: '+intToStr(MemStream.Size));
//此处作了修改,SizeOf(MemStream)不对
//此处删除了多余的WriteLn,否则与给定的Size不同,无法显示。
WriteLn('');
Flush(Output);
// ******此处非常重要

{ Write the contents of the memory stream (i.e. the JPEG image) to stdout }
MemStream.SaveToStream(HandleStr);
{ Free everything }
HandleStr.Free;
MemStream.Free;
BitmapImage.Free;
JPEGImage.Free;

end.
 
你试过了吗?我要死了,还是不行!这是按你们所说的写的:
program Project2;
{$APPTYPE CONSOLE}
uses SysUtils,
Windows,
Graphics,
JPEG,
Classes;
var
BitmapImage: TBitmap;
JPEGImage: TJPEGImage;
NumWidth: Byte;
NumStr: String[10];
Rect: TRect;
MemStream: TMemoryStream;
HandleStr: THandleStream;
CountValue: LongInt;

begin
{ Create the bitmap for the counter }
BitmapImage := TBitmap.Create;
{ Load the Font attributes }
with BitmapImage.Canvas.Fontdo
begin
Name := 'Arial';
Height := 16;
{ Pixels }
Color := clRed;
end;
{ Set the background color }
with BitmapImage.Canvas.Brushdo
begin
Color := clBlack;
end;

NumWidth := 5;
CountValue :=6356;
{ Create the count as a string }
NumStr := IntToStr(CountValue);
if NumWidth > 0 then
while Length(NumStr) < NumWidthdo
NumStr := '0' + NumStr;
{----------------------------------------------------------------}
{----------------- Build the image -----------------------------}
{----------------------------------------------------------------}
with BitmapImage, BitmapImage.Canvasdo
begin
Height := TextHeight(NumStr)+4;
Width := TextWidth(NumStr)+4;
with Rectdo
begin
Top := 0;
Left := 0;
Bottom := Height;
Right := Width;
end;
FillRect(Rect);
TextOut(2, 2, NumStr);
end;

{---------------------------------------------------------------}
{---------- Write the image to stdout --------------------------}
{---------------------------------------------------------------}
{ Assign the bitmap to the JPEG }
JPEGImage := TJPEGImage.Create;
JPEGImage.Assign(BitmapImage);
JPEGImage.CompressionQuality := 100;
{ Best quality }
{ Create a memory stream to get the contents of the JPEG file. This is
only to determine the size of the JPEG. If I knew how to get the
size of the JPEG right from the JPEGImage, then
the saving to the
memory stream step can be avoided }
MemStream := TMemoryStream.Create;
JPEGImage.SaveToStream(MemStream);
{ Create a stream connected to stdout }
HandleStr := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));
{ Write out the content type to stdout }
writeLn('http/1.1 200 ');
writeLn('Date: Thu, 01 Apr 1999 05:07:31 GMT');
writeLn('Last-Modified: Mon, 17 Aug 1998 00:01:33 GMT');
WriteLn('Content-type: image/jpeg');
WriteLn;
WriteLn('Content-length: '+intToStr(MemStream.Size));
//此处作了修改,SizeOf(MemStream)不对
//此处删除了多余的WriteLn,否则与给定的Size不同,无法显示。
WriteLn;
Flush(Output);
// ******此处非常重要

{ Write the contents of the memory stream (i.e. the JPEG image) to stdout }
MemStream.SaveToStream(HandleStr);
MemStream.SaveToFile('c:/1.jpg');
{ Free everything }
HandleStr.Free;
MemStream.Free;
BitmapImage.Free;
JPEGImage.Free;

end.

这次CGI不报错,但是,不显示图象!!!!!!!!!!
拜托了!
 
我刚刚试过,在Netscape下是正常的,但在IE下不行,原因待查,稍等。
 
你要气死我了,我用了一中午的时间调你的程序,最后才发现你居然在
WriteLn('Content-type: image/jpeg');
后又加了一个WriteLn,简直岂有此理,我调好的程序你贴过去用就没事了,
偏偏加了这么一句,白白浪费了我一个中午。
另外,上一条消息最后发现是我搞错了,原因仍是你的程序搞的,我用Netscape时
用的是我的程序,用IE时是你的程序。当然不对了。
 
对不起!加一个writeln是Jimchael Tsee说的!
而且,我加这一句是因为按你的程序调不好,才
加的。
我是把你的程序^c然后^p以后编译,但是还是不
好用,拖大了!!!!!!!!!!
不管怎样,这分是给你了,对不起,耽误你时间了!
但是为什么还不行呢?
 
还有,我编译后试了一下,发现确实是IE不可见,NETSCAPT可见!
这是为什么!
决不是那个writeln的问题!
 
1、那个WriteLn是要去掉的。
2、我这现在NETSCAPE和IE都好了,似乎是IE的缓冲有问题,但不能肯定,重启一下
机器试试。实在不行的话,把头上的Date和Last Modify去掉试试。
 
后退
顶部