• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
安安的BLOG
安安目前专注电子商务解决方案^_^
博客园    首页    新随笔    联系   管理    订阅  订阅

用System.Web.HttpRequest模拟一个form的post

比如果我的form是这样的  
  <form   method=post   action="http://www.domain.com/abc.aspx">  
      <input   type=text   name="username">  
      <input   type=password   name="pwd">  
  </form>  
   
我想用code来模拟这个post
http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
可以看这个页面

  using   System.IO;  
  using   System.Net;  
   
  String   url   =   "http://www.domain.com/abc.aspx";  
  HttpWebRequest   req   =   (HttpWebRequest)WebRequest.Create(url);  
   
  //if   your   username   and   pwd   contains   special   chars,   you   need   to   call   HttpUtility.UrlEncode   to   encode   the   values  
  string   s   =   "username=abc&pwd=def";  
  byte[]   requestBytes   =   System.Text.Encoding.ASCII.GetBytes   (s);  
  req.Method   =   "POST";  
  req.ContentType   =   "application/x-www-form-urlencoded";  
  req.ContentLength   =   requestBytes.Length;  
  Stream   requestStream   =   req.GetRequestStream();  
  requestStream.Write(requestBytes,0,requestBytes.Length);  
  requestStream.Close();  
   
  HttpWebResponse   res   =   (HttpWebResponse)req.GetResponse();  
  StreamReader   sr   =   new   StreamReader(res.GetResponseStream(),   System.Text.Encoding.Default);  
  String   line   =   streamReader.ReadToEnd();  
  streamReader.Close();  
  res.Close();  
posted @ 2007-03-20 13:48  安安  阅读(970)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3