http服务用indy httpserver 简单实现,只负责将客户端Post的数据显示。
客户端想通过ClientSocket 构造http协议数据来Post 实现。
服务端:
procedure TFrmMain.IdServerCommandGet(AThread: TIdPeerThread;
RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo);
var
sRequest:String;
begin
if Requestinfo.Command = 'POST' then
begin
sRequest := RequestInfo.UnparsedParams;
OutPutLog(sRequest);
//获取回送客户端信息
ResponseInfo.ContentText :=GetResponse('002',sRequest);
end;
end;
客户端:ClienntSocket 阻塞 SendStr:=SendStr+'POST / HTTP/1.1'+#13#10;
SendStr:=SendStr+'Accept: */*'+#13#10;
SendStr:=SendStr+'Connection: Keep-Alive'+#13+#10;
SendStr:=SendStr+#13#10;
SendStr:=SendStr+sTmp; //stmp 存放xml 数据
cs.Host :='127.0.0.1';
cs.Port :=2001;
cs.Open;
if cs.Active then
cs.Socket.SendText(SendStr);
mmLog.Lines.Text :=cs.Socket.ReceiveText; 测试发现服务端接收不到数据,请教HTTP协议高手指点,是否协议构造有问题。
使用Indy http控件测试服务器可以正常接收Post数据回送数据也正常。
客户端想通过ClientSocket 构造http协议数据来Post 实现。
服务端:
procedure TFrmMain.IdServerCommandGet(AThread: TIdPeerThread;
RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo);
var
sRequest:String;
begin
if Requestinfo.Command = 'POST' then
begin
sRequest := RequestInfo.UnparsedParams;
OutPutLog(sRequest);
//获取回送客户端信息
ResponseInfo.ContentText :=GetResponse('002',sRequest);
end;
end;
客户端:ClienntSocket 阻塞 SendStr:=SendStr+'POST / HTTP/1.1'+#13#10;
SendStr:=SendStr+'Accept: */*'+#13#10;
SendStr:=SendStr+'Connection: Keep-Alive'+#13+#10;
SendStr:=SendStr+#13#10;
SendStr:=SendStr+sTmp; //stmp 存放xml 数据
cs.Host :='127.0.0.1';
cs.Port :=2001;
cs.Open;
if cs.Active then
cs.Socket.SendText(SendStr);
mmLog.Lines.Text :=cs.Socket.ReceiveText; 测试发现服务端接收不到数据,请教HTTP协议高手指点,是否协议构造有问题。
使用Indy http控件测试服务器可以正常接收Post数据回送数据也正常。
谢谢月亮。
memo1.lines.loadfromfile('a.xml');
xmlHttp:=CreateOleObject('Msxml2.XMLHTTP');
xmlHttp.open('POST',sUrl,false);
xmlHttp.send(memo1.lines.txt); --执行这里提示参数不正确。
sTmp:=xmlHttp.responseText;
还不明白 xmlhttp 异步执行的原理。如何接收服务回报。
url:string;
xmlHttp:Olevariant;
responseText:Widestring;
begin
try
xmlHttp:=CreateOleObject('Msxml2.XMLHTTP');
xmlHttp.open('POST',url,false);
xmlHttp.send('fdfdsfdsa');
responseText:=xmlHttp.responseText;
if xmlHttp.status='200' then
begin
//--
end;
finally
xmlHttp:= unAssigned;
end;
end;就是这样用,没什么问题
url:string;
xmlHttp:Olevariant;
responseText:Widestring;
begin
try
xmlHttp:=CreateOleObject('Msxml2.XMLHTTP');
xmlHttp.open('POST',url,false);
xmlHttp.setRequestHeader('Content-Type', 'text/xml');
xmlHttp.send(xmlStr);
responseText:=xmlHttp.responseText;
if xmlHttp.status='200' then
begin
//--
end;
finally
xmlHttp:= unAssigned;
end;
end;