51CTOÏÂÔØ-×Ô¼º¶¯ÊÖ±àдһ¸öä¯ÀÀÆ÷ - ·ÖÏíC#ÏîÄ¿¿ª·¢°¸Àý

·¢²¼Ê±¼ä : ÐÇÆÚÎå ÎÄÕÂ51CTOÏÂÔØ-×Ô¼º¶¯ÊÖ±àдһ¸öä¯ÀÀÆ÷ - ·ÖÏíC#ÏîÄ¿¿ª·¢°¸Àý¸üÐÂÍê±Ï¿ªÊ¼ÔĶÁ

µÚÈýÕ ä¯ÀÀÆ÷³ÌÐò }

}

}

axWebBrowser2.Stop();

67 private void axWebBrowser2_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) { System.IO.FileStream fs= new FileStream(\ FileAccess.Write); StreamWriter sw=new StreamWriter(fs); sw.WriteLine(DateTime.Now.ToShortDateString()+\ sw.Close(); fs.Close(); }

private void axWebBrowser2_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e) { e.cancel=bPop; }

private void OnBeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e) { for(int i=0;i

private void GoBtn_Click(object sender, System.EventArgs e) { System.Object nullObject = 0; string str = \ System.Object nullObjStr = str; Cursor.Current = Cursors.WaitCursor; axWebBrowser2.Navigate(textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr); Cursor.Current = Cursors.Default; }

private void axWebBrowser2_DownloadBegin(object sender, System.EventArgs e) { bPop=false; }

private void axWebBrowser2_DownloadComplete(object sender, System.EventArgs e) { bPop=true; }

68 C#ÍøÂçÓ¦Óð¸Àýµ¼º½ 3.6ÍøÒ³Ô´´úÂëä¯ÀÀ³ÌÐò

3.6.1 ²½Öè1-½¨Á¢³ÌÐò¿ò¼Ü

Ê×ÏÈ£¬Ð½¨Ò»¸ö³ÌÐòÏîÄ¿£¬Èçͼ3-23¡£Ñ¡ÔñʹÓÃWindows ApplicationÄ£°å¡£

ͼ3-23£ºÍøÒ³Ô´Âëä¯ÀÀ³ÌÐò ͼ3-24£º³ÌÐò½çÃæ²¼¾ÖЧ¹û

3.6.2 ²½Öè2-½¨Á¢³ÌÐò½çÃæ

Ê×ÏÈÔÚSolution ExplorerÖÐË«»÷Form1.cs£¬Õâʱ½øÈë½çÃæÉè¼Æ´°¿Ú¡£ÔÚÕâÀÎÒÃÇ¿ÉÒÔʹÓÃToolboxÀïµÄ¹¤¾ß×é¼þÖ±½ÓÌí¼Óµ½³ÌÐòÖС£

·Ö±ðʹÓÃToolboxÖеÄLabel¡¢Button¡¢TextBoxÕâЩ±ê×¼×é¼þ²¼¾Ö³ÌÐò½çÃ棬´ïµ½Í¼3-24µÄЧ¹û¡£

ÔÚ±¾ÀýÖУ¬³ýÁËÒª¸Ä±ä¸÷¸ö×é¼þµÄTextÊôÐÔ£¬¼´×é¼þÏÔʾµÄÎÄ×ÖºÍÇ°¾°±³¾°É«Í⣬»¹Òª¶ÔijЩ×é¼þ×öÌØÊâµÄÉèÖ㬼û±í3-5¡£

±í3-5£º×é¼þµÄÊôÐÔÉèÖÃ

¶ÔÏóÃû³Æ TextBox1 Button1 Button2 TextBox2 ÀàÐÍ TextBox Button Button TextBox ÊôÐÔ Anchor Anchor Anchor WordWrap Anchor ht Multiline ScrollBars True Both Öµ Top,Left,Right Top,Right Top,Right Yes Top,Bottom,Left,Rig´Ëʱ£¬ÏµÍ³»á×Ô¶¯Ìí¼Ó´úÂ룬Éú³ÉÎÒÃÇÐèÒªµÄ²¼¾ÖºÍ×é¼þÊôÐÔ¡£

µÚÈýÕ ä¯ÀÀÆ÷³ÌÐò 69 3.6.3 ²½Öè3-ʵÏÖä¯ÀÀÔ´´úÂ빦ÄÜ

²½ÖèÃèÊö

Ç°ÃæÔø½²µ½¹ý£¬ä¯ÀÀÆ÷ʵ¼ÊÉÏÊÇͨ¹ýHttpЭÒé´ÓÍøÂçÉÏ»ñµÃHTMLÎļþ£¬È»ºóÔÙ½âÊÍÕâЩÎļþ£¬×îºóÔÙÔÚÍøÒ³ÖÐÏÔʾ³öÄÚÈݵġ£ÎÒÃÇÕâ¸öÔ´´úÂëä¯ÀÀÆ÷£¬Êµ¼ÊÉϾÍÊÇÍê³ÉÁËÇ°°ë²¿·Ö£¬¼´Ö»Í¨¹ýHttpЭÒé°ÑHTMLÎļþÏÂÔØÏÂÀ´£¬²»¾­¹ý½âÊÍÖ±½ÓÏÔʾ¸øÓû§¡£Õâ¸ö¹¤×÷ÆäʵºÜ¼òµ¥£¬¿ÉÒÔÖ±½ÓÓÉWebRequestÀàʵÏÖ¡£

¹¦ÄÜʵÏÖ

ÒýÈëÈçϵÄÃû×Ö¿Õ¼äÒÔ¹©ÎÒÃÇÔÚ½ÓÏÂÀ´µÄ±à³ÌÖз½±ãµÄÒýÓÃÆäÖеÄ×é¼þ¡£

using System;

using System.Drawing; using System.Collections;

using System.ComponentModel; using System.Windows.Forms; using System.Net;

using System.Net.Sockets; using System.IO; using System.Text;

1¡¢½¨Á¢»ñµÃÍøÒ³Êý¾ÝµÄÀà

ÎÒÃÇÊ×ÏȽ¨Á¢Ò»¸öGetWebPageSourceÀàÀ´»ñµÃÖ¸¶¨ÍøÖ·µÄÍøÒ³Ô´´úÂ룺

public class GetWebPageSource { public GetWebPageSource() { } public GetWebPageSource(string webAddress) { this.webAddress = webAddress; } public string GetSource() { StringBuilder strSource = new StringBuilder(\ try { WebRequest WReq = WebRequest.Create(this.webAddress); WebResponse WResp = WReq.GetResponse(); // »ñµÃÊý¾ÝÁ÷ StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII); string strTemp = \ while ((strTemp = sr.ReadLine()) != null) { strSource.Append(strTemp + \ } sr.Close(); } catch (WebException WebExcp) { //·¢Éú´íÎóÔò±¨´í MessageBox.Show(WebExcp.Message, \ } return strSource.ToString(); }

70

}

C#ÍøÂçÓ¦Óð¸Àýµ¼º½ //·ÃÎʱäÁ¿µÄ·½·¨

public void setWebAddress(string strNewAddress) { this.webAddress = strNewAddress; }

//˽ÓбäÁ¿

private string webAddress;

ͨ¹ýÕâ¸öÀàµÄGetSourceº¯ÊýÎÒÃǾͿÉÒÔ·½±ãµÄ»ñµÃÖ¸¶¨ÍøÖ·µÄÍøÒ³´úÂë¡£ÕâЩ´úÂëÊÇ×÷ΪstringÀàÐ͵ıäÁ¿·µ»ØµÄ¡£

È»ºó£¬ÎÒÃǵŤ×÷¾ÍÊÇÒªÕûºÏÓû§½çÃæÁË¡£ 2¡¢ÕûºÏÓû§½çÃæ

Ϊ¡°Í˳ö¡±°´Å¥Ìí¼Óµ¥»÷ʼþ£¨·½·¨Ç°ÃæÒѾ­½éÉܹýÁË£©£¬ÔÚÕâÀïÖ»ÐèÒªÌí¼ÓÒ»ÐдúÂ룺

private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); }

Exitº¯Êý»áÁî³ÌÐòÍ˳ö¡£

ÏÂÃæÈç¹ûÄãÏëÈÃÓû§Ôڱ༭¿òÖÐÊäÈëÍêURLµØÖ·ºó°´»Ø³µ¾Í¿ÉÒÔÖ´ÐУ¬¶ø²»ÐèҪŲ¶¯Êó±êÈ¥µã»÷¡°Ô´Â롱°´Å¥µÄ»°£¬ÔòÒªÏìÓ¦KeyPressʼþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { char chr = e.KeyChar; //13 = Enter key if (chr == 13) { GetPageSource(); } }

ÆäÖÐGetPageSourceÊÇÎÒÃǶ¨Ò庯Êý£¬Ëü°ïÖúÎÒÃǼì²éURLÊÇ·ñºÏ·¨£¬²¢Ê¹ÓÃÇ°ÃæдµÄÀàÀ´»ñµÃÍøÒ³Ô´´úÂë²¢ÏÔʾ³öÀ´¡£

public void GetPageSource()

{ string strAddress = textBox1.Text.Trim(); strAddress = strAddress.ToLower(); if ( strAddress.Length <= HTTP.Length) { //URLµØÖ·²»¹»³¤ MessageBox.Show(\ MessageBoxButtons.OK); textBox1.Text = HTTP; } else if (!strAddress.StartsWith(HTTP)) { //ûÓÐÊäÈëhttp://±êʶ MessageBox.Show(\ Identifier\ textBox1.Text = HTTP; } else { // ´´½¨GetWebPageSourceÀà¶ÔÏó

ÁªÏµºÏͬ·¶ÎÄ¿Í·þ£ºxxxxx#qq.com(#Ì滻Ϊ@)