DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: haibei
今日帖子: 8
在线用户: 9
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 linlingwei (飞雪) ★☆☆☆☆ -
盒子活跃会员
2023/9/15 12:36:28
标题:
新手请教 IdHTTP带Header的 POST方法? 浏览:300
加入我的收藏
楼主: 2个 HTTP Request Header,如下:
timestamp=1694751449
sign=f6f44fedfd3743fd2660bfc5d0038141

上传的数据明细
{"data":"张三"}

网址:http://10.2.8.39:8831/public/order
----------------------------------------------
-
作者:
男 bdsclub (重庆专业做Delphi的老人。) ★☆☆☆☆ -
普通会员
2023/9/15 14:06:53
1楼: IdHTTP1.Request.CustomHeaders.Add
----------------------------------------------
逐梦DELPHI的重庆仔儿!
作者:
男 linlingwei (飞雪) ★☆☆☆☆ -
盒子活跃会员
2023/9/15 15:41:56
2楼: @bdsclub:

procedure Tform1.Button2Click(Sender: TObject);
var
  IdHTTP1: TIdHTTP;
  Params: TStringList;
begin
  IdHTTP1 := TIdHTTP.Create(nil);
  Params := TStringList.Create;
  try
    IdHTTP1.Request.CustomHeaders.Add('timestamp:1694751449');
    IdHTTP1.Request.CustomHeaders.Add('sign:f6f44fedfd3743fd2660bfc5d0038141');

    Params.Add('{"data":"张三"}');
    IdHTTP1.Post('http://10.2.8.39:8831/public/order', Params);
  finally
    Params.Free;
    IdHTTP1.Free;
  end;
end;
一直提示错误 HTTP/1.1 400
----------------------------------------------
-
作者:
男 k3man (嗯哼) ★☆☆☆☆ -
普通会员
2023/9/15 16:27:18
3楼: var
  Headers: TStringList;
begin
  Headers := TStringList.Create;
  try
    Headers.Add('CustomHeader: CustomValue');
    // 添加其他 Header 字段...
    
    HTTP.Post('http://example.com', RequestContent, Headers);
    ...
  finally
 ....
  end;
end;
早该换nethttpclient了
----------------------------------------------
-
作者:
男 z_y_b_delphi (z_y_b_delphi) ★☆☆☆☆ -
普通会员
2023/9/15 16:30:33
4楼: 建议使用TNetHTTPClient,参考:
https://www.cnblogs.com/zhangxiny/p/17028219.html
----------------------------------------------
-
作者:
男 linlingwei (飞雪) ★☆☆☆☆ -
盒子活跃会员
2023/9/15 19:06:05
5楼: 听取大侠的意见,学用NetHTTPClient进行post,返回结果却一直提示
要用”application/json;charset=utf-8“格式,我已经设置这个格式了。
请大侠们给指点一下。
var
Client: TNetHTTPClient; Request: TNetHTTPRequest; Response: IHTTPResponse; Params: TStringList;
Client := TNetHTTPClient.Create(nil);
Request := TNetHTTPRequest.Create(nil);
Params := TStringList.Create;
try 
Request.CustomHeaders['timestamp'] := '1694751449';
Request.CustomHeaders['sign'] := 'f6f44fedfd3743fd2660bfc5d0038141';
Request.CustomHeaders['Content-Type'] := 'application/json;charset=utf-8';

Params.Add('{"data":"张三"}');

Response:=Client.Post('http://10.2.8.39:8831/public/order', Params);
// 响应结果
if Response.StatusCode = 200 then
begin
ShowMessage(Response.ContentAsString);
end
else
begin
ShowMessage('Error: ' + Response.StatusText);
end;
finally
Params.Free;
Request.Free;
Client.Free;
end;
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/15 21:03:27
6楼: According with some official text, "you dont need say what it will be encoded, because JSON is ALWAYS UTF-8"

1) try use only :  "application/json"  <----

2) dont mix two "charset" when using JSON, or be, the "input text (sending) should be a UTF-8", then the "output (receiving) it will be a UTF-8"!!!
.... Server accept "JSON"
.... Client send "JSON"

------


JSON must be encoded by UTF-8, and there is no "charset" parameter.

RFC 8259 :

IANA Considerations
The media type for JSON text is application/json.
...
Note: No "charset" parameter is defined for this registration. Adding one really has no effect on compliant recipients.

Also,

8.1. Character Encoding

JSON text exchanged between systems that are not part of a closed
ecosystem MUST be encoded using UTF-8 [RFC3629].

Previous specifications of JSON have not required the use of UTF-8 when transmitting JSON text. However, the vast majority of JSON-based software implementations have chosen to use the UTF-8 encoding, to the extent that it is the only encoding that achieves interoperability.

Implementations MUST NOT add a byte order mark (U+FEFF) to the beginning of a networked-transmitted JSON text. In the interests of interoperability, implementations that parse JSON texts MAY ignore the presence of a byte order mark rather than treating it as an error.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 linlingwei (飞雪) ★☆☆☆☆ -
盒子活跃会员
2023/9/16 8:40:56
7楼: 都尝试了,还是没能解决。
----------------------------------------------
-
作者:
男 ghs_79 (ghs) ★☆☆☆☆ -
盒子活跃会员
2023/9/16 11:05:15
8楼: 由于地址不可访问,无法测试,你试试下面代码。
var
  Client: TNetHTTPClient;
  Response: IHTTPResponse;
  Params: TStringStream;
begin
  Client := TNetHTTPClient.Create(nil);
  Params := TStringStream.Create();
  try
    Client.CustomHeaders['timestamp'] := '1694751449';
    Client.CustomHeaders['sign'] := 'f6f44fedfd3743fd2660bfc5d0038141';
    //Client.CustomHeaders['Content-Type'] := 'application/json;charset=utf-8';
    Client.ContentType := 'application/json;charset=utf-8';

    Params.WriteString('{"data":"张三"}');
    Response := Client.Post('http://10.2.8.39:8831/public/order&#39;, Params);
// 响应结果
    if Response.StatusCode = 200 then
    begin
      ShowMessage(Response.ContentAsString);
    end
    else
    begin
      ShowMessage('Error: ' + Response.StatusText);
    end;
  finally
    Params.Free;
    Client.Free;
  end;
end;
----------------------------------------------
Delphi爱好者。
作者:
男 linlingwei (飞雪) ★☆☆☆☆ -
盒子活跃会员
2023/9/16 16:42:35
9楼: @ghs_79 
感谢,问题解决。
----------------------------------------------
-
作者:
男 slan06 (slan) ▲▲△△△ -
普通会员
2023/9/20 12:51:34
10楼: application/json;charset=utf-8这个模式只能把json字符串写入流在发送。
如果是application/x-www-form-urlencoded; charset=UTF-8,可以用strnglist直接发送
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行74.21875毫秒 RSS