• 在.net中使用xmlhttp组件读取网页源码


             using MSXML2; 




              string url = "http://www.s**.com";
              
                XMLHTTPClass myXMLHTTP = new XMLHTTPClass();
                //string url = "http://www.s**.com";
                myXMLHTTP.open("GET", url, false, "", "");
                myXMLHTTP.send("");
                this.textBox1.Text = myXMLHTTP.responseText;

    +++++++++++++++++++++++++++++++++++++++


            public static CookieContainer PostLogin()
            {
                string url = "http://www.s**.com";
                //string args = "username=c5k00144&passwd=aa0000";
                //string outdata = "";

                string args = "";
                string outdata = "";

                Encoding encoding = Encoding.GetEncoding("gb2312");

                CookieCollection myCookies = null;
                CookieContainer myCookieContainer = new CookieContainer();

                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
                //myHttpWebRequest.Referer = "http://www.s**.com";
                myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
                myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
                myHttpWebRequest.Method = "POST";
                myHttpWebRequest.CookieContainer = myCookieContainer;
                // 写入
                byte[] b = encoding.GetBytes(args);
                myHttpWebRequest.ContentLength = b.Length;
                using (System.IO.Stream sw = myHttpWebRequest.GetRequestStream())
                {
                    sw.Write(b, 0, b.Length);
                    if (sw != null)
                        sw.Close();
                }
              
             

                HttpWebResponse response = null;
                System.IO.StreamReader sr = null;
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                myCookies = response.Cookies;
                sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);
                outdata = sr.ReadToEnd();
                return myCookieContainer;
            }

            public static string GETCommand(string url, CookieContainer myCookieContainer)
            {
                string outdata = "";
                Encoding encoding = Encoding.GetEncoding("gb2312");

                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                myHttpWebRequest.ContentType = "text/html";
                myHttpWebRequest.Method = "GET";
                myHttpWebRequest.CookieContainer = myCookieContainer;

                HttpWebResponse response = null;
                System.IO.StreamReader sr = null;
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);
                outdata = sr.ReadToEnd();

                return outdata;
            }


            private string GetContentFromUrll(string _requestUrl)
            {
                string _StrResponse = "";
                HttpWebRequest _WebRequest = (HttpWebRequest)WebRequest.Create(_requestUrl);
                _WebRequest.Method = "GET";
                WebResponse _WebResponse = _WebRequest.GetResponse();
                System.IO.StreamReader _ResponseStream = new System.IO.StreamReader(_WebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
                _StrResponse = _ResponseStream.ReadToEnd();
                _WebResponse.Close();
                _ResponseStream.Close();
                return _StrResponse;
            }


            public string GetContentFromWeb(string url, string acyuser, string acypass, string indata)
            {
                StringBuilder sb = new StringBuilder();
                object acyen = false;
                XMLHTTPClass myXMLHTTP = new XMLHTTPClass();
                try
                {
                    myXMLHTTP.open("get", url, acyen, acyuser, acypass);
                    myXMLHTTP.setRequestHeader("Accept-Lauguage", "zh-cn");
                    myXMLHTTP.setRequestHeader("Content-Type", "text/html;charset=gb2312");
                    myXMLHTTP.send(indata);
                    //byte[] DefaultBytes = (byte[])myXMLHTTP.responseBody;
                    //sb.Append(Encoding.Default.GetString(DefaultBytes));
                    sb.Append(myXMLHTTP.responseText);
                }
                catch (Exception ex)
                {
                    string tmp = ex.Message;
                    return "";
                }
                return sb.ToString();
            }

            public string GetContentFromWeb(string url, string indata)
            {
                StringBuilder sb = new StringBuilder();
                object acyen = false;
                object acyuser = null;
                object acypass = null;
                XMLHTTPClass myXMLHTTP = new XMLHTTPClass();
                try
                {
                    myXMLHTTP.open("get", url, acyen, acyuser, acypass);
                    myXMLHTTP.setRequestHeader("Accept-Lauguage", "zh-cn");
                    myXMLHTTP.setRequestHeader("Content-Type", "text/html;charset=gb2312");
                    myXMLHTTP.send(indata);
                    sb.Append(myXMLHTTP.responseText);
                }
                catch (Exception ex)
                {
                    string tmp = ex.Message;
                    return "";
                }
                return sb.ToString();
            }


            public string GetContent(string url)
            {
                string content = "";
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
              
              //  WebResponse myResponseStream = myHttpWebResponse.GetResponseStream();

                System.IO.Stream myResponseStream = myHttpWebResponse.GetResponseStream();

                System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
                content = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return content;
            }

  • 相关阅读:
    Code first常用命令
    vue问题集锦
    net WebConfig读写
    CefSharp使用心得
    工作问题
    工作总结
    HttpContext.Current.Session为null
    Java基础00-接口组成更新31
    Java基础00-Lamda表达式30
    Java基础00-网络编程29
  • 原文地址:https://www.cnblogs.com/Fooo/p/750050.html
Copyright © 2020-2023  润新知