winfrom post 向服务器请求数据

  1. public void PostMoths()  
  2.         {  
  3.             string strURL = "http://www.cqjg.gov.cn/newwww/c7/clwz.asp";  
  4.             System.Net.HttpWebRequest request;  
  5.             request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);  
  6.             //Post请求方式  
  7.             request.Method = "POST";  
  8.             //内容类型  
  9.             request.ContentType = "application/x-www-form-urlencoded";  
  10.             //参数经过URL编码  
  11.             string paraUrlCoded = System.Web.HttpUtility.UrlEncode("LicenseTxt");  
  12.             paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarId .Text.Trim());  
  13.             paraUrlCoded +="& "+ System.Web.HttpUtility.UrlEncode("VIN");  
  14.             paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(txtCarNumber.Text.Trim());  
  15.             byte[] payload;  
  16.             //将URL编码后的字符串转化为字节  
  17.             payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);  
  18.             //设置请求的ContentLength   
  19.             request.ContentLength = payload.Length;  
  20.             //获得请求流  
  21.             Stream writer = request.GetRequestStream();  
  22.             //将请求参数写入流  
  23.             writer.Write(payload, 0, payload.Length);  
  24.             //关闭请求流  
  25.             writer.Close();  
  26.             System.Net.HttpWebResponse response;  
  27.             //获得响应流  
  28.             response = (System.Net.HttpWebResponse)request.GetResponse();  
  29.             System.IO.Stream s;  
  30.             s = response.GetResponseStream();  
  31.             string StrDate = "";  
  32.             string strValue = "";  
  33.             StreamReader Reader = new StreamReader(s,Encoding.Default);  
  34.             while ((StrDate= Reader.ReadLine())!=null)  
  35.             {  
  36.                 strValue += StrDate+"\r\n";  
  37.             }  
  38.             // txtInfo.Text = strValue;  
  39.             txtInfo.Text= FindContent(strValue);  
  40.             //XmlTextReader Reader = new XmlTextReader(s);  
  41.             //Reader.MoveToContent();  
  42.             //string strValue = Reader.ReadInnerXml();  
  43.             //strValue = strValue.Replace("<", "<");  
  44.             //strValue = strValue.Replace(">", ">");  
  45.             //MessageBox.Show(strValue);  
  46.             //Reader.Close();  
  47.         }  

展开阅读全文

没有更多推荐了,返回首页