新手,高分求问:
报文示例如下{"image_content":"图片的base64内容","id":"12345678",}
接口方法http(post)方式,请求body是json
请问怎么用delphi实现,给个代码吧,谢谢
报文示例如下{"image_content":"图片的base64内容","id":"12345678",}
接口方法http(post)方式,请求body是json
请问怎么用delphi实现,给个代码吧,谢谢
解决方案 »
- 哪有 TChromium控件下载,或者请教怎么使用delphichromiumembedded?
- 類型轉換問題
- TDBComBox控件的小问题
- Borland使用者请进—关于盗版
- 如何打开指定路径的word文档?
- 一MM原创:一个月内妹妹打了三次胎
- dbgrid的数据保存的问题:dbgrid中一般是移开当前记录时才post的,如果直接关闭窗体时怎么保存数据呢?
- 有人会用ImageList吗?我不会也~~~
- 请教各位大虾:怎么才能卸载掉动态生成的FRAME?
- Delphi中要怎栏才能CLOSE一个空数据集?
- 色彩模式为cmyk的jpg 图怎么转成bmp图?在delphi7下
- 求教ImageEn,在虚拟视频上做上时间戳,但如何实现回传带了时间戳的图像?
begin
Result := TCustomIndyHttp.Create(nil);
Result.AllowCookies := true;
Result.HandleRedirects := true; // 允许头转向
Result.ReadTimeout := 5000; // 请求超时设置
Result.ConnectTimeout := 15000; Result.Request.ContentType := 'application/json'; // 设置内容类型为jso
Result.Request.Accept := 'application/json';
end;const
// URL = '/picasso-wan/governance-api/v1/file_upload.json';
URL = 'http://..........';
JSONStr = '{ "fileStr": "string"}';
var
AJSon, AResult: ISuperObject;
SendJSONStream: TStringStream;
AResultStr: string;
AIdhttp: TCustomIndyHttp;
AItem: TSuperArray;
begin
AIdhttp := CreateIdHttp;
AIdhttp.ConnectTimeout := 15000;
AIdhttp.ReadTimeout := 15000;
AJSon := SO(JSONStr);
Result := False;
AJSon.S['id'] :='12345678';
AJSon.S['image_content'] := '图片的base64内容';
SendJSONStream := TStringStream.Create(UTF8Encode(AJSon.AsString));
try
try
AResultStr := AIdhttp.Post(FHostURL + URL, SendJSONStream); AResult := SO(AResultStr);
if AResult.b['success'] then
begin
if Assigned(AResult['collection']) then
begin
AItem := AResult['collection'].AsArray;
if AItem.Length > 0 then
begin
APic.fileUrl := AItem[0]['fileUrl'].AsString;
APic.fileUrl := StringReplace(APic.fileUrl, '.jpg.jpg', '.jpg', [rfReplaceAll, rfIgnoreCase]); end;
Result := True;
end;
AErrStr := '无数据返回';
end;
except
on e: EIdHTTPProtocolException do
begin
AResultStr := e.ErrorMessage;
AResult := SO(AResultStr);
if Assigned(AResult['message']) then
begin
AErrStr := AResult['message'].AsString;
end
else
begin
if not ARsultErrMsg then
AErrStr := GetErrStr(e)
else
AErrStr := e.Message;
end;
end;
on e: Exception do
begin
if not ARsultErrMsg then
AErrStr := GetErrStr(e)
else
AErrStr := e.Message;
end;
end;
finally
AIdhttp.Free;
SendJSONStream.Free;
end;
end;
var
jsonObj: TJsonObject;
IdHttp: TIdHTTP;
postData: UTF8String;
resp: string;
reqStm: TStringStream;
begin
reqStm := TStringStream.Create('');
try
jsonObj := TJsonObject.Create;
try
jsonObj.AddPair('image_content','图片的base64内容');
jsonObj.AddPair('id','12345678');
postData := UTF8Encode(jsonObj.ToString);
reqStm.WriteBuffer(BytesOf(postData), Length(postData));
finally
FreeAndNil(jsonObj);
end;
IdHttp := TIdHTTP.Create;
try
try
IdHttp.AllowCookies := True;
IdHttp.ConnectTimeout := 30000;
IdHttp.Request.ContentType := 'application/json;charset=UTF-8';
IdHttp.Request.Accept := '*/*';
resp := IdHttp.Post('你的URL', reqStm);
if IdHttp.ResponseCode = 200 then
begin
//你的处理
end;
except ON E:Exception do
end;
finally
FreeAndNil(IdHttp);
end;
finally
FreeAndNil(reqStm);
end;
end;