MMMHUHU
TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
可以用HttpWebRequest和HttpWebResponse
或者WebRequest和WebResponse
| //調(diào)用方法 Dictionary< string , string > postData = new Dictionary< string , string >(); postData.Add( "user" , "aaa" ); postData.Add( "pass" , "bbb" ); GetPageByPost( "http://www.xxx.com/send.aspx" , postData, Encoding.UTF8); /// <summary> /// 以 Post 方式提交網(wǎng)頁數(shù)據(jù),獲得服務(wù)器返回的數(shù)據(jù) /// </summary> /// <param name="url"> Url </param> /// <param name="postData">Post 數(shù)據(jù)</param> /// <param name="encoder">網(wǎng)頁編碼</param> /// <returns>服務(wù)器返回的數(shù)據(jù)</returns> public string GetPageByPost( string url, Dictionary< string , string > postData, Encoding encoder) { string html = "" ; HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); webReq.Method = "POST" ; Stream reqStream = null ; if (postData != null && postData.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (KeyValuePair< string , string > kv in postData) { sb.Append(HttpUtility.UrlEncode(kv.Key)); sb.Append( "=" ); sb.Append(HttpUtility.UrlEncode(kv.Value)); sb.Append( "&" ); } byte [] data = Encoding.UTF8.GetBytes(sb.ToString().TrimEnd( '&' )); webReq.ContentType = ContentType; webReq.ContentLength = data.Length; reqStream = webReq.GetRequestStream(); reqStream.Write(data, 0, data.Length); } HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse(); Stream stream = webResp.GetResponseStream(); StreamReader sr = new StreamReader(stream, encoder); html = sr.ReadToEnd(); sr.Close(); stream.Close(); if (reqStream != null ) { reqStream.Close(); } return html; } |
查看完整回答