DataSnap Server HTTP json格式修改 返回图片
{"result":["hello"]}
GetCommandResponse>DSHTTPService1FormatResult>(if not Handled then) GetCommandResponse
1、去掉result符号
能否只返回["hello"] ,不加result标识,和tcp/IP返回的结果一样?
procedure TServerContainer1.DSHTTPService1FormatResult(Sender: TObject;
var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean);
var
str: string;
begin
// str :=ResultVal.ToString;
Handled := true;//加上这句话就可以了。
end;
在Datasnap.DSHTTPCommon.pas,有
procedure TDSJsonResponseHandler.GetCommandResponse
if not Handled then
ResponseObj := TJSONObject.Create(TJSONPair.Create(TJSONString.Create('result'), JsonParam));的处理,所以可以屏蔽。Handled 设为true就好了。
还有URL编码,特别是汉字,如何能禁止呢??再查。
2、去掉转义字符
元凶在这Datasnap.DSHTTPCommon.pas,TDBXJSONTools.DBXToJSON,
'["[{\"PID\":\"0001\",\"Name\":\"\u5F20\u4E09\u4E30\",\"Sex\":\"\u7537\",\"Age\":100},{\"PID\":\"002\",\"Name\":\"\u90ED\u9756\",\"Sex\":\"\u7537\",\"Age\":80}]"]'
JsonParam := TDBXJSONTools.DBXToJSON(Command.Parameters[I].Value,
Command.Parameters[I].DataType, FDSService.LocalConnection);
//用下面这句话就可以了,DBXToJSON引起的。
JsonParam := TJSONObject.ParseJSONValue(Command.Parameters[I].Value.AsString) as TJSONValue;
TJSONString
2016.10.11 做了二次修改,兼容单个键值,数值值。例如,进返回时间["2012.0.0.0."]
procedure TDSJsonResponseHandler.GetCommandResponse(Command: TDBXCommand; out Response: TJSONValue;
out ResponseStream: TStream);
var
JsonParams: TJSONArray;
JsonParam,JsonParamBak,jv: TJSONValue;
I: Integer;
ParamDirection: TDBXParameterDirection;
OwnParams: Boolean;
ConvList: TObjectList<TDBXRequestFilter>;
ResponseObj: TJSONObject;
Handled: Boolean;
tstr:string;
strStream:TStringStream;
useDefault:Boolean;
begin
JsonParams := nil;
OwnParams := false;
ConvList := nil;
ResponseStream := nil;
Handled := False; try
// collect the output/return parameters
JsonParams := TJSONArray.Create;
OwnParams := true;
useDefault:=false;
ConvList := TObjectList<TDBXRequestFilter>.Create(false); for I := to Command.Parameters.Count - do
begin
// select the output and return parameters for the response
ParamDirection := Command.Parameters[I].ParameterDirection;
if (ParamDirection = TDBXParameterDirections.OutParameter) or
(ParamDirection = TDBXParameterDirections.InOutParameter) or
(ParamDirection = TDBXParameterDirections.ReturnParameter) then
begin
JsonParam := nil;
ConvList.Clear; {If a subclass doesn't handle the parameter themselves, then manage the parameter by either
applying a filter on the result, or passing back the pure JSON representation}
if not HandleParameter(Command, Command.Parameters[I], JsonParam, ResponseStream) then
begin
FDSService.FiltersForCriteria([I], I = Command.Parameters.Count - , ConvList);
if ConvList.Count = then
begin
if not ConvList.Items[].CanConvert(Command.Parameters[I].Value) then
Raise TDSServiceException.Create(Format(SCannotConvertParam, [I, ConvList.Items[].Name]));
JsonParam := ConvList.Items[].ToJSON(Command.Parameters[I].Value, FDSService.LocalConnection);
end
else
begin
JsonParam := TJSONObject.ParseJSONValue(Command.Parameters[I].Value.AsString) as TJSONValue;
if JsonParam=nil then
begin
useDefault:=true;
JsonParam := TDBXJSONTools.DBXToJSON(Command.Parameters[I].Value,
Command.Parameters[I].DataType, FDSService.LocalConnection);
end;
end;
end; if JsonParam <> nil then
JsonParams.AddElement(JsonParam);
end;
end; //Store the result array as a JSON Value, to make it more generic for the event and result object
JsonParamBak:=JsonParam;
JsonParam := JsonParams; if Assigned(FResultEvent) then
FResultEvent(Self, JsonParam, Command, Handled); if not Handled then
begin
ResponseObj := TJSONObject.Create(TJSONPair.Create(TJSONString.Create('result'), JsonParam));
//allow subclasses to add to the result object if they wish
ProcessResultObject(ResponseObj, Command);
Response := ResponseObj;
end
else
begin
if (JsonParams.Count = ) and (useDefault=false) then
Response := JsonParamBak
else
Response := JsonParam;
end; OwnParams := false;
finally
FreeAndNil(ConvList);
if OwnParams then
JsonParams.Free;
end;
end;
GetCommandResponse
3、 防止汉字转成unicode数字
[{"PID":"0001","Name":"张三丰","Sex":"男","Age":100},{"PID":"002","Name":"郭靖","Sex":"男","Age":80}]
通过浏览器打开URL,返回的是
[[{"PID":"0001","Name":"\u5F20\u4E09\u4E30","Sex":"\u7537","Age":100},{"PID":"002","Name":"\u90ED\u9756","Sex":"\u7537","Age":80}]]
Datasnap.DSHTTP.pas function PopulateContent
ResponseInfo.ContentText := StringOf(ByteContent(Response))
改为:
ResponseInfo.ContentText := Response.ToString;
Response.Value 为空的原因是结构不一致不,从tjson读取后看value不为空,那么返回value测试一下不。或者对于 Command.Parameters[I].DataType是字符的特殊处理。
CharSet
IdCustomHTTPServer.pas
FConnection.IOHandler.Write(ContentText, CharsetToEncoding(CharSet));
IdCustomHTTPServer.pas
ResponseInfo.ContentText 这个就是返回字符串,
ResponseInfo.ContentText := Response.ToString;// Response.ToJSON;
ResponseInfo.ContentText := AnsiReplaceStr(ResponseInfo.ContentText, '\"','"');
还是因为这个引起的,System.JSON.pas
function TJSONString.ToString: string;
begin
if FStrBuffer = nil then
Result := ''
else
Result := '"' + AnsiReplaceStr(FStrBuffer.ToString, '"', '\"') + '"';
end;
var
jo: TJSONObject;
Command: TDBXCommand;
pa: TDBXParameter;
JsonParam: TJSONValue;
begin pa := TDBXParameter.Create(nil);
pa.DataType := TDBXDataTypes.AnsiStringType;
pa.Value.AsString := '[{"name":"david"}]'; JsonParam := TDBXJSONTools.DBXToJSON(pa.Value, pa.DataType, false);
self.Caption := JsonParam.Value;
JsonParam.ToString;
4、字符编码
text/html与text/plain
在浏览器打开的时候汉字是乱码,查看网页属性,编码是windows-132,而asp。net生成的webapi网页是utf-8,查找IdCustomHTTPServer.pas
procedure TIdHTTPResponseInfo.WriteHeader;
var
i: Integer;
LBufferingStarted: Boolean;
begin
if HeaderHasBeenWritten then begin
raise EIdHTTPHeaderAlreadyWritten.Create(RSHTTPHeaderAlreadyWritten);
end;
FHeaderHasBeenWritten := True; if AuthRealm <> '' then
begin
ResponseNo := ;
if (Length(ContentText) = ) and (not Assigned(ContentStream)) then
begin
ContentType := 'text/html; charset=utf-8'; {Do not Localize}
ContentText := '<HTML><BODY><B>' + IntToStr(ResponseNo) + ' ' + ResponseText + '</B></BODY></HTML>'; {Do not Localize}
ContentLength := -; // calculated below
end;
end; // RLebeau 5/15/2012: for backwards compatibility. We really should
// make the user set this every time instead...
if ContentType = '' then begin
if (ContentText <> '') or (Assigned(ContentStream)) then begin
ContentType := 'text/html; charset=ISO-8859-1'; {Do not Localize}//ContentType := 'text/plain; charset=utf-8';
end;
end;
字符编码是charset=ISO-8859-1,改成utf-8就好了!!!
给TDBXParameter赋值
procedure TForm4.Button1Click(Sender: TObject);
var
LDBXParameter: TDBXParameter;
LJSONParam: TJSONValue;
pin: TDBXParameter;
begin
LDBXParameter := TDBXParameter.Create(); pin := TDBXParameter.Create();
pin.DataType := TDBXDataTypes.AnsiStringType;
pin.Value.AsString := '[{"PatientID":"aaaaaaaaa","PictuerName":"BBBBBBBBBBB"}]';
LJSONParam := TDBXJSONTools.DBXToJSON(pin.Value, pin.DataType, false);
//下面这句直接赋值给LJSONParam 不行,导致下面的JSONToDBX转换失败,所以改用上面的方法DBXToJSON
// LJSONParam := TJSONObject.ParseJSONValue('[{"PatientID":"aaaaaaaaa","PictuerName":"BBBBBBBBBBB"}]'); LDBXParameter.DataType := TDBXDataTypes.WideStringType;
TDBXJSONTools.JSONToDBX(LJSONParam, LDBXParameter.Value, LDBXParameter.DataType, true, true (* Owns *) );
Memo1.Lines.Text := LDBXParameter.Value.AsString end;
Datasnap.DSService.pas
procedure TDSRESTService.ProcessParameters(const ADSMethodName: string; const Params: TStrings; Content: TArray<Byte>; const ACommand: TDBXCommand);
dataSnap服务器获取get、post方法参数
URL格式
http://localhost:8023/datasnap/rest/TServerMethods1/EchoString?p1=p1325325&p2=anme
uses Data.DBXPlatform;
function TServerMethods1.EchoString(Value: string): string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
end;
这样只能获取到列表的参数,无法获取流参数。
获取POST方法
把方法名加update前缀
function TServerMethods1.updateEchoString(Value: string): string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
Value就是post里的字符串
end;
RESTDebug发送汉字编码处理(未使用utf8编码)
RESTDebug,发送汉字,TEncoding.ANSI.GetString(Content)可以接收汉字。如下修改正常了。 Datasnap.DSService.pas文件。原版代码接收到的是乱码。
var
as1:AnsiString;
utf8Bytes:TBytes;
as1 := TEncoding.ANSI.GetString(Content);
utf8Bytes:=TEncoding.UTF8.GetBytes(as1);
LBody := TJSONObject.ParseJSONValue(utf8Bytes, 0,True);
如果汉字经过ut8编码0x,比如在线工具里的utf8编码0x0x0x则,接收正常,无需任何改动。
用http://ouapi.com/在线post工具发送明文汉字
datasnap服务器接收乱码。
返回数据自己修改
2018.1.18
想返回什么格式都行啦!!!
function TServerMethods1.EchoString(Value: string): string;
begin
Result := Value;
GetInvocationMetadata.ResponseContent:='abc';
end;
like this
http://blog.csdn.net/maxwoods/article/details/25163357
https://stackoverflow.com/questions/13879238/return-an-image-from-a-delphi-rest-server-and-show-it-in-a-browser/13879240#13879240
还可以设置返回的格式,这样的话不需要任何修改了,不需要修改原文件,各种跨平台前端都支持!
GetInvocationMetadata.ResponseContentType := 'image/png';
GetInvocationMetadata.ResponseContentType := 'image/jpeg';
GetInvocationMetadata.ResponseContentType := 'application/json';
GetInvocationMetadata().ResponseContentType := 'application/json; charset=utf-8';
GetInvocationMetaData.CloseSession := True;
thanks
http://www.itgo.me/a/x5918832448088122623/how-to-download-jpeg-image-for-browsers-in-delphi-c-builder-rest-webbroker
void TCommerMethods::DownloadImage()
{
TStringStream *Stream;
String S;
Stream = new TStringStream;
Stream->LoadFromFile("C:\\Temp\\MyImage.jpg");
S = Stream->DataString;
delete Stream;
GetInvocationMetadata()->ResponseContentType = "image/jpeg";
GetInvocationMetadata()->ResponseContent = S;
}
https://pastebin.com/tgiNep52
function CarregarImagem(const sCaminho: String): AnsiString;
var
oFileStream : TFileStream;
begin
oFileStream:= TFileStream.Create(sCaminho, fmOpenRead or fmShareDenyWrite);
try
if oFileStream.Size > then
begin
SetLength(Result, oFileStream.Size);
oFileStream.Read(Pointer(Result)^, oFileStream.Size);
end;
finally
FreeAndNil(oFileStream);
end; sImagem := CarregarImagem(sDirImagem);
if (bExtPNG) then
GetInvocationMetadata().ResponseContentType := 'image/png'
else
begin
GetInvocationMetadata().ResponseContentType := 'image/jpeg';
GetInvocationMetadata().ResponseCode := ;
GetInvocationMetadata().ResponseContent := sImagem;
GetInvocationMetadata().CloseSession := True;
end;
http://kelvermerlotti.com/por-que-o-datasnap-rest-retorna-um-json-sujo/#more-203
procedure TWebModule1.DSHTTPWebDispatcher1FormatResult(Sender: TObject;
var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean);
var
Aux: TJSONValue;
begin
Aux := ResultVal;
ResultVal := TJSONArray(Aux).Items[0];
TJSONArray(Aux).Remove(0);
Aux.Free;
Handled := True;
end
DataSnap Server HTTP json格式修改 返回图片的更多相关文章
- struts2使用jsp和<s:property>标签获取json格式的返回数据
struts2使用jsp和<s:property>标签获取json格式的返回数据 1.struts2的action中 return "success"; 2.指向的返回 ...
- 关于C# webapi ,接口返回字符串和json格式 ,返回值中有反斜杠
最近遇到一个比较郁闷的问题,记录一下 写了一个接口,想返回json 数据,但是返回值中总是带有反斜杠... ,下面来看原因 首先,配置 webapi的路由 App_Start 文件夹下 ,WebApi ...
- JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口
这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用JSONP调用. 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID& ...
- @ResponseBody将集合数据转换为json格式并返回给客户端
spring-mvc.xml: <beans xmlns:mvc="http://www.springframework.org/schema/mvc" > <m ...
- ReadyAPI/soapUI发送post请求json格式(带有中文字符),后台获取参数为空
解决:请求编码格式默认为空,在"TestCase"的指定Step的Request Properties中, 改Encoding编码格式为UTF-8. 原文:soapUI发送post ...
- Django-choices字段值对应关系(性别)-MTV与MVC科普-Ajax发json格式与文件格式数据-contentType格式-Ajax搭配sweetalert实现删除确认弹窗-自定义分页器-批量插入-07
目录 models 字段补充 choices 参数/字段(用的很多) MTV与MVC模型 科普 Ajax 发送 GET.POST 请求的几种常见方式 用 Ajax 做一个小案例 准备工作 动手用 Aj ...
- html中通过js获取接口JSON格式数据解析以及跨域问题
前言:本人自学前端开发,一直想研究下js获取接口数据在html的实现,顺利地找到了获取数据的方法,但是有部分接口在调用中出现无法展示数据.经查,发现时跨域的问题,花费了一通时间,随笔记录下过程,以方便 ...
- java请求POST发送json格式请求
public static String upload(String url){ try { HttpClient httpclient = new DefaultHttpClient(); Http ...
- 快速将对象转化为JSON格式
1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...
随机推荐
- L5负载均衡
L5负载均衡组件的功能职责 L5的功能特征如下: 名字服务:以SID(由模块ID和命令字ID组成)为关键字,通过SID取得真正的IP和端口地址,使得IP和端口配置对调用者透明,运维变更配置更方便: 负 ...
- dgraph cluster docker-compose 安装
dgraph 是一款基于golang 的图数据库,使用了graphql+ 的查询方式 集群的安装官方也提供了对应的模版,比较简单 docker-compose 文件 我做了一些简单修改(数据存储的问题 ...
- FastAdmin 绑定的模块禁用路由
为了安全,将后台入口隐藏. 这里出一个问题,因为装了 CMS 插件,使用入口登录后显示的是 CMS 的首页. 这个问题已经修复. https://gitee.com/karson/fastadmin/ ...
- Refused to display '[url]' in a frame because it set 'X-Frame-Options' to 'Deny'.
X-Frame-Options是一个HTTP标头(header),用来告诉浏览器这个网页是否可以放在iFrame内.例如: X-Frame-Options: DENY X-Frame-Options: ...
- netty异步
通俗理解:http://lingnanlu.github.io/2016/08/16/netty-asyc-callback 异步的小demo:https://blog.csdn.net/coder_ ...
- 【linux】crontab命令
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...
- spring boot学习(1) hello world
第一节:SpringBoot 简介 主要是简化开发: 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提 ...
- java web程序 html标签中<th>和<td>的用法区别:
<th>是用于表头式的 <td>是列 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...
- linux 下 rpc python 实例之使用XML-RPC进行远程文件共享
这是个不错的练习,使用python开发P2P程序,或许通过这个我们可以自己搞出来一个P2P下载工具,类似于迅雷.XML-RPC是一个远程过程调用(remote procedure call,RPC)的 ...
- 解决thinkphp设置session周期无效的问题
thinkphp的session设置周期是无效的:直接的影响就是无法保留用户的登陆状态:用thinkphp开发的项目:关闭浏览器后用户就退出了:即便设置了session周期也没作用:这个bug存在很久 ...