採用delphi7的indy組件java
提交的數據中包含中文,須要進行URL編碼:編碼
function UrlEncode(const ASrc: string): string;
const
UnsafeChars = '*#%<>+ []'; {do not localize}
var
i: Integer;
begin
Result := ''; {Do not Localize}
for i := 1 to Length(ASrc) do begin
if (AnsiPos(ASrc[i], UnsafeChars) > 0) or (ASrc[i]< #32) or (ASrc[i] > #127) then begin
Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2); {do not localize}
end else begin
Result := Result + ASrc[i];
end;
end;
end;code
procedure TForm1.BitBtn1Click(Sender: TObject);
var
Param:String;
F_Params:TStringList;
RStream:TStringStream;
begin
try
F_Params:=TStringList.Create;
RStream:=TStringStream.Create('');
Param:='爲要提交的數據,須要用URL編碼';
Param:=Trim(Param);
F_Params.Add(Param); //添加的第一個參數
F_Params.Add('CurToken=8eeafa5eb583766c'); //添加的第二個參數 orm
//參數順序必定要與java接口中定義的參數順序一致,不然調用失敗接口
IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions-[hoForceEncodeParams];
IdHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';
IdHTTP1.Request.AcceptCharSet:='GBK,utf-8;q=0.7,*;q=0.3';
IdHTTP1.Request.AcceptLanguage:='zh-CN,zh;q=0.8';
IdHTTP1.Post('URL接口地址',F_Params,RStream);
showmessage(Utf8ToAnsi(RStream.DataString)); // 返回值中若是有中文會出現亂碼,因此須要通過Utf8ToAnsi來解碼
finally
FreeAndNil(F_Params);
FreeAndNil(RStream);
end;
end; utf-8