现在的位置: 首页 > 综合 > 正文

Delphi中使用IXMLHTTPRequest如何用POST方式提交带参

2013年03月26日 ⁄ 综合 ⁄ 共 1027字 ⁄ 字号 评论关闭

http://blog.sina.com.cn/s/blog_51a71c010100gbua.html

说明:服务器端为JAVA,编码UTF-8,返回数据编码UTF-8;数据交换格式JSON。

procedure TloginForm.loginBtnClick(Sender: TObject);
var
  jo: ISuperObject; //JSON接口
  req: IXMLHTTPRequest;
  url: WideString; //要访问的url

  params : string; //要提交的数据
begin
  //要访问的url
  url := 'http://localhost/wuliu/servlet/Login';

  {req := CoXMLHTTP.Create; //Delphi2010用此名称}
  //实例化IXMLHTTPRequest
  req := CoXMLHTTPRequest.Create;
  //调用open方法
  req.open('Post', url, False, EmptyParam, EmptyParam);
  //设置post必要参数
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  //req.SetRequestHeader('Content-Length', intToStr(length(sParams)));

  //提交数据
  params := 'account=大家好&password=好了';

  //发送请求
  req.send(params);

  showMessage(req.responseText);

  //jo := SO(req.responseText);

end;

以上为本人已经实现的方法,已经可以正确提交需要的参数到服务器端。这里注意req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 这个参数是POST参数数据必须有的。params := 'account=大家好&password=好了';是要提交的参数数据,类型为String。这些准备好了,用req.send(params);提交数据到服务器端;得到的数据就是req.responseText);

如有问题请联系:sunylat@gmail.com

抱歉!评论已关闭.