delphi 之 get post

本文介绍了使用Delphi进行HTTP操作的方法,包括设置HTTP请求属性、发送POST请求及处理响应数据。同时展示了如何通过IDHTTP组件上传文件并获取上传文件路径。

没测试过这个

var 
     Source:   TMemoryStream; 
     Response:   TStringStream   ; 
 begin 
 try 
         //   Set   the   properties   for   HTTP 
         idHTTP.Request.Username   :=   'www '; 
         idHTTP.Request.Password   :=   'www '; 
 //         idHTTP1.Request.ProxyServer   :=   '11.133.190.13 '; 
 //         idHTTP1.Request.ProxyPort   :=   StrToIntDef( '80 ',   80); 
         idHTTP.Request.ContentType   :=   'application/x-www-form-urlencoded '; 
           Response   :=   TStringStream.Create( ' '); 
             try 
                     Source   :=   TMemoryStream.Create; 
                     try 
                         Source.LoadFromFile( 'unsub.xml ');   //发送这个XML包,反向取消请求 
                         idHTTP.Post( 'http://222.333.444.555/dsmp/dsmp.wsdl ',   Source,   Response); 
                     finally 
                       //   Source.Free; 
                     end; 

                 memo.Lines.Text   :=   Response.DataString;//返回的字串,可以把它存为XML就可以 
                 memo.Lines.SaveToFile( 'unsubresp.xml ');         //返回的反向取消应答包 
             finally 
                 Response.Free; 
             end; 
     finally 
     end

这个方法试了 可以用

以下仅供参考,要求在web页面写特殊的字符,根据返回的结果判断是否登陆成功,用户名,密码方式见下:

View Code
procedure   TForm1.Button1Click(Sender:   TObject); 
 var 
     Url:   string; 
     Response   :   TStringStream; 
     Request   :   TStrings; 
 begin 
     Memo1.Clear; 
     Response   :=   TStringStream.Create( ' '); 
     try 
         Request   :=   TStringList.Create; 
         try 
             Request.Append( 'txtUserId=hengli&txtPassword=ysj); 
             url   :=   'http://192.168.1.33/nmmis/index.aspx '; 
             IdHTTP1.Request.ContentType   :=   'application/x-www-form-urlencoded '; 
             IdHTTP1.Post(Url,   Request,   Response); 
             Memo1.Lines.Text   :=   Response.DataString; 
         finally 
             FreeAndNil(Request); 
         end; 
     finally 
         FreeAndNil(Response); 
     end; 
 end;

通过idhttp上传文件到web上,然后返回上传的文件路径。

View Code
function   UpLoagFile(FileName:   String):   string; 
 var 
     obj   :   TIdMultiPartFormDataStream; 
     Url:   String; 
 begin 
     obj   :=   TIdMultiPartFormDataStream.Create; 
     try 
         obj.AddFile( 'Image ',FileName,   GetMIMETypeFromFile(FileName)); 
         http.Request.ContentType   :=   obj.RequestContentType; 
         obj.Position   :=   0; 
         Url   :=     'http://192.168.1.44/insertImage.aspx '; 
         Result   :=   http.Post(Url,   obj); 
     finally 
         obj.Free; 
     end; 
 end; 

 服务端,C#: 
 private   void   Page_Load(object   sender,   System.EventArgs   e) 
 { 
         foreach(string   f   in   Request.Files.AllKeys) 
         {   
                 string   fileName,   fullName; 
                 HttpPostedFile   postFile   =   Request.Files[f];   
                 if   (postFile.ContentLength   >   10) 
                 {   
       fileName   =   postFile.FileName; 
       //GetFileName(   fileName,   out   fullName   ); 
       fullName   =   "UpLoad/aaa.jpg " 
       postFile.SaveAs   (   Server.MapPath   (   fullName   )   ); 
       Response.Write   (fullName); 
 } 
         } 
 }

 

 

 

 

 

 

posted on 2012-08-22 10:33 ccqin 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ccqin/archive/2012/08/22/2650348.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值