为了更加清楚,这是我填写日期和提到的图标的地方: 解决方案您不能指定响应的格式,除非所请求的资源为此目的提供了明确的输入参数或专用URL(例如,请求响应是以html,xml,json等格式发送).设置 TIdHTTP.Response.ContentType 属性是没有用的.它将被响应的实际 Content-Type 标头覆盖.要在请求中发送 JSON,必须将其发布为 TStream ,例如 TMemoryStream 或 TStringStream ,然后根据需要设置 TIdHTTP.Request.ContentType ,例如: varReqJson:TStringStream;开始ReqJson:= TStringStream.Create('json content here',TEncoding.UTF8);尝试IdHTTP1.Request.ContentType:='application/json';IdHTTP1.Post(URL,ReqJson);最后ReqJson.免费;结尾;结尾; 要接收 JSON, TIdHTTP 可以 将其作为 String (使用服务器报告的字符集进行解码)返回: varReqJson:TStringStream;响应:字符串;开始ReqJson:= TStringStream.Create('json content here',TEncoding.UTF8);尝试IdHTTP1.Request.ContentType:='application/json';RespJson:= IdHTTP1.Post(URL,ReqJson);最后ReqJson.免费;结尾;//根据需要使用RespJson ...结尾; 将原始字节写入您选择的输出 TStream : varReqJson:TStringStream;响应:TMemoryStream;开始RespJson:= TMemoryStream.Create;尝试ReqJson:= TStringStream.Create('json内容在这里',TEncoding.UTF8);尝试IdHTTP1.Request.ContentType:='application/json';RespJson:= IdHTTP1.Post(URL,ReqJson,RespJson);最后ReqJson.免费;结尾;RespJson.Position:= 0;//根据需要使用RespJson ...最后响应免费.结尾;结尾; HTTP响应代码在 TIdHTTP.Response.ResponseCode (和 TIdHTTP.ResponseCode )属性中可用.I have encountered a few sites that uses JSON for request and responseI come across two types :1- application/x-www-form-urlencoded as request and return a response application/json content type2- application/json content type for both request and responsein type 1 i tried changing the the response content type usingmIdHttp.Response.ContentType := 'application/json';but using http analyzer i can see that it doesn't change and its still text/htmlnow i do not know if the problem is with the fact that i can't change content type or not but i do not know how to deal with json !a few question regarding json :1- do i have to encode json data when posting ? how ?2- how can i parse the json response code ? how to get it ? does it need some kind of encode or special convertion ?3- what kind of idhttp setting for json changes with each site and needs configuring ?I understand my questions sound a bit general, but all the other questions are very specific and don't explain the basics when dealing with 'application/json' content type.Edit 1 :thanks to Remy Lebeau answer i was able to successfully work with type 1but i still have trouble sending JSON request, can someone please share an working example, this is one of the sites posting information please use this for your example :One important note : this particular site's post and request content are exactly like each other ! and it baffles me because at the site, i specify an start date and an end date then click on a folder like icon and this post is sent (the one you can see above), and the result should be links (and it is) but instead of appearing only in request content they also appear in post ! (also i'm trying to get the links but at the post the links, the thing i want, are also being sent, how can i post something i don't have !!?)just for more clarity here is the place i fill the date and the icon i mentioned : 解决方案 You cannot specify the format of the response, unless the requested resource offers an explicit input parameter or dedicated URL for that exact purpose (ie, to request a response be sent as html, xml, json, etc). Setting the TIdHTTP.Response.ContentType property is useless. It will be overwritten by the actual Content-Type header of the response.To send JSON in a request, you must post it as a TStream, like TMemoryStream or TStringStream, and set the TIdHTTP.Request.ContentType as needed, eg:var ReqJson: TStringStream;begin ReqJson := TStringStream.Create('json content here', TEncoding.UTF8); try IdHTTP1.Request.ContentType := 'application/json'; IdHTTP1.Post(URL, ReqJson); finally ReqJson.Free; end;end;To receive JSON, TIdHTTP can eitherreturn it as a String (decoded using a server-reported charset):var ReqJson: TStringStream; RespJson: String;begin ReqJson := TStringStream.Create('json content here', TEncoding.UTF8); try IdHTTP1.Request.ContentType := 'application/json'; RespJson := IdHTTP1.Post(URL, ReqJson); finally ReqJson.Free; end; // use RespJson as needed...end;write the raw bytes to an output TStream of your choosing:var ReqJson: TStringStream; RespJson: TMemoryStream;begin RespJson := TMemoryStream.Create; try ReqJson := TStringStream.Create('json content here', TEncoding.UTF8); try IdHTTP1.Request.ContentType := 'application/json'; RespJson := IdHTTP1.Post(URL, ReqJson, RespJson); finally ReqJson.Free; end; RespJson.Position := 0; // use RespJson as needed... finally RespJson.Free; end;end;The HTTP response code is available in the TIdHTTP.Response.ResponseCode (and TIdHTTP.ResponseCode) property. 这篇关于idHttp:如何处理json请求和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 21:45