比如想在一个button事件中,自己定义一个html字符串发出post或get的http请求,另一个页面request接收,就和html submit的一样,如何做?用httpwebrequest+httpwebresponse? 能给个例子吗

解决方案 »

  1.   

    HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
    byte[] bdata = Encoding.Default.GetBytes(postData);   
      System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("");   
      myRequest.Method = "POST";   
      myRequest.ContentType = "application/x-www-form-urlencoded";   
      myRequest.ContentLength = bdata.Length;   
      Stream newStream = myRequest.GetRequestStream();   
      

  2.   

    HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
    private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)  
    {  
      byte[] bdata = Encoding.Default.GetBytes(postData);  
      System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrl);  
      myRequest.Method = "POST";  
      myRequest.ContentType = "application/x-www-form-urlencoded";  
      myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";  
      myRequest.Referer = loginUrl;  
      myRequest.KeepAlive = true;  
      myRequest.AllowAutoRedirect = true;  
      if (cookieContainer == null)  
      cookieContainer = new CookieContainer();  
      myRequest.CookieContainer = cookieContainer;  
      myRequest.ContentLength = bdata.Length;  
      Stream newStream = myRequest.GetRequestStream();  
      newStream.Write(l_data, 0, bdata.Length);  
      newStream.Close();  
      HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  
      StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));  
      string content = reader.ReadToEnd();  
      return content;  
    }  
     
     
      

  3.   

    但是如何设置参数名了。比如人家提供的接口是request["name"]该如何办呢