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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM