zoukankan      html  css  js  c++  java
  • Indy10 idhttp返回JSON设置

    https://stackoverflow.com/questions/24025646/delphi-http-post-json

    HTTP1.Request.ContentEncoding should be HTTP1.Request.CharSet instead. UTF-8 is a charset encoding, not a content encoding. And then make sure your JSON data is actually encoded to UTF-8 before posting it. If you are using ASCII characters, the TStringStream code you showed is fine. But if you are using non-ASCII Characters, you need to encode them, such as with Utf8Encode()TIdHTTP does not encode TStream data, it is sent as-is.

    Procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);

    var

    Json: string;

    sResponse: string;

    JsonToSend: TStringStream;

    begin

    Json := '{"auth": {"applicationId": "' + edApplication.text +

    '","applicationPassword": "' + edpassword.text +

    '","accountId": "' + edaccount.text +

    '","userId": "' + edUser.text +

    '"}}';

     

    memoRequest.Text := Json;

     

    JsonToSend := TStringStream.Create(Utf8Encode(Json)); // D2007 and earlier only

    //in D2009 and later, use this instead:

    //JsonToSend := TStringStream.Create(Json, TEncoding.UTF8);

    try

    HTTP1.Request.ContentType := 'application/json';

    HTTP1.Request.CharSet := 'utf-8';

     

    try

    sResponse := HTTP1.Post(cbAddress.Text, JsonToSend);

    except

    on E: Exception do

    ShowMessage('Error on request: '#13#10 + e.Message);

    end;

    finally

    JsonToSend.Free;

    end;

     

    memoResponse.Text := sResponse;

    end;

    Alternatively:

    Procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);

    var

    Json: string;

    sResponse: string;

    JsonToSend: TMemoryStream;

    begin

    Json := '{"auth": {"applicationId": "' + edApplication.text +

    '","applicationPassword": "' + edpassword.text +

    '","accountId": "' + edaccount.text +

    '","userId": "' + edUser.text +

    '"}}';

     

    memoRequest.Text := Json;

     

    JsonToSend := TMemoryStream.Create;

    try

    WriteStringToStream(JsonToSend, Json, enUTF8);

    JsonToSend.Position := 0;

     

    HTTP1.Request.ContentType := 'application/json';

    HTTP1.Request.CharSet := 'utf-8';

     

    try

    sResponse := HTTP1.Post(cbAddress.Text, JsonToSend);

    except

    on E: Exception do

    ShowMessage('Error on request: '#13#10 + e.Message);

    end;

    finally

    JsonToSend.Free;

    end;

     

    memoResponse.Text := sResponse;

    end;

    酒肉穿肠过 佛祖心中留 世人若学我 如同入魔道
  • 相关阅读:
    第二次,营造完整的人生(上)
    御风者(二)——狼王
    我的个人博客
    FTP 协议解析
    关于 Wireshark3 中 GeoIP 的问题
    CentOS8 NextCloud 私有云存储搭建
    Windows10 临时将线程绑定至指定CPU的方法
    CentOS8 yum/dnf 配置国内源(临时)
    Cknife流量分析
    samba + OPENldap 搭建文件共享服务器
  • 原文地址:https://www.cnblogs.com/jspdelphi/p/8336575.html
Copyright © 2011-2022 走看看