读书人

C# 获取网页返回值,该如何处理

发布时间: 2012-10-19 16:53:36 作者: rapoo

C# 获取网页返回值
我做一个远程验证用户名跟密码的窗口

窗口上输入用户名跟密码

发到一个比如http://www.xxx.com/login.php 这样的来验证

获取返回值

应该怎么弄...

[解决办法]
HttpWebRequest
[解决办法]
httpwebrequest
用postdata发送类似username=xxx&password=xxx的数据到这个url,其中username这几个字表示你表单上文本框的id,另一个类似。
[解决办法]

C# code
    public class HttpHelper    {        private static string contentType = "application/x-www-form-urlencoded";        private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";        private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";        public static string referer = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=1006102&s_url=http://id.qq.com/index.html";        /// <summary>        /// 获取HTML        /// </summary>        /// <param name="url"></param>        /// <param name="cookieContainer"></param>        /// <param name="method">GET or POST</param>        /// <param name="postData">like "username=admin&password=123"</param>        /// <returns></returns>        public static string GetHtml(string url, CookieContainer cookieContainer, string method = "GET", string postData = "")        {            HttpWebRequest httpWebRequest = null;            HttpWebResponse httpWebResponse = null;            try            {                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);                httpWebRequest.CookieContainer = cookieContainer;                httpWebRequest.ContentType = contentType;                httpWebRequest.Referer = referer;                httpWebRequest.Accept = accept;                httpWebRequest.UserAgent = userAgent;                httpWebRequest.Method = method;                httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;                if (method.ToUpper() == "POST")                {                    byte[] byteRequest = Encoding.Default.GetBytes(postData);                    Stream stream = httpWebRequest.GetRequestStream();                    stream.Write(byteRequest, 0, byteRequest.Length);                    stream.Close();                }                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();                Stream responseStream = httpWebResponse.GetResponseStream();                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);                string html = streamReader.ReadToEnd();                streamReader.Close();                responseStream.Close();                httpWebRequest.Abort();                httpWebResponse.Close();                return html;            }            catch (Exception)            {                return string.Empty;            }        }    }
[解决办法]
写一个有返回值的SQL存储过程吧,

每次提交的时候调用它
[解决办法]
login.aspx,用post方式将数据交给服务器,然后存入IIS的session会话中,或者使用Cookies。然后你的mis系统涉及验证得启用Forms表单验证功能。当从数据库中得到用户名,首先判断是否有用户存在,如果有,再判断是否密码符合。

读书人网 >C#

热点推荐