Delphi xe5 控件TIdhttp的用法post,get解决中文乱码问题

网络接口如下图:

浏览器演示如下:http://xxx.xxx.xxx.xxx/web/login!doLogin?data={"password":"yy123","userCode":"yyy123","terminalCode":"123"}

返回信息是一个json格式的字符串:{"errorMsg":"登录成功!","result":"1","sign":"56519e7b026a4f0e82eeb6496fd5c555"}

现在我想用TIdhttp控件来传递json格式的数据。那么它的方法是Get(AURL:string,AResponseContent:TStream);根据意思可知第二个参数是TStream类型的返回值。

那么下面参数aURL:string,aURL:=‘http://xxx.xxx.xxx.xxx/web/login!doLogin?data='+jo.ToString;

jo:TJsonObject;通过AddPair()添加数值对。用的是ToString方法。

如果我的jo(json)不是这样写的,我直接通过字符串jsontosend:TStringStream;jsontosend:=tstringream.creat('{"password":"yy123","userCode":"yy123","terminalCode":"123"}');那么get的时候就是get(aURL+jsontosend.DataString);

注意:这个时候用get(aURL+jsontosend.ToString);是会格式错误的。 

中文乱码问题:

就是关于第二个参数aResponseContent,(要重命名)

aResponseContent:TStringStream

aResponseContent:=TStringStream.create(' ',65001);这样就可以了。至于这个65001请参考:http://blog.sina.com.cn/s/blog_549f50ec01019cgc.html

把它写到TMemo中:Memo1.lines.add(aResponseContent.DataString);

下面是源码:可能没什么用在实现部分uses DBXjson,如果不知道该引用什么,先选择变量或方法,右键,Refactor--Find Unit。添加一个就好了。

[delphi] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. procedure TForm1.btnTestClick(Sender: TObject);  
  2. var  
  3.   jo: tjsonobject;  
  4.   jp: tjsonpair;  
  5.   responsejson: tstringstream;  
  6.   aURL: string;  
  7.   http: TIdcustomHTTP;  
  8. begin  
  9.   jo := tjsonobject.Create;  
  10.   try  
  11.     jp := tjsonpair.Create('terminalCode', TEterminalcode.Text);  
  12.     jo.AddPair(jp);  
  13.     jp := tjsonpair.Create('password', TEpassword.Text);  
  14.     jo.AddPair(jp);  
  15.     jo.AddPair('userCode', tjsonstring.Create(TEusercode.Text));  
  16.     aURL := 'http://112.64.158.30:7777/web/login!doLogin?data=';  
  17.     responsejson := tstringstream.Create('', 65001);  
  18.      http := TIdHTTP.Create(nil);  
  19.     http.HandleRedirects := true;  
  20.     http.ReadTimeout := 3000;  
  21.     http.Request.Accept := 'text/javascript';  
  22.     http.Request.ContentType := 'application/json';  
  23.     http.Request.CharSet := 'utf-8';  
  24.     http.Request.ContentEncoding := 'utf-8';  
  25.     responsejson := tstringstream.Create('', 65001);  
  26.     http.Get(aURL + jo.ToString, responsejson);  
  27.     Memo1.Lines.Add(responsejson.DataString);  
  28.   finally  
  29.   jo.Free;  
  30.   responsejson.Free;  
  31.   http.Free;  
  32.     end;  
  33. end;  


这个post方法怎么用都不对,后来才知道,http.Request.ContentType := 'application/json'是不对的,应该改为:http.Request.ContentType := 'application/x-www-form-urlencoded';这样就对了。郁闷死,大概服务器接收的不是json类型。关于这类问题可以查看MIME。

[delphi] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. var  
  2.   jo: tjsonobject;  
  3.   jp: tjsonpair;  
  4.   jsontosend: tstringstream;  
  5.   responsestr: string;  
  6.   aURL: string;  
  7.   http: TIdcustomHTTP;  
  8. begin  
  9.   jo := tjsonobject.Create;  
  10.   try  
  11.     jp := tjsonpair.Create('terminalCode', TEterminalcode.Text);//生成tjsonpair  
  12.     jo.AddPair(jp);  
  13.     jp := tjsonpair.Create('password', TEpassword.Text);  
  14.     jo.AddPair(jp);  
  15.     jo.AddPair('userCode', tjsonstring.Create(TEusercode.Text));  
  16.     aURL := 'http://xxx.xxx.xxx.xxx/web/login!doLogin';  
  17.     http := TIdHTTP.Create(nil);  
  18.     http.HandleRedirects := true;  
  19.     http.ReadTimeout := 3000;  
  20. //  http.Request.ContentType := 'application/json';  
  21.      http.Request.ContentType := 'application/x-www-form-urlencoded';  
  22.     http.Request.CharSet := 'utf-8';  
  23.     http.Request.ContentEncoding := 'utf-8';//可以省略,目前感觉没用。  
  24.     jsontosend := tstringstream.Create('data=' + jo.ToString);  
  25.     responsestr := http.Post(aURL, jsontosend);//jsontosend是要发送的json参数  
  26.     if getjsonvalue(responsestr, 'errorMsg') = '登录成功!' then//由于参数不同,返回结果不同,需要判断一下。可以无视  
  27.       TEerrorcode.Text := '0'  
  28.     else  
  29.       TEerrorcode.Text := getjsonvalue(responsestr, 'errorCode');//下面的是显示json解析结果的,可以无视。  
  30.     TEresult.Text := getjsonvalue(responsestr, 'result');  
  31.     TEerrorMsg.Text := getjsonvalue(responsestr, 'errorMsg');  
  32.     TEsign.Text := getjsonvalue(responsestr, 'sign');  
  33.   finally  
  34.     jo.Free;  
  35.     http.Free;  
  36.   end;  

解析json的函数:其实不用这么麻烦,因为要解析的json格式相同,所以写了一个外部unit。下面是这个getjsonvalue函数:


 

[delphi] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. unit UnitJSON;  
  2.   
  3. interface  
  4.   
  5. uses DBXJSON, DBXJSONReflect, System.SysUtils;  
  6. function getJSONValue(jsonstr: string; jsonvalue:string): string;  
  7.   
  8. implementation  
  9.   
  10. function getJSONValue(jsonstr: string; jsonvalue:string): string;  
  11. var  
  12.   jo: tjsonobject;  
  13.   jv: tjsonvalue;  
  14. begin  
  15.   jo := nil;  
  16.   try  
  17.     jo := tjsonobject.Create;  
  18.     jo := tjsonobject.ParseJSONValue(tencoding.UTF8.GetBytes(jsonstr), 0)  
  19.       as tjsonobject;  
  20.     jv := jo.Get(jsonvalue).jsonvalue;  
  21.     Result := jv.Value;  
  22.   finally  
  23.     jo.Free;  
  24.   end;  
  25. end;  
  26.   
  27. end.  


详细的json字符串解析可以看我写的相关帖子,比较详细,也很容易理解。

http://blog.csdn.net/syndicater/article/details/17302857

原文地址:https://www.cnblogs.com/findumars/p/6358516.html